-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_test.go
More file actions
293 lines (252 loc) · 8.14 KB
/
export_test.go
File metadata and controls
293 lines (252 loc) · 8.14 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package wordcounter_test
import (
"os"
"strings"
"testing"
wcg "github.com/100gle/wordcounter"
)
func TestExportToCSV(t *testing.T) {
// Test CSV export through FileCounter interface
// Create a test file
testContent := "Hello 世界"
testFile := "testdata/export_test.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
// Create FileCounter and test CSV export
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
result, err := fc.ExportCSV()
if err != nil {
t.Errorf("ExportCSV failed with error: %v", err)
}
// Check if result contains expected headers
if !strings.Contains(result, "File,Lines,ChineseChars,NonChineseChars,TotalChars") {
t.Errorf("ExportCSV result doesn't contain expected headers: %v", result)
}
}
func TestExportToCSVWithFilename(t *testing.T) {
// Test CSV export with filename through FileCounter interface
testContent := "Hello 世界"
testFile := "testdata/export_test2.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
filename := "test.csv"
csvData, err := fc.ExportCSV(filename)
if err != nil {
t.Errorf("ExportCSV failed with error: %v", err)
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
t.Errorf("ExportCSV did not create file: %v", err)
}
// Check if result contains expected headers
if !strings.Contains(csvData, "File,Lines,ChineseChars,NonChineseChars,TotalChars") {
t.Errorf("ExportCSV result doesn't contain expected headers: %v", csvData)
}
err = os.Remove(filename)
if err != nil {
t.Errorf("ExportCSV could not delete file: %v", err)
}
}
func TestExportToExcel(t *testing.T) {
// Test Excel export through FileCounter interface
testContent := "Hello 世界"
testFile := "testdata/export_test3.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
filename := "test.xlsx"
err = fc.ExportExcel(filename)
if err != nil {
t.Errorf("ExportExcel failed with error: %v", err)
}
// Check if file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
t.Errorf("ExportExcel did not create file: %v", err)
}
// Clean up file
err = os.Remove(filename)
if err != nil {
t.Errorf("ExportExcel could not delete file: %v", err)
}
}
func TestExportToTable(t *testing.T) {
// Test Table export through FileCounter interface
testContent := "Hello 世界"
testFile := "testdata/export_test4.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
result := fc.ExportTable()
// Check if result contains expected headers and data
if !strings.Contains(result, "FILE") || !strings.Contains(result, "LINES") {
t.Errorf("ExportTable result doesn't contain expected content: %v", result)
}
}
func TestExportCounterFunctions(t *testing.T) {
// Test the standalone export functions
testContent := "Hello 世界\nSecond line"
testFile := "testdata/export_counter_test.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
// Test ExportCounterCSV
csvResult, err := wcg.ExportCounterCSV(fc)
if err != nil {
t.Errorf("ExportCounterCSV failed: %v", err)
}
if !strings.Contains(csvResult, "File,Lines,ChineseChars,NonChineseChars,TotalChars") {
t.Errorf("ExportCounterCSV result doesn't contain expected headers")
}
// Test ExportCounterCSV with filename
csvFile := "test_counter.csv"
_, err = wcg.ExportCounterCSV(fc, csvFile)
if err != nil {
t.Errorf("ExportCounterCSV with filename failed: %v", err)
}
if _, err := os.Stat(csvFile); os.IsNotExist(err) {
t.Errorf("ExportCounterCSV did not create file")
}
defer os.Remove(csvFile)
// Test ExportCounterExcel
excelFile := "test_counter.xlsx"
err = wcg.ExportCounterExcel(fc, excelFile)
if err != nil {
t.Errorf("ExportCounterExcel failed: %v", err)
}
if _, err := os.Stat(excelFile); os.IsNotExist(err) {
t.Errorf("ExportCounterExcel did not create file")
}
defer os.Remove(excelFile)
// Test ExportCounterTable
tableResult := wcg.ExportCounterTable(fc)
if !strings.Contains(tableResult, "FILE") || !strings.Contains(tableResult, "LINES") {
t.Errorf("ExportCounterTable result doesn't contain expected content")
}
}
func TestExportErrorHandling(t *testing.T) {
// Test export to invalid path for Excel
testContent := "Hello world"
testFile := "testdata/export_error_test.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
// Test Excel export to invalid directory
invalidPath := "/invalid/directory/test.xlsx"
err = wcg.ExportCounterExcel(fc, invalidPath)
if err == nil {
t.Errorf("Expected error when exporting to invalid path, but got none")
}
// Test CSV export to invalid directory
invalidCSVPath := "/invalid/directory/test.csv"
_, err = wcg.ExportCounterCSV(fc, invalidCSVPath)
if err == nil {
t.Errorf("Expected error when exporting CSV to invalid path, but got none")
}
}
// TestExportWithoutFilename tests export functions without providing filename
func TestExportWithoutFilename(t *testing.T) {
testContent := "Hello 世界"
testFile := "testdata/export_no_filename_test.txt"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
fc := wcg.NewFileCounter(testFile)
err = fc.Count()
if err != nil {
t.Fatalf("Failed to count: %v", err)
}
// Test CSV export without filename (should return CSV string only)
csvResult, err := wcg.ExportCounterCSV(fc)
if err != nil {
t.Errorf("ExportCounterCSV without filename failed: %v", err)
}
if csvResult == "" {
t.Errorf("ExportCounterCSV without filename should return CSV string")
}
// Test Excel export without filename (should use default filename)
err = wcg.ExportCounterExcel(fc)
if err != nil {
t.Errorf("ExportCounterExcel without filename failed: %v", err)
}
// Clean up default file
defer os.Remove("counter.xlsx")
// Test Table export (always returns string, no file)
tableResult := wcg.ExportCounterTable(fc)
if tableResult == "" {
t.Errorf("ExportCounterTable should return table string")
}
}
// TestExportEmptyData tests export functions with empty data
func TestExportEmptyData(t *testing.T) {
// Create a mock counter with no data
// We'll use a DirCounter with no files to test empty data export
dc := wcg.NewDirCounter("/nonexistent/directory")
// Test CSV export with empty data - should still work but return headers only
csvResult, err := wcg.ExportCounterCSV(dc)
if err != nil {
t.Errorf("CSV export with empty data should not fail: %v", err)
}
if csvResult == "" {
t.Errorf("CSV export should return at least headers")
}
// Test Excel export with empty data - should still work
err = wcg.ExportCounterExcel(dc)
if err != nil {
t.Errorf("Excel export with empty data should not fail: %v", err)
}
// Clean up default file
defer os.Remove("counter.xlsx")
// Test Table export with empty data - should return table with headers only
tableResult := wcg.ExportCounterTable(dc)
if tableResult == "" {
t.Errorf("Table export should return at least headers")
}
// Should contain table headers
if !strings.Contains(tableResult, "FILE") {
t.Errorf("Table export should contain FILE header")
}
}