1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// Copyright 2014 nb.io, LLC
// Author: Cameron Walters <cameron@nb.io>
package st
import (
"fmt"
"testing"
)
func Example_caller() {
f := func() {
file, line := caller()
fmt.Printf("%s:%d", file, line)
}
f() // the output will contain this line's number
// Output: st_test.go:16
}
type stTest struct{}
type stTestInterface interface{}
func TestExpectReject(t *testing.T) {
// Standard expectations
Expect(t, "a", "a")
Expect(t, 42, 42)
Expect(t, nil, nil)
Expect(t, stTestInterface(nil), nil)
Expect(t, []int{42}, []int{42})
// Standard rejections
Reject(t, "a", "A")
Reject(t, 42, int64(42.0))
Reject(t, 42, 42.0)
Reject(t, 42, "42")
Reject(t, []int{42}, []int{41})
Reject(t, stTest{}, nil)
Reject(t, []string{}, nil)
Reject(t, []stTest{}, nil)
var typedNil *stTest
Reject(t, typedNil, nil)
// Table-based test
examples := []struct{ a, b string }{
{"first", "first"},
{"second", "second"},
}
for i, ex := range examples {
Expect(t, ex, ex, i)
Expect(t, &ex, &ex, i)
Reject(t, ex, &ex, i)
Reject(t, ex, 0, i)
Reject(t, ex, "", i)
Reject(t, ex, byte('a'), i)
Reject(t, ex, float64(5.9), i)
}
}
func TestAssertRefute(t *testing.T) {
// Standard assertions
Assert(t, "a", "a")
Assert(t, 42, 42)
Assert(t, nil, nil)
Assert(t, []int{42}, []int{42})
// Standard refutations
Refute(t, "a", "A")
Refute(t, 42, int64(42.0))
Refute(t, 42, 42.0)
Refute(t, 42, "42")
Refute(t, []int{42}, []int{41})
Refute(t, []string{}, nil)
Refute(t, []stTest{}, nil)
// Table-based test
examples := []struct{ a, b string }{
{"first", "first"},
{"second", "second"},
}
// Note: there's no argument to pass the index to assertions.
for _, ex := range examples {
Assert(t, ex, ex)
Assert(t, &ex, &ex)
Refute(t, ex, &ex)
Refute(t, ex, 0)
Refute(t, ex, "")
Refute(t, ex, byte('a'))
Refute(t, ex, float64(5.9))
}
}
func Test_exampleNum(t *testing.T) {
expectationFunc := func(t *testing.T, n ...int) []int {
return n
}
Expect(t, exampleNum(expectationFunc(t)), "")
Expect(t, exampleNum(expectationFunc(t, 0)), "0.")
Expect(t, exampleNum(expectationFunc(t, 1)), "1.")
Expect(t, exampleNum(expectationFunc(t, 2)), "2.")
}
|