errorsubject.go 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package assert
  2. func Error(value error) *ErrorSubject {
  3. return &ErrorSubject{value: value}
  4. }
  5. type ErrorSubject struct {
  6. Subject
  7. value error
  8. }
  9. func (subject *ErrorSubject) Named(name string) *ErrorSubject {
  10. subject.Subject.Named(name)
  11. return subject
  12. }
  13. func (subject *ErrorSubject) Fail(verb string, other error) {
  14. subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.Error() + ">.")
  15. }
  16. func (subject *ErrorSubject) DisplayString() string {
  17. return subject.Subject.DisplayString(subject.value.Error())
  18. }
  19. func (subject *ErrorSubject) Equals(expectation error) {
  20. if subject.value != expectation {
  21. subject.Fail("is equal to", expectation)
  22. }
  23. }
  24. func (subject *ErrorSubject) IsNil() {
  25. if subject.value != nil {
  26. subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.")
  27. }
  28. }
  29. func (subject *ErrorSubject) IsNotNil() {
  30. if subject.value == nil {
  31. subject.FailWithMessage("Not true that the error is not nil.")
  32. }
  33. }