errorsubject.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package unit
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type ErrorSubject struct {
  7. *Subject
  8. value error
  9. }
  10. func NewErrorSubject(base *Subject, value error) *ErrorSubject {
  11. return &ErrorSubject{
  12. Subject: base,
  13. value: value,
  14. }
  15. }
  16. func (subject *ErrorSubject) Named(name string) *ErrorSubject {
  17. subject.Subject.Named(name)
  18. return subject
  19. }
  20. func (subject *ErrorSubject) Fail(verb string, other error) {
  21. subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.Error() + ">.")
  22. }
  23. func (subject *ErrorSubject) DisplayString() string {
  24. return subject.Subject.DisplayString(subject.value.Error())
  25. }
  26. func (subject *ErrorSubject) Equals(expectation error) {
  27. if subject.value != expectation {
  28. subject.Fail("is equal to", expectation)
  29. }
  30. }
  31. func (subject *ErrorSubject) IsNil() {
  32. if subject.value != nil {
  33. subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.")
  34. }
  35. }
  36. func (subject *ErrorSubject) HasCode(code int) {
  37. errorPrefix := fmt.Sprintf("[Error 0x%04X]", code)
  38. if !strings.Contains(subject.value.Error(), errorPrefix) {
  39. subject.FailWithMessage(fmt.Sprintf("Not ture that %s has error code 0x%04X.", subject.DisplayString(), code))
  40. }
  41. }