lib_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 TestReadRowsFromSheet(t *testing.T) {
  195. var sharedstringsXML = bytes.NewBufferString(`
  196. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  197. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  198. <si>
  199. <t>Foo</t>
  200. </si>
  201. <si>
  202. <t>Bar</t>
  203. </si>
  204. <si>
  205. <t xml:space="preserve">Baz </t>
  206. </si>
  207. <si>
  208. <t>Quuk</t>
  209. </si>
  210. </sst>`)
  211. var sheetxml = bytes.NewBufferString(`
  212. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  213. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  214. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  215. <dimension ref="A1:B2"/>
  216. <sheetViews>
  217. <sheetView tabSelected="1" workbookViewId="0">
  218. <selection activeCell="C2" sqref="C2"/>
  219. </sheetView>
  220. </sheetViews>
  221. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  222. <sheetData>
  223. <row r="1" spans="1:2">
  224. <c r="A1" t="s">
  225. <v>0</v>
  226. </c>
  227. <c r="B1" t="s">
  228. <v>1</v>
  229. </c>
  230. </row>
  231. <row r="2" spans="1:2">
  232. <c r="A2" t="s">
  233. <v>2</v>
  234. </c>
  235. <c r="B2" t="s">
  236. <v>3</v>
  237. </c>
  238. </row>
  239. </sheetData>
  240. <pageMargins left="0.7" right="0.7"
  241. top="0.78740157499999996"
  242. bottom="0.78740157499999996"
  243. header="0.3"
  244. footer="0.3"/>
  245. </worksheet>`)
  246. worksheet := new(XLSXWorksheet)
  247. error := xml.Unmarshal(sheetxml, worksheet)
  248. if error != nil {
  249. t.Error(error.String())
  250. return
  251. }
  252. sst := new(XLSXSST)
  253. error = xml.Unmarshal(sharedstringsXML, sst)
  254. if error != nil {
  255. t.Error(error.String())
  256. return
  257. }
  258. reftable := MakeSharedStringRefTable(sst)
  259. rows := readRowsFromSheet(worksheet, reftable)
  260. if len(rows) != 2 {
  261. t.Error("Expected len(rows) == 2")
  262. }
  263. row := rows[0]
  264. if len(row.Cells) != 2 {
  265. t.Error("Expected len(row.Cells) == 2, got ", strconv.Itoa(len(row.Cells)))
  266. }
  267. cell1 := row.Cells[0]
  268. if cell1.String() != "Foo" {
  269. t.Error("Expected cell1.String() == 'Foo', got ", cell1.String())
  270. }
  271. cell2 := row.Cells[1]
  272. if cell2.String() != "Bar" {
  273. t.Error("Expected cell2.String() == 'Bar', got ", cell2.String())
  274. }
  275. }
  276. func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
  277. var sharedstringsXML = bytes.NewBufferString(`
  278. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  279. <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>`)
  280. var sheetxml = bytes.NewBufferString(`
  281. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  282. <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"/>
  283. <sheetData>
  284. <row r="1" spans="1:3">
  285. <c r="A1" t="s">
  286. <v>
  287. 0
  288. </v>
  289. </c>
  290. <c r="B1" t="s">
  291. <v>
  292. 1
  293. </v>
  294. </c>
  295. <c r="C1" t="s">
  296. <v>
  297. 2
  298. </v>
  299. </c>
  300. </row>
  301. <row r="2" spans="1:3">
  302. <c r="A2" t="s">
  303. <v>
  304. 3
  305. </v>
  306. </c>
  307. <c r="B2" t="s">
  308. <v>
  309. 4
  310. </v>
  311. </c>
  312. <c r="C2" t="s">
  313. <v>
  314. 3
  315. </v>
  316. </c>
  317. </row>
  318. <row r="3" spans="1:3">
  319. <c r="A3" t="s">
  320. <v>
  321. 4
  322. </v>
  323. </c>
  324. <c r="C3" t="s">
  325. <v>
  326. 3
  327. </v>
  328. </c>
  329. </row>
  330. </sheetData>
  331. <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  332. </worksheet>
  333. `)
  334. worksheet := new(XLSXWorksheet)
  335. error := xml.Unmarshal(sheetxml, worksheet)
  336. if error != nil {
  337. t.Error(error.String())
  338. return
  339. }
  340. sst := new(XLSXSST)
  341. error = xml.Unmarshal(sharedstringsXML, sst)
  342. if error != nil {
  343. t.Error(error.String())
  344. return
  345. }
  346. reftable := MakeSharedStringRefTable(sst)
  347. rows := readRowsFromSheet(worksheet, reftable)
  348. if len(rows) != 3 {
  349. t.Error("Expected len(rows) == 3, got ", strconv.Itoa(len(rows)))
  350. }
  351. row := rows[2]
  352. if len(row.Cells) != 3 {
  353. t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
  354. }
  355. cell1 := row.Cells[0]
  356. if cell1.String() != "No" {
  357. t.Error("Expected cell1.String() == 'No', got ", cell1.String())
  358. }
  359. cell2 := row.Cells[1]
  360. if cell2.String() != "" {
  361. t.Error("Expected cell2.String() == '', got ", cell2.String())
  362. }
  363. cell3 := row.Cells[2]
  364. if cell3.String() != "Yes" {
  365. t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
  366. }
  367. }
  368. func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
  369. var row *Row
  370. var cell1, cell2, cell3, cell4 *Cell
  371. var sharedstringsXML = bytes.NewBufferString(`
  372. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  373. <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>`)
  374. var sheetxml = bytes.NewBufferString(`
  375. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  376. <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>
  377. `)
  378. worksheet := new(XLSXWorksheet)
  379. error := xml.Unmarshal(sheetxml, worksheet)
  380. if error != nil {
  381. t.Error(error.String())
  382. return
  383. }
  384. sst := new(XLSXSST)
  385. error = xml.Unmarshal(sharedstringsXML, sst)
  386. if error != nil {
  387. t.Error(error.String())
  388. return
  389. }
  390. reftable := MakeSharedStringRefTable(sst)
  391. rows := readRowsFromSheet(worksheet, reftable)
  392. if len(rows) != 8 {
  393. t.Error("Expected len(rows) == 8, got ", strconv.Itoa(len(rows)))
  394. }
  395. row = rows[0]
  396. if len(row.Cells) != 4 {
  397. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  398. }
  399. cell1 = row.Cells[0]
  400. if cell1.String() != "A" {
  401. t.Error("Expected cell1.String() == 'A', got ", cell1.String())
  402. }
  403. cell2 = row.Cells[1]
  404. if cell2.String() != "B" {
  405. t.Error("Expected cell2.String() == 'B', got ", cell2.String())
  406. }
  407. cell3 = row.Cells[2]
  408. if cell3.String() != "C" {
  409. t.Error("Expected cell3.String() == 'C', got ", cell3.String())
  410. }
  411. cell4 = row.Cells[3]
  412. if cell4.String() != "D" {
  413. t.Error("Expected cell4.String() == 'D', got ", cell4.String())
  414. }
  415. row = rows[1]
  416. if len(row.Cells) != 4 {
  417. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  418. }
  419. cell1 = row.Cells[0]
  420. if cell1.String() != "1" {
  421. t.Error("Expected cell1.String() == '1', got ", cell1.String())
  422. }
  423. cell2 = row.Cells[1]
  424. if cell2.String() != "" {
  425. t.Error("Expected cell2.String() == '', got ", cell2.String())
  426. }
  427. cell3 = row.Cells[2]
  428. if cell3.String() != "" {
  429. t.Error("Expected cell3.String() == '', got ", cell3.String())
  430. }
  431. cell4 = row.Cells[3]
  432. if cell4.String() != "" {
  433. t.Error("Expected cell4.String() == '', got ", cell4.String())
  434. }
  435. }