|
12 | 12 | package excelize
|
13 | 13 |
|
14 | 14 | import (
|
| 15 | + "fmt" |
15 | 16 | "path/filepath"
|
| 17 | + "slices" |
16 | 18 | "testing"
|
| 19 | + "time" |
17 | 20 |
|
18 | 21 | "github.com/stretchr/testify/assert"
|
19 | 22 | )
|
@@ -116,3 +119,70 @@ func TestGetDocProps(t *testing.T) {
|
116 | 119 | _, err = f.GetDocProps()
|
117 | 120 | assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
118 | 121 | }
|
| 122 | + |
| 123 | +func TestCustomProps(t *testing.T) { |
| 124 | + f := NewFile() |
| 125 | + expected := []CustomProperty{ |
| 126 | + {Name: "Text Prop", Value: "text"}, |
| 127 | + {Name: "Boolean Prop 1", Value: true}, |
| 128 | + {Name: "Boolean Prop 2", Value: false}, |
| 129 | + {Name: "Number Prop 1", Value: -123.456}, |
| 130 | + {Name: "Number Prop 2", Value: int32(1)}, |
| 131 | + {Name: "Date Prop", Value: time.Date(2021, time.September, 11, 0, 0, 0, 0, time.UTC)}, |
| 132 | + } |
| 133 | + for _, prop := range expected { |
| 134 | + assert.NoError(t, f.SetCustomProps(prop)) |
| 135 | + } |
| 136 | + props, err := f.GetCustomProps() |
| 137 | + assert.NoError(t, err) |
| 138 | + assert.Equal(t, expected, props) |
| 139 | + |
| 140 | + // Test delete custom property |
| 141 | + assert.NoError(t, f.SetCustomProps(CustomProperty{Name: "Boolean Prop 1", Value: nil})) |
| 142 | + props, err = f.GetCustomProps() |
| 143 | + assert.NoError(t, err) |
| 144 | + expected = slices.Delete(expected, 1, 2) |
| 145 | + assert.Equal(t, expected, props) |
| 146 | + |
| 147 | + // Test change custom property value data type |
| 148 | + assert.NoError(t, f.SetCustomProps(CustomProperty{Name: "Boolean Prop 2", Value: "true"})) |
| 149 | + props, err = f.GetCustomProps() |
| 150 | + assert.NoError(t, err) |
| 151 | + assert.Equal(t, props[1].Value, "true") |
| 152 | + |
| 153 | + // Test set custom property with unsupported value data type |
| 154 | + assert.Equal(t, ErrParameterInvalid, f.SetCustomProps(CustomProperty{Name: "Boolean Prop 2", Value: 1})) |
| 155 | + |
| 156 | + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCustomProps.xlsx"))) |
| 157 | + assert.NoError(t, f.Close()) |
| 158 | + |
| 159 | + // Test set custom property without property name |
| 160 | + f = NewFile() |
| 161 | + assert.Equal(t, ErrParameterInvalid, f.SetCustomProps(CustomProperty{})) |
| 162 | + |
| 163 | + // Test set custom property with unsupported charset |
| 164 | + f.Pkg.Store(defaultXMLPathDocPropsCustom, MacintoshCyrillicCharset) |
| 165 | + assert.EqualError(t, f.SetCustomProps(CustomProperty{Name: "Prop"}), "XML syntax error on line 1: invalid UTF-8") |
| 166 | + |
| 167 | + // Test get custom property with unsupported charset |
| 168 | + _, err = f.GetCustomProps() |
| 169 | + assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8") |
| 170 | + |
| 171 | + // Test set custom property with unsupported charset content types |
| 172 | + f = NewFile() |
| 173 | + f.ContentTypes = nil |
| 174 | + f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) |
| 175 | + assert.EqualError(t, f.SetCustomProps(CustomProperty{Name: "Prop"}), "XML syntax error on line 1: invalid UTF-8") |
| 176 | + |
| 177 | + // Test get custom property with unsupported charset |
| 178 | + f.Pkg.Store(defaultXMLPathDocPropsCustom, []byte(fmt.Sprintf(`<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="%s"><property fmtid="%s" pid="2" name="Prop"><vt:filetime>x</vt:filetime></property></Properties>`, NameSpaceDocumentPropertiesVariantTypes, EXtURICustomPropertyFmtID))) |
| 179 | + _, err = f.GetCustomProps() |
| 180 | + assert.EqualError(t, err, "parsing time \"x\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"x\" as \"2006\"") |
| 181 | + |
| 182 | + // Test get custom property with unsupported value data type |
| 183 | + f.Pkg.Store(defaultXMLPathDocPropsCustom, []byte(fmt.Sprintf(`<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="%s"><property fmtid="%s" pid="2" name="Prop"><vt:cy></vt:cy></property></Properties>`, NameSpaceDocumentPropertiesVariantTypes, EXtURICustomPropertyFmtID))) |
| 184 | + props, err = f.GetCustomProps() |
| 185 | + assert.Equal(t, []CustomProperty{{Name: "Prop"}}, props) |
| 186 | + assert.NoError(t, err) |
| 187 | + assert.NoError(t, f.Close()) |
| 188 | +} |
0 commit comments