diff options
author | xuri <xuri.me@gmail.com> | 2021-11-13 14:11:16 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2021-11-13 14:11:16 +0800 |
commit | adecf447e15244207af5dfb7177447d278db7526 (patch) | |
tree | 2d144014798c351aa2c9697cfb1ad6bf36f95f08 /lib_test.go | |
parent | 5de671f8bb8a4d8951d5511c3bd1745fc46ce842 (diff) |
This closes #1059, represent boolean in XML as 0/1 rather than true/false
Diffstat (limited to 'lib_test.go')
-rw-r--r-- | lib_test.go | 31 |
1 files changed, 31 insertions, 0 deletions
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, `<xlsxFont><b val="1"></b></xlsxFont>`, 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("<xlsxFont><b val=\"\"></b></xlsxFont>"), &node)) + assert.Equal(t, true, *node.B.Val) + for content, err := range map[string]string{ + "<xlsxFont><b val=\"0\"><i></i></b></xlsxFont>": "unexpected child of attrValBool", + "<xlsxFont><b val=\"x\"></b></xlsxFont>": "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)) |