123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package httpexpect
- import (
- "errors"
- "time"
- )
- // Duration provides methods to inspect attached time.Duration value.
- type Duration struct {
- noCopy noCopy
- chain *chain
- value *time.Duration
- }
- // NewDuration returns a new Duration instance.
- //
- // If reporter is nil, the function panics.
- //
- // Example:
- //
- // d := NewDuration(t, time.Second)
- // d.Le(time.Minute)
- func NewDuration(reporter Reporter, value time.Duration) *Duration {
- return newDuration(newChainWithDefaults("Duration()", reporter), &value)
- }
- // NewDurationC returns a new Duration instance with config.
- //
- // Requirements for config are same as for WithConfig function.
- //
- // Example:
- //
- // d := NewDurationC(config, time.Second)
- // d.Le(time.Minute)
- func NewDurationC(config Config, value time.Duration) *Duration {
- return newDuration(newChainWithConfig("Duration()", config.withDefaults()), &value)
- }
- func newDuration(parent *chain, val *time.Duration) *Duration {
- return &Duration{chain: parent.clone(), value: val}
- }
- // Raw returns underlying time.Duration value attached to Duration.
- // This is the value originally passed to NewDuration.
- //
- // Example:
- //
- // d := NewDuration(t, duration)
- // assert.Equal(t, timestamp, d.Raw())
- func (d *Duration) Raw() time.Duration {
- if d.value == nil {
- return 0
- }
- return *d.value
- }
- // Alias is similar to Value.Alias.
- func (d *Duration) Alias(name string) *Duration {
- opChain := d.chain.enter("Alias(%q)", name)
- defer opChain.leave()
- d.chain.setAlias(name)
- return d
- }
- // Deprecated: support for unset durations will be removed. The only method that
- // can create unset duration is Cookie.MaxAge. Instead of Cookie.MaxAge().IsSet(),
- // please use Cookie.HasMaxAge().
- func (d *Duration) IsSet() *Duration {
- opChain := d.chain.enter("IsSet()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- }
- return d
- }
- // Deprecated: support for unset durations will be removed. The only method that
- // can create unset duration is Cookie.MaxAge. Instead of Cookie.MaxAge().NotSet(),
- // please use Cookie.NotHasMaxAge().
- func (d *Duration) NotSet() *Duration {
- opChain := d.chain.enter("NotSet()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if !(d.value == nil) {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is not present"),
- },
- })
- }
- return d
- }
- // IsEqual succeeds if Duration is equal to given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Second)
- // d.IsEqual(time.Second)
- func (d *Duration) IsEqual(value time.Duration) *Duration {
- opChain := d.chain.enter("IsEqual()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value == value) {
- opChain.fail(AssertionFailure{
- Type: AssertEqual,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: durations are equal"),
- },
- })
- }
- return d
- }
- // NotEqual succeeds if Duration is not equal to given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Second)
- // d.NotEqual(time.Minute)
- func (d *Duration) NotEqual(value time.Duration) *Duration {
- opChain := d.chain.enter("NotEqual()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if *d.value == value {
- opChain.fail(AssertionFailure{
- Type: AssertNotEqual,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: durations are non-equal"),
- },
- })
- }
- return d
- }
- // Deprecated: use IsEqual instead.
- func (d *Duration) Equal(value time.Duration) *Duration {
- return d.IsEqual(value)
- }
- // Gt succeeds if Duration is greater than given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute)
- // d.Gt(time.Second)
- func (d *Duration) Gt(value time.Duration) *Duration {
- opChain := d.chain.enter("Gt()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value > value) {
- opChain.fail(AssertionFailure{
- Type: AssertGt,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: duration is larger than given value"),
- },
- })
- }
- return d
- }
- // Ge succeeds if Duration is greater than or equal to given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute)
- // d.Ge(time.Second)
- func (d *Duration) Ge(value time.Duration) *Duration {
- opChain := d.chain.enter("Ge()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value >= value) {
- opChain.fail(AssertionFailure{
- Type: AssertGe,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: duration is larger than or equal to given value"),
- },
- })
- }
- return d
- }
- // Lt succeeds if Duration is lesser than given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Second)
- // d.Lt(time.Minute)
- func (d *Duration) Lt(value time.Duration) *Duration {
- opChain := d.chain.enter("Lt()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value < value) {
- opChain.fail(AssertionFailure{
- Type: AssertLt,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: duration is less than given value"),
- },
- })
- }
- return d
- }
- // Le succeeds if Duration is lesser than or equal to given value.
- //
- // Example:
- //
- // d := NewDuration(t, time.Second)
- // d.Le(time.Minute)
- func (d *Duration) Le(value time.Duration) *Duration {
- opChain := d.chain.enter("Le()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value <= value) {
- opChain.fail(AssertionFailure{
- Type: AssertLe,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{value},
- Errors: []error{
- errors.New("expected: duration is less than or equal to given value"),
- },
- })
- }
- return d
- }
- // InRange succeeds if Duration is within given range [min; max].
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute)
- // d.InRange(time.Second, time.Hour)
- // d.InRange(time.Minute, time.Minute)
- func (d *Duration) InRange(min, max time.Duration) *Duration {
- opChain := d.chain.enter("InRange()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if !(*d.value >= min && *d.value <= max) {
- opChain.fail(AssertionFailure{
- Type: AssertInRange,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{AssertionRange{min, max}},
- Errors: []error{
- errors.New("expected: duration is within given range"),
- },
- })
- }
- return d
- }
- // NotInRange succeeds if Duration is not within given range [min; max].
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute*10)
- // d.NotInRange(time.Minute, time.Minute-time.Nanosecond)
- // d.NotInRange(time.Minute+time.Nanosecond, time.Minute*10)
- func (d *Duration) NotInRange(min, max time.Duration) *Duration {
- opChain := d.chain.enter("NotInRange()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- if *d.value >= min && *d.value <= max {
- opChain.fail(AssertionFailure{
- Type: AssertNotInRange,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{AssertionRange{min, max}},
- Errors: []error{
- errors.New("expected: duration is not within given range"),
- },
- })
- }
- return d
- }
- // InList succeeds if Duration is equal to one of the values from given
- // list of time.Duration.
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute)
- // d.InList(time.Minute, time.Hour)
- func (d *Duration) InList(values ...time.Duration) *Duration {
- opChain := d.chain.enter("InList()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if len(values) == 0 {
- opChain.fail(AssertionFailure{
- Type: AssertUsage,
- Errors: []error{
- errors.New("unexpected empty list argument"),
- },
- })
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- var isListed bool
- for _, v := range values {
- if *d.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{d.value},
- Expected: &AssertionValue{AssertionList(valueList)},
- Errors: []error{
- errors.New("expected: duration is equal to one of the values"),
- },
- })
- }
- return d
- }
- // NotInList succeeds if Duration is not equal to any of the values from
- // given list of time.Duration.
- //
- // Example:
- //
- // d := NewDuration(t, time.Minute)
- // d.NotInList(time.Second, time.Hour)
- func (d *Duration) NotInList(values ...time.Duration) *Duration {
- opChain := d.chain.enter("NotInList()")
- defer opChain.leave()
- if opChain.failed() {
- return d
- }
- if len(values) == 0 {
- opChain.fail(AssertionFailure{
- Type: AssertUsage,
- Errors: []error{
- errors.New("unexpected empty list argument"),
- },
- })
- return d
- }
- if d.value == nil {
- opChain.fail(AssertionFailure{
- Type: AssertNotNil,
- Actual: &AssertionValue{d.value},
- Errors: []error{
- errors.New("expected: duration is present"),
- },
- })
- return d
- }
- for _, v := range values {
- if *d.value == v {
- valueList := make([]interface{}, 0, len(values))
- for _, v := range values {
- valueList = append(valueList, v)
- }
- opChain.fail(AssertionFailure{
- Type: AssertNotBelongs,
- Actual: &AssertionValue{d.value},
- Expected: &AssertionValue{AssertionList(valueList)},
- Errors: []error{
- errors.New("expected: duration is not equal to any of the values"),
- },
- })
- return d
- }
- }
- return d
- }
|