Skip to content

Commit

Permalink
Merge 3532bf1 into 77cce29
Browse files Browse the repository at this point in the history
  • Loading branch information
VikranthBala committed Apr 9, 2023
2 parents 77cce29 + 3532bf1 commit 341fd6f
Showing 1 changed file with 195 additions and 153 deletions.
348 changes: 195 additions & 153 deletions canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,176 +7,218 @@ import (
)

func TestCanon_Number(t *testing.T) {
type (
myInt int
)

chain := newMockChain(t).enter("test")
defer chain.leave()

var (
val float64
ok bool
)

val, ok = canonNumber(chain, 123)
assert.True(t, ok)
assert.Equal(t, 123.0, val)
chain.assert(t, success)
chain.clear()

val, ok = canonNumber(chain, 123.0)
assert.True(t, ok)
assert.Equal(t, 123.0, val)
chain.assert(t, success)
chain.clear()

val, ok = canonNumber(chain, myInt(123))
assert.True(t, ok)
assert.Equal(t, 123.0, val)
chain.assert(t, success)
chain.clear()

_, ok = canonNumber(chain, "123")
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonNumber(chain, nil)
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()
type myInt int

cases := []struct {
name string
in interface{}
val interface{}
result chainResult
}{
{
name: "input is int",
in: 123,
val: 123.0,
result: success,
},
{
name: "input is float",
in: 123.0,
val: 123.0,
result: success,
},
{
name: "input is myInt",
in: myInt(123),
val: 123.0,
result: success,
},
{
name: "input is string",
in: "123",
result: failure,
},
{
name: "input is nil",
in: nil,
result: failure,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()
val, ok := canonNumber(chain, tc.in)
assert.Equal(t, tc.result, chainResult(ok))
chain.assert(t, tc.result)
if tc.result {
assert.Equal(t, tc.val, val)
}
})
}
}

func TestCanon_Array(t *testing.T) {
func TestCannon_Array(t *testing.T) {

type (
myArray []interface{}
myInt int
myArray []interface{}
)

chain := newMockChain(t).enter("test")
defer chain.leave()

var (
val []interface{}
ok bool
)

val, ok = canonArray(chain, []interface{}{123.0, 456.0})
assert.True(t, ok)
assert.Equal(t, []interface{}{123.0, 456.0}, val)
chain.assert(t, success)
chain.clear()

val, ok = canonArray(chain, myArray{myInt(123), 456.0})
assert.True(t, ok)
assert.Equal(t, []interface{}{123.0, 456.0}, val)
chain.assert(t, success)
chain.clear()

_, ok = canonArray(chain, "123")
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonArray(chain, func() {})
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonArray(chain, nil)
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonArray(chain, []interface{}(nil))
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()
cases := []struct {
name string
in interface{}
val interface{}
result chainResult
}{
{
name: "input is []interface{}",
in: []interface{}{123.0, 456.0},
val: []interface{}{123.0, 456.0},
result: success,
},
{
name: "input is myArray{}",
in: myArray{myInt(123), 456.0},
val: []interface{}{123.0, 456.0},
result: success,
},
{
name: "input is string",
in: "123",
result: failure,
},
{
name: "input is func() {}",
in: func() {},
result: failure,
},
{
name: "input is nil",
in: nil,
result: failure,
},
{
name: "input is []interface{}(nil)",
in: []interface{}(nil),
result: failure,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()
val, ok := canonArray(chain, tc.in)
assert.Equal(t, tc.result, chainResult(ok))
chain.assert(t, tc.result)
if tc.result {
assert.Equal(t, tc.val, val)
}
})
}
}

func TestCanon_Map(t *testing.T) {

type (
myMap map[string]interface{}
myInt int
myMap map[string]interface{}
)

chain := newMockChain(t).enter("test")
defer chain.leave()

var (
val map[string]interface{}
ok bool
)

val, ok = canonMap(chain, map[string]interface{}{"foo": 123.0})
assert.True(t, ok)
assert.Equal(t, map[string]interface{}{"foo": 123.0}, val)
chain.assert(t, success)
chain.clear()

val, ok = canonMap(chain, myMap{"foo": myInt(123)})
assert.True(t, ok)
assert.Equal(t, map[string]interface{}{"foo": 123.0}, val)
chain.assert(t, success)
chain.clear()

_, ok = canonMap(chain, "123")
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonMap(chain, func() {})
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonMap(chain, nil)
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()

_, ok = canonMap(chain, map[string]interface{}(nil))
assert.False(t, ok)
chain.assert(t, failure)
chain.clear()
cases := []struct {
name string
in interface{}
val interface{}
result chainResult
}{
{
name: "input is map[string]interface{}{}",
in: map[string]interface{}{"foo": 123.0},
val: map[string]interface{}{"foo": 123.0},
result: success,
},
{
name: "input is myMap{}",
in: myMap{"foo": myInt(123)},
val: map[string]interface{}{"foo": 123.0},
result: success,
},
{
name: "input is string",
in: "123",
result: failure,
},
{
name: "input is func() {}",
in: func() {},
result: failure,
},
{
name: "input is nil",
in: nil,
result: failure,
},
{
name: "input is map[string]interface{}(nil)",
in: map[string]interface{}(nil),
result: failure,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()
val, ok := canonMap(chain, tc.in)
assert.Equal(t, tc.result, chainResult(ok))
chain.assert(t, tc.result)
if tc.result {
assert.Equal(t, tc.val, val)
}
})
}
}

func TestCannon_Decode(t *testing.T) {
t.Run("target is nil", func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()

canonDecode(chain, 123, nil)

chain.assert(t, failure)
})

t.Run("value is not marshallable", func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()

type S struct {
MyFunc func() string
}

value := &S{
MyFunc: func() string { return "foo" },
}

var target S
canonDecode(chain, value, &target)

chain.assert(t, failure)
})

t.Run("value is not unmarshallable into target", func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()

var target int
canonDecode(chain, true, target)
type S struct {
MyFunc func() string
}
var (
target S
targetInt int
)

chain.assert(t, failure)
})
cases := []struct {
name string
value interface{}
target interface{}
}{
{
name: "target is nil",
value: 123,
target: nil,
},
{
name: "value is not marshallable",
value: &S{MyFunc: func() string { return "foo" }},
target: &target,
},
{
name: "value is not unmarshallable into target",
value: true,
target: targetInt,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
chain := newMockChain(t).enter("test")
defer chain.leave()
canonDecode(chain, tc.value, tc.target)
chain.assert(t, failure)
})
}
}

0 comments on commit 341fd6f

Please sign in to comment.