func Test_Int64(t *testing.T) { we := asserter.Using(t) we.CheckThat(int64(1), Is(Int64())) we.CheckThat(int64(1), ToType(Is(Int64Type()))) we.CheckThat(int(1), Is(Not(Int64()))) we.CheckThat(int(1), ToType(Is(Not(Int64Type())))) }
func Test_Empty_onSlices(t *testing.T) { we := asserter.Using(t) empty := []string{} hasTwo := []string{"itsy", "bitsy"} we.CheckThat(empty, Is(Empty())) we.CheckThat(hasTwo, Is(Not(Empty()))) }
func Test_Uint(t *testing.T) { we := asserter.Using(t) we.CheckThat(uint(1), Is(Uint())) we.CheckThat(uint(1), ToType(Is(UintType()))) we.CheckThat(int(1), Is(Not(Uint()))) we.CheckThat(int(1), ToType(Is(Not(UintType())))) }
func Test_PanicWhen_onFunctionAcceptingTwoArgsDotDotDot(t *testing.T) { we := asserter.Using(t) var functionInvoked bool PanicOn13 := PanicWhenApplying(func(arg int, why ...string) { functionInvoked = true if arg == 13 { panic("Superstition") } }, "Disallow13") functionInvoked = false we.CheckThat(PanicOn13.Match("thirteen"), Matched. Comment("Should panic when can't invoke function")) we.CheckFalse(functionInvoked, "Shouldn't have invoked function") functionInvoked = false we.CheckThat(PanicOn13.Match(12), DidNotMatch) we.CheckTrue(functionInvoked, "Should have invoked function") functionInvoked = false we.CheckThat(PanicOn13.Match(13), Matched) we.CheckTrue(functionInvoked, "Should have invoked function") logSamples(t, PanicOn13) }
func Test_Empty_onMaps(t *testing.T) { we := asserter.Using(t) empty := map[string]int{} hasTwo := map[string]int{"foo": 1, "bar": 2} we.CheckThat(empty, Is(Empty())) we.CheckThat(hasTwo, Is(Not(Empty()))) }
func Test_ToLen_onSlices(t *testing.T) { we := asserter.Using(t) empty := []string{} hasTwo := []string{"itsy", "bitsy"} we.CheckThat(empty, ToLen(Is(EqualTo(0)))) we.CheckThat(hasTwo, ToLen(Is(EqualTo(2)))) }
func Test_PanicWhen_onFunctionAcceptingBool(t *testing.T) { we := asserter.Using(t) var functionInvoked bool PanicOnFalse := PanicWhenApplying(func(b bool) { functionInvoked = true if !b { panic("Must be true") } }, "PanicOnFalse") functionInvoked = false we.CheckThat(PanicOnFalse.Match("true"), Matched. Comment("Should panic when can't invoke function")) we.CheckFalse(functionInvoked, "Shouldn't have invoked function") functionInvoked = false we.CheckThat(PanicOnFalse.Match(nil), Matched. Comment("Can't invoke function with string")) we.CheckFalse(functionInvoked, "Shouldn't have invoked function") functionInvoked = false we.CheckThat(PanicOnFalse.Match(true), DidNotMatch) we.CheckTrue(functionInvoked, "Should have invoked function") functionInvoked = false we.CheckThat(PanicOnFalse.Match(false), Matched) we.CheckTrue(functionInvoked, "Should have invoked function") logSamples(t, PanicOnFalse) }
func Test_DeepEqualTo(t *testing.T) { we := asserter.Using(t) data := []interface{}{ nil, true, false, int(42), uint(42), float64(42), complex128(42), struct{ x int }{x: 42}, struct{ x int }{x: 42}, &struct{ x int }{x: 42}, struct{ y int }{y: 42}, _DeepEqualType{x: 42}, &_DeepEqualType{x: 42}, []int{42}, []int{42}, map[int]int{42: 42}, map[int]int{42: 42}, make(chan int, 42), make(chan int, 42), } for _, x := range data { matcher := DeepEqualTo(x) for _, y := range data { message := fmt.Sprintf("%T[%v] and %T[%v]", x, x, y, y) if reflect.DeepEqual(x, y) { we.CheckThat(matcher.Match(y), Matched.Comment(message)) } else { we.CheckThat(matcher.Match(y), DidNotMatch.Comment(message)) } } } logSamples(t, DeepEqualTo(42)) }
func Test_ToLen_onMaps(t *testing.T) { we := asserter.Using(t) empty := map[string]int{} hasTwo := map[string]int{"foo": 1, "bar": 2} we.CheckThat(empty, ToLen(Is(EqualTo(0)))) we.CheckThat(hasTwo, ToLen(Is(EqualTo(2)))) }
func Test_Is(t *testing.T) { we := asserter.Using(t) matcher := Is(True()) we.CheckThat(matcher.Match(true), Matched) we.CheckThat(matcher.Match(false), DidNotMatch) logSamples(t, matcher) }
func Test_SliceTypeOf(t *testing.T) { we := asserter.Using(t) boolSlice := make([]bool, 0, 1) intSlice := make([]int, 0, 1) intSliceSlice := make([][]int, 0, 1) we.CheckThat(boolSlice, Is(SliceOf(BoolType()))) we.CheckThat(boolSlice, Is(Not(SliceOf(IntType())))) we.CheckThat(boolSlice, ToType(Is(SliceTypeOf(BoolType())))) we.CheckThat(boolSlice, ToType(Is(Not(SliceTypeOf(IntType()))))) we.CheckThat(intSlice, Is(Not(SliceOf(BoolType())))) we.CheckThat(intSlice, Is(SliceOf(IntType()))) we.CheckThat(intSlice, ToType(Is(Not(SliceTypeOf(BoolType()))))) we.CheckThat(intSlice, ToType(Is(SliceTypeOf(IntType())))) we.CheckThat(intSliceSlice, Is(Not(SliceOf(IntType())))) we.CheckThat(intSliceSlice, Is(Not(SliceOf(SliceOf(IntType()))))) we.CheckThat(intSliceSlice, Is(Not(SliceTypeOf(SliceOf(IntType()))))) we.CheckThat(intSliceSlice, Is(SliceOf(SliceTypeOf(IntType())))) we.CheckThat(intSliceSlice, ToType(Is(SliceTypeOf(SliceTypeOf(IntType()))))) var intArray = [3]int{1, 2, 3} we.CheckThat(intArray, Is(Not(SliceOf(Anything())))) we.CheckThat(intArray, ToType(Is(Not(SliceTypeOf(Anything()))))) }
func logSamples(t *testing.T, matcher *base.Matcher) { t.Logf("Sample results for: %v\n", matcher) we := asserter.Using(t) for index, value := range sampleValues { t.Logf("Sample #%v: %T[value: %v]\n", index+1, value, value) we.LogResult(matcher.Match(value)) } }
func Test_ToLen(t *testing.T) { we := asserter.Using(t) IsLength2 := ToLen(Is(EqualTo(2))) we.CheckThat(IsLength2.Match([]int{}), DidNotMatch.Comment("no elements")) we.CheckThat(IsLength2.Match([]int{7}), DidNotMatch.Comment("too few")) we.CheckThat(IsLength2.Match([]int{7, 8}), Matched.Comment("just right")) we.CheckThat(IsLength2.Match([]int{7, 8, 9}), DidNotMatch.Comment("too many")) }
func Test_EveryElement_ofSlice(t *testing.T) { we := asserter.Using(t) we.CheckThat([]int{1, 2, 3}, EveryElement(LessThan(4)).Comment("all elements")) we.CheckThat([]int{2, 1, 1}, Not(EveryElement(LessThan(2))).Comment("all but first")) we.CheckThat([]int{1, 2, 1}, Not(EveryElement(LessThan(2))).Comment("all but middle")) we.CheckThat([]int{1, 1, 2}, Not(EveryElement(LessThan(2))).Comment("all but last")) we.CheckThat([]int{}, EveryElement(Anything()).Comment("no elements")) }
func Test_AnyElement_ofSlice(t *testing.T) { we := asserter.Using(t) we.CheckThat([]int{1, 2, 3}, AnyElement(EqualTo(1)).Comment("first element")) we.CheckThat([]int{1, 2, 3}, AnyElement(EqualTo(2)).Comment("middle element")) we.CheckThat([]int{1, 2, 3}, AnyElement(EqualTo(3)).Comment("last element")) we.CheckThat([]int{1, 2, 3}, Not(AnyElement(EqualTo(4))).Comment("none matching")) we.CheckThat([]int{}, Not(AnyElement(Anything())).Comment("no elements")) }
func Test_MapTypeOf(t *testing.T) { we := asserter.Using(t) intStringMap := map[int]string{1: "one", 2: "two"} we.CheckThat(intStringMap, ToType(Is(MapTypeOf(IntType(), StringType())))) we.CheckThat(intStringMap, ToType(Is(Not(MapTypeOf(StringType(), StringType()))))) we.CheckThat(intStringMap, ToType(Is(Not(MapTypeOf(IntType(), IntType()))))) we.CheckThat(intStringMap, ToType(Is(Not(MapTypeOf(StringType(), IntType()))))) }
func Test_Float64(t *testing.T) { we := asserter.Using(t) we.CheckThat(float64(1.0), Is(Float64())) we.CheckThat(float64(1.0), ToType(Is(Float64Type()))) we.CheckThat(float32(1.0), Is(Not(Float64()))) we.CheckThat(float32(1.0), ToType(Is(Not(Float64Type())))) we.CheckThat(complex128(1.0), Is(Not(Float64()))) we.CheckThat(complex128(1.0), ToType(Is(Not(Float64Type())))) }
func Test_SameTypeAs(t *testing.T) { we := asserter.Using(t) we.CheckThat(SameTypeAs(false).Match(true), Matched) we.CheckThat(SameTypeAs(true).Match(false), Matched) we.CheckThat(SameTypeAs(2).Match(1), Matched) we.CheckThat(SameTypeAs(int(2)).Match(uint(1)), DidNotMatch) }
func Test_Complex128(t *testing.T) { we := asserter.Using(t) we.CheckThat(complex128(1.0i), Is(Complex128())) we.CheckThat(complex128(1.0i), ToType(Is(Complex128Type()))) we.CheckThat(complex64(1.0i), Is(Not(Complex128()))) we.CheckThat(complex64(1.0i), ToType(Is(Not(Complex128Type())))) we.CheckThat(float64(1.0), Is(Not(Complex128()))) we.CheckThat(float64(1.0), ToType(Is(Not(Complex128Type())))) }
func Test_NonNil(t *testing.T) { we := asserter.Using(t) matcher := NonNil() we.CheckThat(matcher.Match(false), Matched) we.CheckThat(matcher.Match(0), Matched) we.CheckThat(matcher.Match("nil"), Matched) checkMatcherIsNonMatchingOnNils(t, matcher) logSamples(t, matcher) }
// Check Matchers func TestAnything(t *testing.T) { we := asserter.Using(t) matcher := Anything() we.CheckThat(matcher.Match(true), Matched) we.CheckThat(matcher.Match(false), Matched) we.CheckThat(matcher.Match("foo"), Matched) checkMatcherIsMatchingOnNils(t, matcher) logSamples(t, matcher) }
func Test_False(t *testing.T) { we := asserter.Using(t) matcher := False() we.CheckThat(matcher.Match(true), DidNotMatch) we.CheckThat(matcher.Match(false), Matched) we.CheckThat(matcher.Match("false"), DidNotMatch) we.CheckThat(matcher.Match(0), DidNotMatch) checkMatcherIsNonMatchingOnNils(t, matcher) logSamples(t, matcher) }
func checkMatcherIsNonMatchingOnNils(t *testing.T, matcher *base.Matcher) { we := asserter.Using(t) we.CheckThat(matcher.Match(nil), DidNotMatch.Comment("nil")) we.CheckThat(matcher.Match(uninitialized._pointer), DidNotMatch.Comment("uninitialized pointer")) we.CheckThat(matcher.Match(uninitialized._func), DidNotMatch.Comment("uninitialized func")) we.CheckThat(matcher.Match(uninitialized._slice), DidNotMatch.Comment("uninitialized slice")) we.CheckThat(matcher.Match(uninitialized._chan), DidNotMatch.Comment("uninitialized chan")) we.CheckThat(matcher.Match(uninitialized._map), DidNotMatch.Comment("uninitialized map")) we.CheckThat(matcher.Match(uninitialized._interface), DidNotMatch.Comment("uninitialized interface")) }
func Test_NotEqualTo(t *testing.T) { we := asserter.Using(t) we.CheckThat(3, NotEqualTo(2)) we.CheckThat(3, Not(NotEqualTo(3))) we.CheckThat(3, NotEqualTo("3")) we.CheckThat(int(3), NotEqualTo(uint(3))) we.CheckThat(3, NotEqualTo(nil)) we.CheckThat(nil, NotEqualTo(3)) }
func Test_EachElem(t *testing.T) { we := asserter.Using(t) we.CheckThat(EachElem(NotEqualTo(1)).Match([]int{1, 2, 3}), DidNotMatch.Comment("all but first element")) we.CheckThat(EachElem(NotEqualTo(2)).Match([]int{1, 2, 3}), DidNotMatch.Comment("all but middle element")) we.CheckThat(EachElem(NotEqualTo(3)).Match([]int{1, 2, 3}), DidNotMatch.Comment("all but last element")) we.CheckThat(EachElem(NotEqualTo(4)).Match([]int{1, 2, 3}), Matched.Comment("all match")) we.CheckThat(EachElem(Anything()).Match([]int{}), Matched.Comment("no elements")) }
func Test_AnyElem(t *testing.T) { we := asserter.Using(t) we.CheckThat(AnyElem(EqualTo(1)).Match([]int{1, 2, 3}), Matched.Comment("first element")) we.CheckThat(AnyElem(EqualTo(2)).Match([]int{1, 2, 3}), Matched.Comment("middle element")) we.CheckThat(AnyElem(EqualTo(3)).Match([]int{1, 2, 3}), Matched.Comment("last element")) we.CheckThat(AnyElem(EqualTo(4)).Match([]int{1, 2, 3}), DidNotMatch.Comment("None of the three match")) we.CheckThat(AnyElem(Anything()).Match([]int{}), DidNotMatch.Comment("no elements")) }
func Test_EveryMapElement(t *testing.T) { we := asserter.Using(t) twoMap := map[string]int{"foo": 1, "bar": 2} emptyMap := map[string]int{} we.CheckThat(twoMap, EveryMapElement(GreaterThan(0)). Comment("all entries")) we.CheckThat(twoMap, Not(EveryMapElement(GreaterThan(1))). Comment("not entry [foo: 1]")) we.CheckThat(twoMap, Not(EveryMapElement(LessThan(2))). Comment("not entry [bar: 2]")) we.CheckThat(emptyMap, EveryMapElement(Anything()). Comment("no entries")) }
func Test_AnyMapElement(t *testing.T) { we := asserter.Using(t) twoMap := map[string]int{"foo": 1, "bar": 2} emptyMap := map[string]int{} we.CheckThat(twoMap, AnyMapElement(EqualTo(1)). Comment("entry [foo: 1]")) we.CheckThat(twoMap, AnyMapElement(EqualTo(2)). Comment("entry [bar: 2]")) we.CheckThat(twoMap, Not(AnyMapElement(EqualTo(3))). Comment("neither entry matches")) we.CheckThat(emptyMap, Not(AnyMapElement(Anything())). Comment("no entries")) }
func Test_GreaterThan(t *testing.T) { we := asserter.Using(t) we.CheckThat(3, GreaterThan(2)) we.CheckThat(3, Not(GreaterThan(3))) we.CheckThat(3, Not(GreaterThan(4))) we.CheckThat(3, Not(GreaterThan("2"))) we.CheckThat("3", Not(GreaterThan(2))) we.CheckThat(t, Not(GreaterThan(t))) we.CheckThat(nil, Not(GreaterThan(3))) we.CheckThat(3, Not(GreaterThan(nil))) }
func Test_LessThanOrEqualTo(t *testing.T) { we := asserter.Using(t) we.CheckThat(3, Not(LessThanOrEqualTo(2))) we.CheckThat(3, LessThanOrEqualTo(3)) we.CheckThat(3, LessThanOrEqualTo(4)) we.CheckThat(3, Not(LessThanOrEqualTo("4"))) we.CheckThat("3", Not(LessThanOrEqualTo(4))) we.CheckThat(t, Not(LessThanOrEqualTo(t))) we.CheckThat(nil, Not(LessThanOrEqualTo(3))) we.CheckThat(3, Not(LessThanOrEqualTo(nil))) }