lib_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package xlsx
  2. import (
  3. "bytes"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "xml"
  9. )
  10. // Test we can correctly open a XSLX file and return a xlsx.File
  11. // struct.
  12. func TestOpenFile(t *testing.T) {
  13. var xlsxFile *File
  14. var error os.Error
  15. xlsxFile, error = OpenFile("testfile.xlsx")
  16. if error != nil {
  17. t.Error(error.String())
  18. return
  19. }
  20. if xlsxFile == nil {
  21. t.Error("OpenFile returned nil FileInterface without generating an os.Error")
  22. return
  23. }
  24. }
  25. // Test that when we open a real XLSX file we create xlsx.Sheet
  26. // objects for the sheets inside the file and that these sheets are
  27. // themselves correct.
  28. func TestCreateSheet(t *testing.T) {
  29. var xlsxFile *File
  30. var error os.Error
  31. var sheet *Sheet
  32. var row *Row
  33. xlsxFile, error = OpenFile("testfile.xlsx")
  34. if error != nil {
  35. t.Error(error.String())
  36. return
  37. }
  38. if xlsxFile == nil {
  39. t.Error("OpenFile returned a nil File pointer but did not generate an error.")
  40. return
  41. }
  42. if len(xlsxFile.Sheets) == 0 {
  43. t.Error("Expected len(xlsxFile.Sheets) > 0")
  44. return
  45. }
  46. sheet = xlsxFile.Sheets[0]
  47. if len(sheet.Rows) != 2 {
  48. t.Error("Expected len(sheet.Rows) == 2")
  49. return
  50. }
  51. row = sheet.Rows[0]
  52. if len(row.Cells) != 2 {
  53. t.Error("Expected len(row.Cells) == 2")
  54. return
  55. }
  56. cell := row.Cells[0]
  57. cellstring := cell.String()
  58. if cellstring != "Foo" {
  59. t.Error("Expected cell.String() == 'Foo', got ", cellstring)
  60. }
  61. }
  62. // Test that we can correctly extract a reference table from the
  63. // sharedStrings.xml file embedded in the XLSX file and return a
  64. // reference table of string values from it.
  65. func TestReadSharedStringsFromZipFile(t *testing.T) {
  66. var xlsxFile *File
  67. var error os.Error
  68. xlsxFile, error = OpenFile("testfile.xlsx")
  69. if error != nil {
  70. t.Error(error.String())
  71. return
  72. }
  73. if xlsxFile.referenceTable == nil {
  74. t.Error("expected non nil xlsxFile.referenceTable")
  75. return
  76. }
  77. }
  78. func TestLettersToNumeric(t *testing.T) {
  79. var input string
  80. var output int
  81. input = "A"
  82. output = lettersToNumeric(input)
  83. if output != 0 {
  84. t.Error("Expected output 'A' == 0, but got ", strconv.Itoa(output))
  85. }
  86. input = "z"
  87. output = lettersToNumeric(input)
  88. if output != 25 {
  89. t.Error("Expected output 'z' == 25, but got ", strconv.Itoa(output))
  90. }
  91. input = "AA"
  92. output = lettersToNumeric(input)
  93. if output != 26 {
  94. t.Error("Expected output 'AA' == 26, but got ", strconv.Itoa(output))
  95. }
  96. input = "Az"
  97. output = lettersToNumeric(input)
  98. if output != 51 {
  99. t.Error("Expected output 'Az' == 51, but got ", strconv.Itoa(output))
  100. }
  101. input = "BA"
  102. output = lettersToNumeric(input)
  103. if output != 52 {
  104. t.Error("Expected output 'BA' == 52, but got ", strconv.Itoa(output))
  105. }
  106. input = "Bz"
  107. output = lettersToNumeric(input)
  108. if output != 77 {
  109. t.Error("Expected output 'Bz' == 77, but got ", strconv.Itoa(output))
  110. }
  111. input = "AAA"
  112. output = lettersToNumeric(input)
  113. if output != 676 {
  114. t.Error("Expected output 'AAA' == 676, but got ", strconv.Itoa(output))
  115. }
  116. }
  117. func TestPositionalLetterMultiplier(t *testing.T) {
  118. var output int
  119. output = positionalLetterMultiplier(1, 0)
  120. if output != 1 {
  121. t.Error("Expected positionalLetterMultiplier(1, 0) == 1, got ", output)
  122. }
  123. output = positionalLetterMultiplier(2, 0)
  124. if output != 26 {
  125. t.Error("Expected positionalLetterMultiplier(2, 0) == 26, got ", output)
  126. }
  127. output = positionalLetterMultiplier(2, 1)
  128. if output != 1 {
  129. t.Error("Expected positionalLetterMultiplier(2, 1) == 1, got ", output)
  130. }
  131. output = positionalLetterMultiplier(3, 0)
  132. if output != 676 {
  133. t.Error("Expected positionalLetterMultiplier(3, 0) == 676, got ", output)
  134. }
  135. output = positionalLetterMultiplier(3, 1)
  136. if output != 26 {
  137. t.Error("Expected positionalLetterMultiplier(3, 1) == 26, got ", output)
  138. }
  139. output = positionalLetterMultiplier(3, 2)
  140. if output != 1 {
  141. t.Error("Expected positionalLetterMultiplier(3, 2) == 1, got ", output)
  142. }
  143. }
  144. func TestLetterOnlyMapFunction(t *testing.T) {
  145. var input string = "ABC123"
  146. var output string = strings.Map(letterOnlyMapF, input)
  147. if output != "ABC" {
  148. t.Error("Expected output == 'ABC' but got ", output)
  149. }
  150. input = "abc123"
  151. output = strings.Map(letterOnlyMapF, input)
  152. if output != "ABC" {
  153. t.Error("Expected output == 'ABC' but got ", output)
  154. }
  155. }
  156. func TestIntOnlyMapFunction(t *testing.T) {
  157. var input string = "ABC123"
  158. var output string = strings.Map(intOnlyMapF, input)
  159. if output != "123" {
  160. t.Error("Expected output == '123' but got ", output)
  161. }
  162. }
  163. func TestGetCoordsFromCellIDString(t *testing.T) {
  164. var cellIDString string = "A3"
  165. var x, y int
  166. var error os.Error
  167. x, y, error = getCoordsFromCellIDString(cellIDString)
  168. if error != nil {
  169. t.Error(error)
  170. }
  171. if x != 0 {
  172. t.Error("Expected x == 0, but got ", strconv.Itoa(x))
  173. }
  174. if y != 2 {
  175. t.Error("Expected y == 2, but got ", strconv.Itoa(y))
  176. }
  177. }
  178. func TestGetRangeFromString(t *testing.T) {
  179. var rangeString string
  180. var lower, upper int
  181. var error os.Error
  182. rangeString = "1:3"
  183. lower, upper, error = getRangeFromString(rangeString)
  184. if error != nil {
  185. t.Error(error)
  186. }
  187. if lower != 1 {
  188. t.Error("Expected lower bound == 1, but got ", strconv.Itoa(lower))
  189. }
  190. if upper != 3 {
  191. t.Error("Expected upper bound == 3, but got ", strconv.Itoa(upper))
  192. }
  193. }
  194. func TestMakeRowFromSpan(t *testing.T) {
  195. var rangeString string
  196. var row *Row
  197. var length int
  198. rangeString = "1:3"
  199. row = makeRowFromSpan(rangeString)
  200. length = len(row.Cells)
  201. if length != 3 {
  202. t.Error("Expected a row with 3 cells, but got ", strconv.Itoa(length))
  203. }
  204. rangeString = "5:7" // Note - we ignore lower bound!
  205. row = makeRowFromSpan(rangeString)
  206. length = len(row.Cells)
  207. if length != 7 {
  208. t.Error("Expected a row with 7 cells, but got ", strconv.Itoa(length))
  209. }
  210. rangeString = "1:1"
  211. row = makeRowFromSpan(rangeString)
  212. length = len(row.Cells)
  213. if length != 1 {
  214. t.Error("Expected a row with 1 cells, but got ", strconv.Itoa(length))
  215. }
  216. }
  217. func TestReadRowsFromSheet(t *testing.T) {
  218. var sharedstringsXML = bytes.NewBufferString(`
  219. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  220. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  221. <si>
  222. <t>Foo</t>
  223. </si>
  224. <si>
  225. <t>Bar</t>
  226. </si>
  227. <si>
  228. <t xml:space="preserve">Baz </t>
  229. </si>
  230. <si>
  231. <t>Quuk</t>
  232. </si>
  233. </sst>`)
  234. var sheetxml = bytes.NewBufferString(`
  235. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  236. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  237. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  238. <dimension ref="A1:B2"/>
  239. <sheetViews>
  240. <sheetView tabSelected="1" workbookViewId="0">
  241. <selection activeCell="C2" sqref="C2"/>
  242. </sheetView>
  243. </sheetViews>
  244. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  245. <sheetData>
  246. <row r="1" spans="1:2">
  247. <c r="A1" t="s">
  248. <v>0</v>
  249. </c>
  250. <c r="B1" t="s">
  251. <v>1</v>
  252. </c>
  253. </row>
  254. <row r="2" spans="1:2">
  255. <c r="A2" t="s">
  256. <v>2</v>
  257. </c>
  258. <c r="B2" t="s">
  259. <v>3</v>
  260. </c>
  261. </row>
  262. </sheetData>
  263. <pageMargins left="0.7" right="0.7"
  264. top="0.78740157499999996"
  265. bottom="0.78740157499999996"
  266. header="0.3"
  267. footer="0.3"/>
  268. </worksheet>`)
  269. worksheet := new(XLSXWorksheet)
  270. error := xml.Unmarshal(sheetxml, worksheet)
  271. if error != nil {
  272. t.Error(error.String())
  273. return
  274. }
  275. sst := new(XLSXSST)
  276. error = xml.Unmarshal(sharedstringsXML, sst)
  277. if error != nil {
  278. t.Error(error.String())
  279. return
  280. }
  281. reftable := MakeSharedStringRefTable(sst)
  282. rows := readRowsFromSheet(worksheet, reftable)
  283. if len(rows) != 2 {
  284. t.Error("Expected len(rows) == 2")
  285. }
  286. row := rows[0]
  287. if len(row.Cells) != 2 {
  288. t.Error("Expected len(row.Cells) == 2, got ", strconv.Itoa(len(row.Cells)))
  289. }
  290. cell1 := row.Cells[0]
  291. if cell1.String() != "Foo" {
  292. t.Error("Expected cell1.String() == 'Foo', got ", cell1.String())
  293. }
  294. cell2 := row.Cells[1]
  295. if cell2.String() != "Bar" {
  296. t.Error("Expected cell2.String() == 'Bar', got ", cell2.String())
  297. }
  298. }
  299. func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
  300. var sharedstringsXML = bytes.NewBufferString(`
  301. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  302. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5"><si><t>Bob</t></si><si><t>Alice</t></si><si><t>Sue</t></si><si><t>Yes</t></si><si><t>No</t></si></sst>`)
  303. var sheetxml = bytes.NewBufferString(`
  304. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  305. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  306. <sheetData>
  307. <row r="1" spans="1:3">
  308. <c r="A1" t="s">
  309. <v>
  310. 0
  311. </v>
  312. </c>
  313. <c r="B1" t="s">
  314. <v>
  315. 1
  316. </v>
  317. </c>
  318. <c r="C1" t="s">
  319. <v>
  320. 2
  321. </v>
  322. </c>
  323. </row>
  324. <row r="2" spans="1:3">
  325. <c r="A2" t="s">
  326. <v>
  327. 3
  328. </v>
  329. </c>
  330. <c r="B2" t="s">
  331. <v>
  332. 4
  333. </v>
  334. </c>
  335. <c r="C2" t="s">
  336. <v>
  337. 3
  338. </v>
  339. </c>
  340. </row>
  341. <row r="3" spans="1:3">
  342. <c r="A3" t="s">
  343. <v>
  344. 4
  345. </v>
  346. </c>
  347. <c r="C3" t="s">
  348. <v>
  349. 3
  350. </v>
  351. </c>
  352. </row>
  353. </sheetData>
  354. <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  355. </worksheet>
  356. `)
  357. worksheet := new(XLSXWorksheet)
  358. error := xml.Unmarshal(sheetxml, worksheet)
  359. if error != nil {
  360. t.Error(error.String())
  361. return
  362. }
  363. sst := new(XLSXSST)
  364. error = xml.Unmarshal(sharedstringsXML, sst)
  365. if error != nil {
  366. t.Error(error.String())
  367. return
  368. }
  369. reftable := MakeSharedStringRefTable(sst)
  370. rows := readRowsFromSheet(worksheet, reftable)
  371. if len(rows) != 3 {
  372. t.Error("Expected len(rows) == 3, got ", strconv.Itoa(len(rows)))
  373. }
  374. row := rows[2]
  375. if len(row.Cells) != 3 {
  376. t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
  377. }
  378. cell1 := row.Cells[0]
  379. if cell1.String() != "No" {
  380. t.Error("Expected cell1.String() == 'No', got ", cell1.String())
  381. }
  382. cell2 := row.Cells[1]
  383. if cell2.String() != "" {
  384. t.Error("Expected cell2.String() == '', got ", cell2.String())
  385. }
  386. cell3 := row.Cells[2]
  387. if cell3.String() != "Yes" {
  388. t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
  389. }
  390. }
  391. func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
  392. var row *Row
  393. var cell1, cell2, cell3, cell4 *Cell
  394. var sharedstringsXML = bytes.NewBufferString(`
  395. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  396. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
  397. var sheetxml = bytes.NewBufferString(`
  398. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  399. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
  400. `)
  401. worksheet := new(XLSXWorksheet)
  402. error := xml.Unmarshal(sheetxml, worksheet)
  403. if error != nil {
  404. t.Error(error.String())
  405. return
  406. }
  407. sst := new(XLSXSST)
  408. error = xml.Unmarshal(sharedstringsXML, sst)
  409. if error != nil {
  410. t.Error(error.String())
  411. return
  412. }
  413. reftable := MakeSharedStringRefTable(sst)
  414. rows := readRowsFromSheet(worksheet, reftable)
  415. if len(rows) != 8 {
  416. t.Error("Expected len(rows) == 8, got ", strconv.Itoa(len(rows)))
  417. }
  418. row = rows[0]
  419. if len(row.Cells) != 4 {
  420. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  421. }
  422. cell1 = row.Cells[0]
  423. if cell1.String() != "A" {
  424. t.Error("Expected cell1.String() == 'A', got ", cell1.String())
  425. }
  426. cell2 = row.Cells[1]
  427. if cell2.String() != "B" {
  428. t.Error("Expected cell2.String() == 'B', got ", cell2.String())
  429. }
  430. cell3 = row.Cells[2]
  431. if cell3.String() != "C" {
  432. t.Error("Expected cell3.String() == 'C', got ", cell3.String())
  433. }
  434. cell4 = row.Cells[3]
  435. if cell4.String() != "D" {
  436. t.Error("Expected cell4.String() == 'D', got ", cell4.String())
  437. }
  438. row = rows[1]
  439. if len(row.Cells) != 4 {
  440. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  441. }
  442. cell1 = row.Cells[0]
  443. if cell1.String() != "1" {
  444. t.Error("Expected cell1.String() == '1', got ", cell1.String())
  445. }
  446. cell2 = row.Cells[1]
  447. if cell2.String() != "" {
  448. t.Error("Expected cell2.String() == '', got ", cell2.String())
  449. }
  450. cell3 = row.Cells[2]
  451. if cell3.String() != "" {
  452. t.Error("Expected cell3.String() == '', got ", cell3.String())
  453. }
  454. cell4 = row.Cells[3]
  455. if cell4.String() != "" {
  456. t.Error("Expected cell4.String() == '', got ", cell4.String())
  457. }
  458. }