💻 Tech
TOML is another configuration file format like YAML. You can use that to run repeatable test cases in Go. In the example below, the contents of the TOML file can be thought of as array of cases, with each case having the text (test data) and and tokens (expected result). The Text is passed to the function being tested, and the Tokens is checked against the result of calling the function. Each case is run inside a subtest.
type tomlCase struct {
Text string
Tokens []string
}
func loadCases(t *testing.T) []tomlCase {
var testCases struct {
Cases []tomlCase
}
_, err = toml.DecodeFile("tokenize_cases.toml", &testCases)
require.NoError(t, err, "Failed to decode file")
return testCases.Cases
}
func TestTable(t *testing.T) {
for _, tc := range loadCases(t) {
t.Run(tc.Text, func(t *testing.T) {
tokens := Tokenize(tc.Text)
require.Equal(t, tc.Tokens, tokens)
})
}
}
[[cases]]
text = "Who's in first place?"
tokens = ["who", "in", "first", "place"]
[[cases]]
text = "Who's second?"
tokens = ["who", "second"]
[[cases]]
text = ""