-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalue_test.go
More file actions
173 lines (164 loc) · 4.96 KB
/
value_test.go
File metadata and controls
173 lines (164 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package werkbook
import (
"math"
"testing"
"time"
"github.com/jpoz/werkbook/formula"
"github.com/jpoz/werkbook/ooxml"
)
func TestToValue(t *testing.T) {
tests := []struct {
name string
input any
want Value
wantErr bool
}{
{"nil", nil, Value{Type: TypeEmpty}, false},
{"string", "hello", Value{Type: TypeString, String: "hello"}, false},
{"empty string", "", Value{Type: TypeString, String: ""}, false},
{"bool true", true, Value{Type: TypeBool, Bool: true}, false},
{"bool false", false, Value{Type: TypeBool, Bool: false}, false},
{"int", 42, Value{Type: TypeNumber, Number: 42}, false},
{"int64", int64(100), Value{Type: TypeNumber, Number: 100}, false},
{"float64", 3.14, Value{Type: TypeNumber, Number: 3.14}, false},
{"float32", float32(1.5), Value{Type: TypeNumber, Number: 1.5}, false},
{"uint", uint(7), Value{Type: TypeNumber, Number: 7}, false},
{"NaN", math.NaN(), Value{}, true},
{"Inf", math.Inf(1), Value{}, true},
{"unsupported", []int{1}, Value{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := toValue(tt.input)
if (err != nil) != tt.wantErr {
t.Fatalf("toValue(%v) err = %v, wantErr %v", tt.input, err, tt.wantErr)
}
if !tt.wantErr && got != tt.want {
t.Errorf("toValue(%v) = %#v, want %#v", tt.input, got, tt.want)
}
})
}
}
func TestCellDataToValue_SharedStringNumeric(t *testing.T) {
// Styles slice: index 0 = default, index 1 = text format (@).
textStyle := &Style{NumFmtID: 49}
styles := []*Style{nil, textStyle}
tests := []struct {
name string
cd ooxml.CellData
styles []*Style
wantType ValueType
wantNum float64
wantStr string
}{
{
name: "shared string numeric stays string",
cd: ooxml.CellData{Type: "s", Value: "-8086931554011838357"},
wantType: TypeString,
wantStr: "-8086931554011838357",
},
{
name: "shared string positive integer stays string",
cd: ooxml.CellData{Type: "s", Value: "42"},
wantType: TypeString,
wantStr: "42",
},
{
name: "shared string float stays string",
cd: ooxml.CellData{Type: "s", Value: "3.14"},
wantType: TypeString,
wantStr: "3.14",
},
{
name: "shared string with non-numeric text",
cd: ooxml.CellData{Type: "s", Value: "hello"},
wantType: TypeString,
wantStr: "hello",
},
{
name: "shared string numeric with text format stays string",
cd: ooxml.CellData{Type: "s", Value: "42", StyleIdx: 1},
styles: styles,
wantType: TypeString,
wantStr: "42",
},
{
name: "shared string float with text format stays string",
cd: ooxml.CellData{Type: "s", Value: "3.14", StyleIdx: 1},
styles: styles,
wantType: TypeString,
wantStr: "3.14",
},
{
name: "str type stays string even if numeric",
cd: ooxml.CellData{Type: "str", Value: "42"},
wantType: TypeString,
wantStr: "42",
},
{
name: "inlineStr type stays string even if numeric",
cd: ooxml.CellData{Type: "inlineStr", Value: "100"},
wantType: TypeString,
wantStr: "100",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cellDataToValue(tt.cd, tt.styles, false)
if got.Type != tt.wantType {
t.Fatalf("cellDataToValue(%+v).Type = %v, want %v", tt.cd, got.Type, tt.wantType)
}
if tt.wantType == TypeNumber && got.Number != tt.wantNum {
t.Errorf("cellDataToValue(%+v).Number = %v, want %v", tt.cd, got.Number, tt.wantNum)
}
if tt.wantType == TypeString && got.String != tt.wantStr {
t.Errorf("cellDataToValue(%+v).String = %q, want %q", tt.cd, got.String, tt.wantStr)
}
})
}
}
func TestCellDataToValue_DateCell(t *testing.T) {
tests := []struct {
name string
cd ooxml.CellData
date1904 bool
want float64
}{
{
name: "1900 date system",
cd: ooxml.CellData{Type: "d", Value: "2024-06-15"},
date1904: false,
want: timeToSerial(time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC)),
},
{
name: "1904 date system",
cd: ooxml.CellData{Type: "d", Value: "1904-01-01T00:00:00Z"},
date1904: true,
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cellDataToValue(tt.cd, nil, tt.date1904)
if got.Type != TypeNumber {
t.Fatalf("cellDataToValue(%+v).Type = %v, want %v", tt.cd, got.Type, TypeNumber)
}
if got.Number != tt.want {
t.Fatalf("cellDataToValue(%+v).Number = %v, want %v", tt.cd, got.Number, tt.want)
}
})
}
}
func TestErrorCodeCompatibility(t *testing.T) {
if got := ErrorCodeFromString("#SPILL!"); got != ErrorCode(formula.ErrValSPILL) {
t.Fatalf("ErrorCodeFromString(#SPILL!) = %v, want %v", got, formula.ErrValSPILL)
}
v := Value{Type: TypeError, String: "#REF!"}
code, ok := v.ErrorCode()
if !ok {
t.Fatal("Value.ErrorCode() reported non-error for TypeError value")
}
if code != ErrorCode(formula.ErrValREF) {
t.Fatalf("Value.ErrorCode() = %v, want %v", code, formula.ErrValREF)
}
}