123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- package httpexpect
- import (
- "errors"
- )
- // Boolean provides methods to inspect attached bool value
- // (Go representation of JSON boolean).
- type Boolean struct {
- noCopy noCopy
- chain *chain
- value bool
- }
- // NewBoolean returns a new Boolean instance.
- //
- // If reporter is nil, the function panics.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.IsTrue()
- func NewBoolean(reporter Reporter, value bool) *Boolean {
- return newBoolean(newChainWithDefaults("Boolean()", reporter), value)
- }
- // NewBooleanC returns a new Boolean instance with config.
- //
- // Requirements for config are same as for WithConfig function.
- //
- // Example:
- //
- // boolean := NewBooleanC(config, true)
- // boolean.IsTrue()
- func NewBooleanC(config Config, value bool) *Boolean {
- return newBoolean(newChainWithConfig("Boolean()", config.withDefaults()), value)
- }
- func newBoolean(parent *chain, val bool) *Boolean {
- return &Boolean{chain: parent.clone(), value: val}
- }
- // Raw returns underlying value attached to Boolean.
- // This is the value originally passed to NewBoolean.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // assert.Equal(t, true, boolean.Raw())
- func (b *Boolean) Raw() bool {
- return b.value
- }
- // Decode unmarshals the underlying value attached to the Boolean to a target variable.
- // target should be one of these:
- //
- // - pointer to an empty interface
- // - pointer to a boolean
- //
- // Example:
- //
- // value := NewBoolean(t, true)
- //
- // var target bool
- // value.Decode(&target)
- //
- // assert.Equal(t, true, target)
- func (b *Boolean) Decode(target interface{}) *Boolean {
- opChain := b.chain.enter("Decode()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- canonDecode(opChain, b.value, target)
- return b
- }
- // Alias is similar to Value.Alias.
- func (b *Boolean) Alias(name string) *Boolean {
- opChain := b.chain.enter("Alias(%q)", name)
- defer opChain.leave()
- b.chain.setAlias(name)
- return b
- }
- // Path is similar to Value.Path.
- func (b *Boolean) Path(path string) *Value {
- opChain := b.chain.enter("Path(%q)", path)
- defer opChain.leave()
- return jsonPath(opChain, b.value, path)
- }
- // Schema is similar to Value.Schema.
- func (b *Boolean) Schema(schema interface{}) *Boolean {
- opChain := b.chain.enter("Schema()")
- defer opChain.leave()
- jsonSchema(opChain, b.value, schema)
- return b
- }
- // IsTrue succeeds if boolean is true.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.IsTrue()
- func (b *Boolean) IsTrue() *Boolean {
- opChain := b.chain.enter("IsTrue()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if !(b.value == true) {
- opChain.fail(AssertionFailure{
- Type: AssertEqual,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{true},
- Errors: []error{
- errors.New("expected: boolean is true"),
- },
- })
- }
- return b
- }
- // IsFalse succeeds if boolean is false.
- //
- // Example:
- //
- // boolean := NewBoolean(t, false)
- // boolean.IsFalse()
- func (b *Boolean) IsFalse() *Boolean {
- opChain := b.chain.enter("IsFalse()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if !(b.value == false) {
- opChain.fail(AssertionFailure{
- Type: AssertEqual,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{false},
- Errors: []error{
- errors.New("expected: boolean is false"),
- },
- })
- }
- return b
- }
- // Deprecated: use IsTrue instead.
- func (b *Boolean) True() *Boolean {
- return b.IsTrue()
- }
- // Deprecated: use IsFalse instead.
- func (b *Boolean) False() *Boolean {
- return b.IsFalse()
- }
- // IsEqual succeeds if boolean is equal to given value.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.IsEqual(true)
- func (b *Boolean) IsEqual(value bool) *Boolean {
- opChain := b.chain.enter("IsEqual()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if !(b.value == value) {
- opChain.fail(AssertionFailure{
- Type: AssertEqual,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: booleans are equal"),
- },
- })
- }
- return b
- }
- // NotEqual succeeds if boolean is not equal to given value.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.NotEqual(false)
- func (b *Boolean) NotEqual(value bool) *Boolean {
- opChain := b.chain.enter("NotEqual()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if b.value == value {
- opChain.fail(AssertionFailure{
- Type: AssertNotEqual,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: booleans are non-equal"),
- },
- })
- }
- return b
- }
- // Deprecated: use IsEqual instead.
- func (b *Boolean) Equal(value bool) *Boolean {
- return b.IsEqual(value)
- }
- // InList succeeds if boolean is equal to one of the values from given
- // list of booleans.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.InList(true, false)
- func (b *Boolean) InList(values ...bool) *Boolean {
- opChain := b.chain.enter("InList()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if len(values) == 0 {
- opChain.fail(AssertionFailure{
- Type: AssertUsage,
- Errors: []error{
- errors.New("unexpected empty list argument"),
- },
- })
- return b
- }
- var isListed bool
- for _, v := range values {
- if b.value == v {
- isListed = true
- break
- }
- }
- if !isListed {
- valueList := make([]interface{}, 0, len(values))
- for _, v := range values {
- valueList = append(valueList, v)
- }
- opChain.fail(AssertionFailure{
- Type: AssertBelongs,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{AssertionList(valueList)},
- Errors: []error{
- errors.New("expected: boolean is equal to one of the values"),
- },
- })
- }
- return b
- }
- // NotInList succeeds if boolean is not equal to any of the values from
- // given list of booleans.
- //
- // Example:
- //
- // boolean := NewBoolean(t, true)
- // boolean.NotInList(true, false) // failure
- func (b *Boolean) NotInList(values ...bool) *Boolean {
- opChain := b.chain.enter("NotInList()")
- defer opChain.leave()
- if opChain.failed() {
- return b
- }
- if len(values) == 0 {
- opChain.fail(AssertionFailure{
- Type: AssertUsage,
- Errors: []error{
- errors.New("unexpected empty list argument"),
- },
- })
- return b
- }
- for _, v := range values {
- if b.value == v {
- valueList := make([]interface{}, 0, len(values))
- for _, v := range values {
- valueList = append(valueList, v)
- }
- opChain.fail(AssertionFailure{
- Type: AssertNotBelongs,
- Actual: &AssertionValue{b.value},
- Expected: &AssertionValue{AssertionList(valueList)},
- Errors: []error{
- errors.New("expected: boolean is not equal to any of the values"),
- },
- })
- return b
- }
- }
- return b
- }
|