Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reedom committed Mar 6, 2023
1 parent e555517 commit d063438
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/option/field_converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package option_test

import (
"go/token"
"go/types"
"testing"

"github.com/reedom/convergen/pkg/option"
"github.com/stretchr/testify/assert"
)

func TestFieldConverter(t *testing.T) {
// Create a new FieldConverter.
fc := option.NewFieldConverter("myConverter", "srcField", "dstField", token.NoPos)

// Assert that the converter and identifiers were set correctly.
assert.Equal(t, "myConverter", fc.Converter())
assert.Equal(t, "srcField", fc.Src().ExprAt(0))
assert.Equal(t, "dstField", fc.Dst().ExprAt(0))
assert.Equal(t, token.NoPos, fc.Pos())

// Set the types and assert that they were set correctly.
argType := types.Typ[types.Int]
retType := types.Typ[types.String]
fc.Set(argType, retType, true)
assert.Equal(t, argType, fc.ArgType())
assert.Equal(t, retType, fc.RetType())
assert.True(t, fc.RetError())

// Test the Match function.
assert.True(t, fc.Match("srcField", "dstField"))
assert.False(t, fc.Match("srcField2", "dstField2"))

// Test the RHSExpr function.
assert.Equal(t, "myConverter(42)", fc.RHSExpr("42"))
}
59 changes: 59 additions & 0 deletions pkg/option/literal_setter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package option_test

import (
"testing"

"github.com/reedom/convergen/pkg/option"
)

func TestIdentMatcher_Match(t *testing.T) {
testCases := []struct {
name string
pattern string
ident string
exactCase bool
wantResult bool
}{
{"Exact case match", "foo", "foo", true, true},
{"Exact case mismatch", "foo", "bar", true, false},
{"Case-insensitive match", "foo", "Foo", false, true},
{"Case-insensitive mismatch", "foo", "Bar", false, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := option.NewIdentMatcher(tc.pattern)
got := m.Match(tc.ident, tc.exactCase)
if got != tc.wantResult {
t.Errorf("m.Match(%q, %v) = %v; want %v", tc.ident, tc.exactCase, got, tc.wantResult)
}
})
}
}

func TestIdentMatcher_PartialMatch(t *testing.T) {
testCases := []struct {
name string
pattern string
ident string
exactCase bool
wantResult bool
}{
{"Exact case match", "foo.bar", "foo.bar", true, true},
{"Exact case mismatch", "foo.bar", "foo.baz", true, false},
{"Case-insensitive match", "foo.bar", "Foo.Bar", false, true},
{"Case-insensitive mismatch", "foo.bar", "Foo.Baz", false, false},
{"Partial match", "foo.bar.baz", "foo.bar.baz.qux", true, true},
{"Partial mismatch", "foo.bar.baz", "foo.bar.qux.baz", true, false},
{"Partial match case-insensitive", "foo.bar.baz", "foo.Bar.baz.Qux", false, true},
{"Partial mismatch case-insensitive", "foo.bar.baz", "foo.bar.Qux.baz", false, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := option.NewIdentMatcher(tc.pattern)
got := m.PartialMatch(tc.ident, tc.exactCase)
if got != tc.wantResult {
t.Errorf("m.PartialMatch(%q, %v) = %v; want %v", tc.ident, tc.exactCase, got, tc.wantResult)
}
})
}
}

0 comments on commit d063438

Please sign in to comment.