Skip to content

Commit b20cb42

Browse files
committed
increase test coverage on the generated machines/new.go
1 parent c23ea05 commit b20cb42

File tree

2 files changed

+168
-34
lines changed

2 files changed

+168
-34
lines changed

Diff for: machines/new_test.go

+85-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: machines/types/gen/templates/new_test.go.tmpl

+83-15
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,47 @@ import (
1010
"github.com/robbyt/go-polyscript/execution/data"
1111
"github.com/robbyt/go-polyscript/execution/script"
1212
{{- range .Types}}
13-
_ "github.com/robbyt/go-polyscript/machines/{{.Value}}"
13+
{{.Value}}Compiler "github.com/robbyt/go-polyscript/machines/{{.Value}}/compiler"
1414
{{- end}}
1515
machineTypes "github.com/robbyt/go-polyscript/machines/types"
1616
"github.com/stretchr/testify/mock"
1717
"github.com/stretchr/testify/require"
1818
)
1919

20+
type mockExecutableContent struct {
21+
mock.Mock
22+
}
23+
24+
func (m *mockExecutableContent) GetMachineType() machineTypes.Type {
25+
args := m.Called()
26+
return args.Get(0).(machineTypes.Type)
27+
}
28+
29+
func (m *mockExecutableContent) GetSource() string {
30+
args := m.Called()
31+
return args.String(0)
32+
}
33+
34+
func (m *mockExecutableContent) GetByteCode() any {
35+
args := m.Called()
36+
return args.Get(0)
37+
}
38+
39+
// mockRisorOption is a mock option satisfying risorCompiler.FunctionalOption.
40+
var mockRisorOption risorCompiler.FunctionalOption = func(c *risorCompiler.Compiler) error {
41+
return nil
42+
}
43+
44+
// mockStarlarkOption is a mock option satisfying starlarkCompiler.FunctionalOption.
45+
var mockStarlarkOption starlarkCompiler.FunctionalOption = func(c *starlarkCompiler.Compiler) error {
46+
return nil
47+
}
48+
49+
// mockExtismOption is a mock option satisfying extismCompiler.FunctionalOption.
50+
var mockExtismOption extismCompiler.FunctionalOption = func(c *extismCompiler.Compiler) error {
51+
return nil
52+
}
53+
2054
func TestNewEvaluator(t *testing.T) {
2155
tests := []struct {
2256
name string
@@ -69,21 +103,55 @@ func TestNewEvaluator(t *testing.T) {
69103
}
70104
}
71105

72-
type mockExecutableContent struct {
73-
mock.Mock
74-
}
106+
func TestNewCompiler(t *testing.T) {
107+
tests := []struct {
108+
name string
109+
options []any
110+
expectError bool
111+
expectedErr string
112+
}{
113+
{
114+
name: "No options provided",
115+
options: []any{},
116+
expectError: true,
117+
expectedErr: "no options provided",
118+
},
119+
{
120+
name: "Valid Risor options",
121+
options: []any{mockRisorOption},
122+
expectError: false,
123+
},
124+
{
125+
name: "Valid Starlark options",
126+
options: []any{mockStarlarkOption},
127+
expectError: false,
128+
},
129+
{
130+
name: "Valid Extism options",
131+
options: []any{mockExtismOption},
132+
expectError: false,
133+
},
134+
{
135+
name: "Mixed options",
136+
options: []any{mockRisorOption, mockStarlarkOption},
137+
expectError: true,
138+
expectedErr: "unable to determine compiler type",
139+
},
140+
}
75141

76-
func (m *mockExecutableContent) GetMachineType() machineTypes.Type {
77-
args := m.Called()
78-
return args.Get(0).(machineTypes.Type)
142+
for _, tt := range tests {
143+
t.Run(tt.name, func(t *testing.T) {
144+
comp, err := NewCompiler(tt.options...)
145+
if tt.expectError {
146+
require.Error(t, err)
147+
require.Nil(t, comp)
148+
require.Contains(t, err.Error(), tt.expectedErr)
149+
} else {
150+
require.NoError(t, err)
151+
require.NotNil(t, comp)
152+
}
153+
})
154+
}
79155
}
80156

81-
func (m *mockExecutableContent) GetSource() string {
82-
args := m.Called()
83-
return args.String(0)
84-
}
85157

86-
func (m *mockExecutableContent) GetByteCode() any {
87-
args := m.Called()
88-
return args.Get(0)
89-
}

0 commit comments

Comments
 (0)