Skip to content

Commit

Permalink
Unit test for UpdateLinkedValue function with unsupported charset
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Jun 28, 2024
1 parent 2504deb commit cf27ddf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3197,9 +3197,8 @@ func SetCellValue(f *excelize.File) func(this js.Value, args []js.Value) interfa
value = args[2].Bool()
case js.TypeNumber:
value = args[2].Float()
case js.TypeString:
default: // js.TypeString:
value = args[2].String()
default:
}
if err := f.SetCellValue(args[0].String(), args[1].String(), value); err != nil {
ret["error"] = err.Error()
Expand Down
18 changes: 18 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/xuri/excelize/v2"
)

var MacintoshCyrillicCharset = []byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}

func TestRegInteropFunc(t *testing.T) {
js.Global().Set("excelize", map[string]interface{}{})
regFuncs()
Expand Down Expand Up @@ -2571,6 +2573,22 @@ func TestUpdateLinkedValue(t *testing.T) {

ret = f.(js.Value).Call("UpdateLinkedValue", js.ValueOf("Sheet1"))
assert.EqualError(t, errArgNum, ret.Get("error").String())

// Test unsupported charset
wb := excelize.NewFile()
wb.Sheet.Delete("xl/worksheets/sheet1.xml")
wb.Pkg.Store("xl/worksheets/sheet1.xml", MacintoshCyrillicCharset)
buf, err := wb.WriteToBuffer()
assert.NoError(t, err)

uint8Array := js.Global().Get("Uint8Array").New(js.ValueOf(buf.Len()))
for k, v := range buf.Bytes() {
uint8Array.SetIndex(k, v)
}
f = OpenReader(js.Value{}, []js.Value{uint8Array})
assert.True(t, f.(js.Value).Get("error").IsNull())
ret = f.(js.Value).Call("UpdateLinkedValue")
assert.Equal(t, ret.Get("error").String(), "XML syntax error on line 1: invalid UTF-8")
}

func TestWriteToBuffer(t *testing.T) {
Expand Down

0 comments on commit cf27ddf

Please sign in to comment.