-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |