Adding tests for NaturalLanguageValues marshalling
Encoding it as a string just when it has a nil value ref
This commit is contained in:
parent
06f0682888
commit
ccea8c926d
|
@ -198,7 +198,8 @@ func (n NaturalLanguageValues) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(nil)
|
||||
}
|
||||
if len(n) == 1 {
|
||||
for _, v := range n {
|
||||
v := n[0]
|
||||
if v.Ref == NilLangRef {
|
||||
return json.Marshal(v.Value)
|
||||
}
|
||||
}
|
||||
|
@ -312,7 +313,7 @@ func unescape(b []byte) []byte {
|
|||
b = bytes.ReplaceAll(b, []byte{'\\', 'r'}, []byte{'\r'})
|
||||
b = bytes.ReplaceAll(b, []byte{'\\', 't'}, []byte{'\t'})
|
||||
b = bytes.ReplaceAll(b, []byte{'\\', 'v'}, []byte{'\v'})
|
||||
b = bytes.ReplaceAll(b, []byte{'\\', '\\'}, []byte{'\\'})
|
||||
b = bytes.ReplaceAll(b, []byte{'\\', '\\'}, []byte{'\\'}) // this should cover the case of \\u -> \u
|
||||
return b
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -612,7 +613,22 @@ func TestNaturalLanguageValueNew(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNaturalLanguageValue_MarshalText(t *testing.T) {
|
||||
t.Skipf("TODO")
|
||||
nlv := LangRefValue{
|
||||
Ref: "en",
|
||||
Value: "test",
|
||||
}
|
||||
tst := NaturalLanguageValues{nlv}
|
||||
j, err := tst.MarshalText()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling: %s", err)
|
||||
}
|
||||
if j == nil {
|
||||
t.Errorf("Error marshalling: nil value returned")
|
||||
}
|
||||
expected := fmt.Sprintf("\"%s[%s]\"", nlv.Value, nlv.Ref)
|
||||
if string(j) != expected {
|
||||
t.Errorf("Wrong value: %s, expected %s", j, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNaturalLanguageValues_Append(t *testing.T) {
|
||||
|
@ -628,7 +644,86 @@ func TestNaturalLanguageValues_Get(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNaturalLanguageValues_MarshalJSON(t *testing.T) {
|
||||
t.Skipf("TODO")
|
||||
{
|
||||
nlv := LangRefValue{
|
||||
Ref: NilLangRef,
|
||||
Value: "test",
|
||||
}
|
||||
tst := NaturalLanguageValues{nlv}
|
||||
j, err := tst.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling: %s", err)
|
||||
}
|
||||
if j == nil {
|
||||
t.Errorf("Error marshalling: nil value returned")
|
||||
}
|
||||
expected := fmt.Sprintf("\"%s\"", nlv.Value)
|
||||
if string(j) != expected {
|
||||
t.Errorf("Wrong value: %s, expected %s", j, expected)
|
||||
}
|
||||
}
|
||||
{
|
||||
nlv := LangRefValue{
|
||||
Ref: "en",
|
||||
Value: "test",
|
||||
}
|
||||
tst := NaturalLanguageValues{nlv}
|
||||
j, err := tst.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling: %s", err)
|
||||
}
|
||||
if j == nil {
|
||||
t.Errorf("Error marshalling: nil value returned")
|
||||
}
|
||||
expected := fmt.Sprintf("{\"%s\":\"%s\"}", nlv.Ref, nlv.Value)
|
||||
if string(j) != expected {
|
||||
t.Errorf("Wrong value: %s, expected %s", j, expected)
|
||||
}
|
||||
}
|
||||
{
|
||||
nlvEn := LangRefValue{
|
||||
Ref: "en",
|
||||
Value: "test",
|
||||
}
|
||||
nlvFr := LangRefValue{
|
||||
Ref: "fr",
|
||||
Value: "teste",
|
||||
}
|
||||
tst := NaturalLanguageValues{nlvEn, nlvFr}
|
||||
j, err := tst.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling: %s", err)
|
||||
}
|
||||
if j == nil {
|
||||
t.Errorf("Error marshalling: nil value returned")
|
||||
}
|
||||
expected := fmt.Sprintf("{\"%s\":\"%s\",\"%s\":\"%s\"}", nlvEn.Ref, nlvEn.Value, nlvFr.Ref, nlvFr.Value)
|
||||
if string(j) != expected {
|
||||
t.Errorf("Wrong value: %s, expected %s", j, expected)
|
||||
}
|
||||
}
|
||||
{
|
||||
nlvEn := LangRefValue{
|
||||
Ref: "en",
|
||||
Value: "test\nwith new line",
|
||||
}
|
||||
nlvFr := LangRefValue{
|
||||
Ref: "fr",
|
||||
Value: "teste\navec une ligne nouvelle",
|
||||
}
|
||||
tst := NaturalLanguageValues{nlvEn, nlvFr}
|
||||
j, err := tst.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling: %s", err)
|
||||
}
|
||||
if j == nil {
|
||||
t.Errorf("Error marshalling: nil value returned")
|
||||
}
|
||||
expected := fmt.Sprintf("{\"%s\":%s,\"%s\":%s}", nlvEn.Ref, strconv.Quote(nlvEn.Value), nlvFr.Ref, strconv.Quote(nlvFr.Value))
|
||||
if string(j) != expected {
|
||||
t.Errorf("Wrong value: %s, expected %s", j, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNaturalLanguageValues_MarshalText(t *testing.T) {
|
||||
|
|
Reference in a new issue