From adecf447e15244207af5dfb7177447d278db7526 Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 13 Nov 2021 14:11:16 +0800 Subject: This closes #1059, represent boolean in XML as 0/1 rather than true/false --- lib_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'lib_test.go') diff --git a/lib_test.go b/lib_test.go index 84a52bb..35dd2a0 100644 --- a/lib_test.go +++ b/lib_test.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/xml" "fmt" + "io" "os" "strconv" "strings" @@ -237,6 +238,36 @@ func TestInStrSlice(t *testing.T) { assert.EqualValues(t, -1, inStrSlice([]string{}, "")) } +func TestBoolValMarshal(t *testing.T) { + bold := true + node := &xlsxFont{B: &attrValBool{Val: &bold}} + data, err := xml.Marshal(node) + assert.NoError(t, err) + assert.Equal(t, ``, string(data)) + + node = &xlsxFont{} + err = xml.Unmarshal(data, node) + assert.NoError(t, err) + assert.NotEqual(t, nil, node) + assert.NotEqual(t, nil, node.B) + assert.NotEqual(t, nil, node.B.Val) + assert.Equal(t, true, *node.B.Val) +} + +func TestBoolValUnmarshalXML(t *testing.T) { + node := xlsxFont{} + assert.NoError(t, xml.Unmarshal([]byte(""), &node)) + assert.Equal(t, true, *node.B.Val) + for content, err := range map[string]string{ + "": "unexpected child of attrValBool", + "": "strconv.ParseBool: parsing \"x\": invalid syntax", + } { + assert.EqualError(t, xml.Unmarshal([]byte(content), &node), err) + } + attr := attrValBool{} + assert.EqualError(t, attr.UnmarshalXML(xml.NewDecoder(strings.NewReader("")), xml.StartElement{}), io.EOF.Error()) +} + func TestBytesReplace(t *testing.T) { s := []byte{0x01} assert.EqualValues(t, s, bytesReplace(s, []byte{}, []byte{}, 0)) -- cgit v1.2.1