Files
go-toml/toml_testgen_support_test.go
T
Cameron Moore 62acca2b68 tomltestgen: add toml-test unit test generation command (#610)
Tests are hidden behind a "testsuite" build tag for now since many tests
are failing.  Use `go test -tags testsuite` to activate.

Use `go generate` to regenerate toml_testgen_test.go.

Co-authored-by: Thomas Pelletier <thomas@pelletier.codes>
2021-10-03 22:15:30 -04:00

46 lines
1.0 KiB
Go

//go:generate go run ./cmd/tomltestgen/main.go -o toml_testgen_test.go
// This is a support file for toml_testgen_test.go
package toml_test
import (
"encoding/json"
"testing"
"github.com/pelletier/go-toml/v2/testsuite"
"github.com/stretchr/testify/require"
)
func testgenInvalid(t *testing.T, input string) {
t.Helper()
t.Logf("Input TOML:\n%s", input)
doc := map[string]interface{}{}
err := testsuite.Unmarshal([]byte(input), &doc)
if err == nil {
out, err := json.Marshal(doc)
if err != nil {
panic("could not marshal map to json")
}
t.Log("JSON output from unmarshal:", string(out))
t.Fatalf("test did not fail")
}
}
func testgenValid(t *testing.T, input string, jsonRef string) {
t.Helper()
t.Logf("Input TOML:\n%s", input)
// TODO: change this to interface{}
var doc map[string]interface{}
err := testsuite.Unmarshal([]byte(input), &doc)
if err != nil {
t.Fatalf("failed parsing toml: %s", err)
}
j, err := testsuite.ValueToTaggedJSON(doc)
require.NoError(t, err)
require.Equal(t, jsonRef, string(j)+"\n")
}