Make ToString() return an error instead of panic (#117)

Fixes #100
This commit is contained in:
Thomas Pelletier
2017-01-15 18:49:11 -08:00
committed by GitHub
parent ee2c0b51cf
commit a1f048ba24
3 changed files with 47 additions and 9 deletions
+17 -2
View File
@@ -1,6 +1,7 @@
package toml
import (
"errors"
"reflect"
"strings"
"testing"
@@ -15,7 +16,8 @@ points = { x = 1, y = 2 }`)
t.Fatal("Unexpected error:", err)
}
reparsedTree, err := Load(toml.ToString())
tomlString, _ := toml.ToString()
reparsedTree, err := Load(tomlString)
assertTree(t, reparsedTree, err, map[string]interface{}{
"name": map[string]interface{}{
@@ -39,7 +41,7 @@ func TestTomlTreeConversionToStringKeysOrders(t *testing.T) {
foo = 1
bar = "baz2"`)
stringRepr := tree.ToString()
stringRepr, _ := tree.ToString()
t.Log("Intermediate string representation:")
t.Log(stringRepr)
@@ -69,6 +71,19 @@ func testMaps(t *testing.T, actual, expected map[string]interface{}) {
}
}
func TestToStringTypeConversionError(t *testing.T) {
tree := TomlTree{
values: map[string]interface{}{
"thing": []string{"unsupported"},
},
}
_, err := tree.ToString()
expected := errors.New("unsupported value type []string: [unsupported]")
if err.Error() != expected.Error() {
t.Errorf("expecting error %s, but got %s instead", expected, err)
}
}
func TestTomlTreeConversionToMapSimple(t *testing.T) {
tree, _ := Load("a = 42\nb = 17")