lib_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 TestReadRowsFromSheet(t *testing.T) {
  79. var sharedstringsXML = bytes.NewBufferString(`
  80. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  81. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  82. <si>
  83. <t>Foo</t>
  84. </si>
  85. <si>
  86. <t>Bar</t>
  87. </si>
  88. <si>
  89. <t xml:space="preserve">Baz </t>
  90. </si>
  91. <si>
  92. <t>Quuk</t>
  93. </si>
  94. </sst>`)
  95. var sheetxml = bytes.NewBufferString(`
  96. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  97. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  98. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  99. <dimension ref="A1:B2"/>
  100. <sheetViews>
  101. <sheetView tabSelected="1" workbookViewId="0">
  102. <selection activeCell="C2" sqref="C2"/>
  103. </sheetView>
  104. </sheetViews>
  105. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  106. <sheetData>
  107. <row r="1" spans="1:2">
  108. <c r="A1" t="s">
  109. <v>0</v>
  110. </c>
  111. <c r="B1" t="s">
  112. <v>1</v>
  113. </c>
  114. </row>
  115. <row r="2" spans="1:2">
  116. <c r="A2" t="s">
  117. <v>2</v>
  118. </c>
  119. <c r="B2" t="s">
  120. <v>3</v>
  121. </c>
  122. </row>
  123. </sheetData>
  124. <pageMargins left="0.7" right="0.7"
  125. top="0.78740157499999996"
  126. bottom="0.78740157499999996"
  127. header="0.3"
  128. footer="0.3"/>
  129. </worksheet>`)
  130. worksheet := new(XLSXWorksheet)
  131. error := xml.Unmarshal(sheetxml, worksheet)
  132. if error != nil {
  133. t.Error(error.String())
  134. return
  135. }
  136. sst := new(XLSXSST)
  137. error = xml.Unmarshal(sharedstringsXML, sst)
  138. if error != nil {
  139. t.Error(error.String())
  140. return
  141. }
  142. reftable := MakeSharedStringRefTable(sst)
  143. rows := readRowsFromSheet(worksheet, reftable)
  144. if len(rows) != 2 {
  145. t.Error("Expected len(rows) == 2")
  146. }
  147. }
  148. func TestLettersToNumeric(t *testing.T) {
  149. var input string
  150. var output int
  151. input = "A"
  152. output = lettersToNumeric(input)
  153. if output != 0 {
  154. t.Error("Expected output 'A' == 0, but got ", strconv.Itoa(output))
  155. }
  156. input = "z"
  157. output = lettersToNumeric(input)
  158. if output != 25 {
  159. t.Error("Expected output 'z' == 25, but got ", strconv.Itoa(output))
  160. }
  161. input = "AA"
  162. output = lettersToNumeric(input)
  163. if output != 26 {
  164. t.Error("Expected output 'AA' == 26, but got ", strconv.Itoa(output))
  165. }
  166. input = "Az"
  167. output = lettersToNumeric(input)
  168. if output != 51 {
  169. t.Error("Expected output 'Az' == 51, but got ", strconv.Itoa(output))
  170. }
  171. input = "BA"
  172. output = lettersToNumeric(input)
  173. if output != 52 {
  174. t.Error("Expected output 'BA' == 52, but got ", strconv.Itoa(output))
  175. }
  176. input = "Bz"
  177. output = lettersToNumeric(input)
  178. if output != 77 {
  179. t.Error("Expected output 'Bz' == 77, but got ", strconv.Itoa(output))
  180. }
  181. input = "AAA"
  182. output = lettersToNumeric(input)
  183. if output != 676 {
  184. t.Error("Expected output 'AAA' == 676, but got ", strconv.Itoa(output))
  185. }
  186. }
  187. func TestPositionalLetterMultiplier(t *testing.T) {
  188. var output int
  189. output = positionalLetterMultiplier(1, 0)
  190. if output != 1 {
  191. t.Error("Expected positionalLetterMultiplier(1, 0) == 1, got ", output)
  192. }
  193. output = positionalLetterMultiplier(2, 0)
  194. if output != 26 {
  195. t.Error("Expected positionalLetterMultiplier(2, 0) == 26, got ", output)
  196. }
  197. output = positionalLetterMultiplier(2, 1)
  198. if output != 1 {
  199. t.Error("Expected positionalLetterMultiplier(2, 1) == 1, got ", output)
  200. }
  201. output = positionalLetterMultiplier(3, 0)
  202. if output != 676 {
  203. t.Error("Expected positionalLetterMultiplier(3, 0) == 676, got ", output)
  204. }
  205. output = positionalLetterMultiplier(3, 1)
  206. if output != 26 {
  207. t.Error("Expected positionalLetterMultiplier(3, 1) == 26, got ", output)
  208. }
  209. output = positionalLetterMultiplier(3, 2)
  210. if output != 1 {
  211. t.Error("Expected positionalLetterMultiplier(3, 2) == 1, got ", output)
  212. }
  213. }
  214. func TestLetterOnlyMapFunction(t *testing.T) {
  215. var input string = "ABC123"
  216. var output string = strings.Map(letterOnlyMapF, input)
  217. if output != "ABC" {
  218. t.Error("Expected output == 'ABC' but got ", output)
  219. }
  220. input = "abc123"
  221. output = strings.Map(letterOnlyMapF, input)
  222. if output != "ABC" {
  223. t.Error("Expected output == 'ABC' but got ", output)
  224. }
  225. }
  226. func TestIntOnlyMapFunction(t *testing.T) {
  227. var input string = "ABC123"
  228. var output string = strings.Map(intOnlyMapF, input)
  229. if output != "123" {
  230. t.Error("Expected output == '123' but got ", output)
  231. }
  232. }
  233. func TestGetCoordsFromCellIDString(t *testing.T) {
  234. var cellIDString string = "A3"
  235. var x, y int
  236. var error os.Error
  237. x, y, error = getCoordsFromCellIDString(cellIDString)
  238. if error != nil {
  239. t.Error(error)
  240. }
  241. if x != 0 {
  242. t.Error("Expected x == 0, but got ", strconv.Itoa(x))
  243. }
  244. if y != 2 {
  245. t.Error("Expected y == 2, but got ", strconv.Itoa(y))
  246. }
  247. }
  248. func TestGetRangeFromString(t *testing.T) {
  249. var rangeString string
  250. var lower, upper int
  251. var error os.Error
  252. rangeString = "1:3"
  253. lower, upper, error = getRangeFromString(rangeString)
  254. if error != nil {
  255. t.Error(error)
  256. }
  257. if lower != 1 {
  258. t.Error("Expected lower bound == 1, but got ", strconv.Itoa(lower))
  259. }
  260. if upper != 3 {
  261. t.Error("Expected upper bound == 3, but got ", strconv.Itoa(upper))
  262. }
  263. }
  264. // func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
  265. // var sharedstringsXML = bytes.NewBufferString(`
  266. // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  267. // <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>`)
  268. // var sheetxml = bytes.NewBufferString(`
  269. // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  270. // <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"/>
  271. // <sheetData>
  272. // <row r="1" spans="1:3">
  273. // <c r="A1" t="s">
  274. // <v>
  275. // 0
  276. // </v>
  277. // </c>
  278. // <c r="B1" t="s">
  279. // <v>
  280. // 1
  281. // </v>
  282. // </c>
  283. // <c r="C1" t="s">
  284. // <v>
  285. // 2
  286. // </v>
  287. // </c>
  288. // </row>
  289. // <row r="2" spans="1:3">
  290. // <c r="A2" t="s">
  291. // <v>
  292. // 3
  293. // </v>
  294. // </c>
  295. // <c r="B2" t="s">
  296. // <v>
  297. // 4
  298. // </v>
  299. // </c>
  300. // <c r="C2" t="s">
  301. // <v>
  302. // 3
  303. // </v>
  304. // </c>
  305. // </row>
  306. // <row r="3" spans="1:3">
  307. // <c r="A3" t="s">
  308. // <v>
  309. // 4
  310. // </v>
  311. // </c>
  312. // <c r="C3" t="s">
  313. // <v>
  314. // 3
  315. // </v>
  316. // </c>
  317. // </row>
  318. // </sheetData>
  319. // <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  320. // </worksheet>
  321. // `)
  322. // worksheet := new(XLSXWorksheet)
  323. // error := xml.Unmarshal(sheetxml, worksheet)
  324. // if error != nil {
  325. // t.Error(error.String())
  326. // return
  327. // }
  328. // sst := new(XLSXSST)
  329. // error = xml.Unmarshal(sharedstringsXML, sst)
  330. // if error != nil {
  331. // t.Error(error.String())
  332. // return
  333. // }
  334. // reftable := MakeSharedStringRefTable(sst)
  335. // rows := readRowsFromSheet(worksheet, reftable)
  336. // if len(rows) != 3 {
  337. // t.Error("Expected len(rows) == 3")
  338. // }
  339. // }