Newline is trimmed, resulting in difference between original string and unmarshal string
rickchristie opened this issue · comments
Hello, wondering if this is the expected behavior, trimming strings makes the package unable to consistently convert strings to YAML and getting it back without the string value changing.
The test case can be run in Go playground here: https://go.dev/play/p/peDcfGBNCbv
Or you can copy and paste this test in the Go playground:
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
type ScriptNode struct {
Script string `yaml:"Script"`
}
func TestStringExactness(t *testing.T) {
original := ScriptNode{
Script: `
if (state.read("throwErr")) {
throw new Error("error thrown from txFork2-1.nFork3")
}
state.write("txFork2-1.nFork3", true)
`,
}
// Marshal into text.
encoded, err := yaml.Marshal(original)
assert.Nil(t, err)
fmt.Println(string(encoded))
// Unmarshal into a new struct.
converted := ScriptNode{}
err = yaml.Unmarshal(encoded, &converted)
assert.Nil(t, err)
// Expect the original string to be same as the one unmarshalled, but the newline is removed.
assert.Equal(t, original.Script, converted.Script)
}
It results in the following failed assertion:
Not equal:
expected: "\n\t\t\t\t\t\tif (state.read(\"throwErr\")) {\n\t\t\t\t\t\t\tthrow new Error(\"error thrown from txFork2-1.nFork3\")\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstate.write(\"txFork2-1.nFork3\", true)\n\t\t\t\t\t"
actual : "\t\t\t\t\t\tif (state.read(\"throwErr\")) {\n\t\t\t\t\t\t\tthrow new Error(\"error thrown from txFork2-1.nFork3\")\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstate.write(\"txFork2-1.nFork3\", true)\n\t\t\t\t\t"