Rewrite TomlTree encoding (#133)

* Rewrite `TomlTree` encoding
* Introduce `TomlTree.WriteTo`
This commit is contained in:
Thomas Pelletier
2017-03-02 09:17:06 -08:00
committed by GitHub
parent 3616783228
commit 7e6e4b1314
5 changed files with 331 additions and 279 deletions
+7 -39
View File
@@ -633,22 +633,13 @@ func TestParseKeyGroupArraySpec(t *testing.T) {
})
}
func TestToTomlValue(t *testing.T) {
func TestTomlValueStringRepresentation(t *testing.T) {
for idx, item := range []struct {
Value interface{}
Expect string
}{
{int(1), "1"},
{int8(2), "2"},
{int16(3), "3"},
{int32(4), "4"},
{int64(12345), "12345"},
{uint(10), "10"},
{uint8(20), "20"},
{uint16(30), "30"},
{uint32(40), "40"},
{uint64(50), "50"},
{float32(12.456), "12.456"},
{float64(123.45), "123.45"},
{bool(true), "true"},
{"hello world", "\"hello world\""},
@@ -660,42 +651,19 @@ func TestToTomlValue(t *testing.T) {
"[\"gamma\",\"delta\"]"},
{nil, ""},
} {
result := toTomlValue(item.Value, 0)
result, err := tomlValueStringRepresentation(item.Value)
if err != nil {
t.Errorf("Test %d - unexpected error: %s", idx, err)
}
if result != item.Expect {
t.Errorf("Test %d - got '%s', expected '%s'", idx, result, item.Expect)
}
}
}
func TestToString(t *testing.T) {
tree, err := Load("[foo]\n\n[[foo.bar]]\na = 42\n\n[[foo.bar]]\na = 69\n")
if err != nil {
t.Errorf("Test failed to parse: %v", err)
return
}
result, err := tree.ToString()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
expected := "\n[foo]\n\n [[foo.bar]]\n a = 42\n\n [[foo.bar]]\n a = 69\n"
if result != expected {
t.Errorf("Expected got '%s', expected '%s'", result, expected)
}
}
func TestToStringMapStringString(t *testing.T) {
in := map[string]interface{}{"m": map[string]string{"v": "abc"}}
want := "\n[m]\n v = \"abc\"\n"
tree := TreeFromMap(in)
got := tree.String()
if got != want {
t.Errorf("want:\n%q\ngot:\n%q", want, got)
}
}
func TestToStringMapInterfaceInterface(t *testing.T) {
in := map[string]interface{}{"m": map[interface{}]interface{}{"v": "abc"}}
in := map[string]interface{}{"m": TreeFromMap(map[string]interface{}{
"v": &tomlValue{"abc", Position{0, 0}}})}
want := "\n[m]\n v = \"abc\"\n"
tree := TreeFromMap(in)
got := tree.String()