github.com/BrandonManuel/git-chglog@v0.0.0-20200903004639-7a62fa08787a/utils_test.go (about) 1 package chglog 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestDotGet(t *testing.T) { 11 assert := assert.New(t) 12 now := time.Now() 13 14 type Nest struct { 15 Str string 16 Int int 17 Time time.Time 18 } 19 20 type Sample struct { 21 Str string 22 Int int 23 Date time.Time 24 Nest Nest 25 } 26 27 sample := Sample{ 28 Str: "sample_string", 29 Int: 12, 30 Date: now, 31 Nest: Nest{ 32 Str: "nest_string", 33 Int: 34, 34 Time: now, 35 }, 36 } 37 38 var val interface{} 39 var ok bool 40 41 // .Str 42 val, ok = dotGet(&sample, "Str") 43 assert.True(ok) 44 assert.Equal(val, "sample_string") 45 46 // Lowercase 47 val, ok = dotGet(&sample, "str") 48 assert.True(ok) 49 assert.Equal(val, "sample_string") 50 51 // Int 52 val, ok = dotGet(&sample, "Int") 53 assert.True(ok) 54 assert.Equal(val, 12) 55 56 // Time 57 val, ok = dotGet(&sample, "Date") 58 assert.True(ok) 59 assert.Equal(val, now) 60 61 // Nest 62 val, ok = dotGet(&sample, "Nest.Str") 63 assert.True(ok) 64 assert.Equal(val, "nest_string") 65 66 val, ok = dotGet(&sample, "Nest.Int") 67 assert.True(ok) 68 assert.Equal(val, 34) 69 70 val, ok = dotGet(&sample, "Nest.Time") 71 assert.True(ok) 72 assert.Equal(val, now) 73 74 val, ok = dotGet(&sample, "nest.int") 75 assert.True(ok) 76 assert.Equal(val, 34) 77 78 // Notfound 79 val, ok = dotGet(&sample, "not.found") 80 assert.False(ok) 81 assert.Nil(val) 82 } 83 84 func TestCompare(t *testing.T) { 85 assert := assert.New(t) 86 87 type sample struct { 88 a interface{} 89 op string 90 b interface{} 91 expected bool 92 } 93 94 table := []sample{ 95 {0, "<", 1, true}, 96 {0, ">", 1, false}, 97 {1, ">", 0, true}, 98 {1, "<", 0, false}, 99 {"a", "<", "b", true}, 100 {"a", ">", "b", false}, 101 {time.Unix(1518018017, 0), "<", time.Unix(1518018043, 0), true}, 102 {time.Unix(1518018017, 0), ">", time.Unix(1518018043, 0), false}, 103 } 104 105 for _, sa := range table { 106 actual, err := compare(sa.a, sa.op, sa.b) 107 assert.Nil(err) 108 assert.Equal(sa.expected, actual) 109 } 110 }