2019-12-01 19:44:44 +00:00
|
|
|
package marshal
|
2019-11-23 03:52:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-01 20:10:42 +00:00
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
"github.com/mikefarah/yq/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonToString(t *testing.T) {
|
|
|
|
var data = test.ParseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
2019-12-01 19:44:44 +00:00
|
|
|
got, _ := NewJsonConverter().JsonToString(data)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t, "{\"b\":{\"c\":2}}", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonToString_withIntKey(t *testing.T) {
|
|
|
|
var data = test.ParseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
2: c
|
|
|
|
`)
|
2019-12-01 19:44:44 +00:00
|
|
|
got, _ := NewJsonConverter().JsonToString(data)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t, `{"b":{"2":"c"}}`, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonToString_withBoolKey(t *testing.T) {
|
|
|
|
var data = test.ParseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
false: c
|
|
|
|
`)
|
2019-12-01 19:44:44 +00:00
|
|
|
got, _ := NewJsonConverter().JsonToString(data)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t, `{"b":{"false":"c"}}`, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonToString_withArray(t *testing.T) {
|
|
|
|
var data = test.ParseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
- item: one
|
|
|
|
- item: two
|
|
|
|
`)
|
2019-12-01 19:44:44 +00:00
|
|
|
got, _ := NewJsonConverter().JsonToString(data)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t, "{\"b\":[{\"item\":\"one\"},{\"item\":\"two\"}]}", got)
|
|
|
|
}
|