yq/pkg/yqlib/lua_test.go
Kim Alvefur 0b54f9caca Add initial test for Lua encoder
Boilerplate mostly copied from toml_test.go
2023-08-05 17:08:38 +02:00

52 lines
1.3 KiB
Go

package yqlib
import (
"bufio"
"fmt"
"testing"
"github.com/mikefarah/yq/v4/test"
)
var luaScenarios = []formatScenario{
{
skipDoc: true,
description: "Basic example",
input: "hello: world\n? look: non-string keys\n: True\nnumbers: [123,456]\n",
expected: "return {[\"hello\"]=\"world\";[{[\"look\"]=\"non-string keys\";}]=true;[\"numbers\"]={123,456,};};\n",
scenarioType: "encode",
},
}
func testLuaScenario(t *testing.T, s formatScenario) {
switch s.scenarioType {
case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences)), s.description)
default:
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
}
}
func documentLuaScenario(t *testing.T, w *bufio.Writer, i interface{}) {
s := i.(formatScenario)
if s.skipDoc {
return
}
switch s.scenarioType {
default:
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
}
}
func TestLuaScenarios(t *testing.T) {
for _, tt := range luaScenarios {
testLuaScenario(t, tt)
}
genericScenarios := make([]interface{}, len(luaScenarios))
for i, s := range luaScenarios {
genericScenarios[i] = s
}
documentScenarios(t, "usage", "lua", genericScenarios, documentLuaScenario)
}