boolsubject.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package assert
  2. import (
  3. "strconv"
  4. )
  5. // Assert on a boolean variable.
  6. func Bool(value bool) *BoolSubject {
  7. return &BoolSubject{value: value}
  8. }
  9. type BoolSubject struct {
  10. Subject
  11. value bool
  12. }
  13. func (subject *BoolSubject) Named(name string) *BoolSubject {
  14. subject.Subject.Named(name)
  15. return subject
  16. }
  17. func (subject *BoolSubject) Fail(verb string, other bool) {
  18. subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatBool(other) + ">.")
  19. }
  20. func (subject *BoolSubject) DisplayString() string {
  21. return subject.Subject.DisplayString(strconv.FormatBool(subject.value))
  22. }
  23. // to be equal to another boolean variable.
  24. func (subject *BoolSubject) Equals(expectation bool) {
  25. if subject.value != expectation {
  26. subject.Fail("is equal to", expectation)
  27. }
  28. }
  29. // to be true.
  30. func (subject *BoolSubject) IsTrue() {
  31. if subject.value != true {
  32. subject.Fail("is", true)
  33. }
  34. }
  35. // to be false.
  36. func (subject *BoolSubject) IsFalse() {
  37. if subject.value != false {
  38. subject.Fail("is", false)
  39. }
  40. }