Add indentation setting for Encoder (#386)

Fixes #371
This commit is contained in:
x-hgg-x
2020-05-04 19:21:22 +02:00
committed by GitHub
parent cc3100c329
commit 82a6a1977d
3 changed files with 59 additions and 11 deletions
+33
View File
@@ -44,6 +44,19 @@ Zstring = "Hello"
String2 = "One"
`)
var basicTestTomlCustomIndentation = []byte(`Ystrlist = ["Howdy","Hey There"]
Zstring = "Hello"
[[Wsublist]]
String2 = "Two"
[[Wsublist]]
String2 = "Three"
[Xsubdoc]
String2 = "One"
`)
var basicTestTomlOrdered = []byte(`Zstring = "Hello"
Ystrlist = ["Howdy","Hey There"]
@@ -207,6 +220,26 @@ func TestBasicMarshal(t *testing.T) {
}
}
func TestBasicMarshalCustomIndentation(t *testing.T) {
var result bytes.Buffer
err := NewEncoder(&result).Indentation("\t").Encode(basicTestData)
if err != nil {
t.Fatal(err)
}
expected := basicTestTomlCustomIndentation
if !bytes.Equal(result.Bytes(), expected) {
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result.Bytes())
}
}
func TestBasicMarshalWrongIndentation(t *testing.T) {
var result bytes.Buffer
err := NewEncoder(&result).Indentation(" \n").Encode(basicTestData)
if err.Error() != "invalid indentation: must only contains space or tab characters" {
t.Error("expect err:invalid indentation: must only contains space or tab characters but got:", err)
}
}
func TestBasicMarshalOrdered(t *testing.T) {
var result bytes.Buffer
err := NewEncoder(&result).Order(OrderPreserve).Encode(basicTestData)