cell_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package excelize
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestCheckCellInArea(t *testing.T) {
  8. expectedTrueCellInAreaList := [][2]string{
  9. {"c2", "A1:AAZ32"},
  10. {"B9", "A1:B9"},
  11. {"C2", "C2:C2"},
  12. }
  13. for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
  14. cell := expectedTrueCellInArea[0]
  15. area := expectedTrueCellInArea[1]
  16. assert.Truef(t, checkCellInArea(cell, area),
  17. "Expected cell %v to be in area %v, got false\n", cell, area)
  18. }
  19. expectedFalseCellInAreaList := [][2]string{
  20. {"c2", "A4:AAZ32"},
  21. {"C4", "D6:A1"}, // weird case, but you never know
  22. {"AEF42", "BZ40:AEF41"},
  23. }
  24. for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
  25. cell := expectedFalseCellInArea[0]
  26. area := expectedFalseCellInArea[1]
  27. assert.Falsef(t, checkCellInArea(cell, area),
  28. "Expected cell %v not to be inside of area %v, but got true\n", cell, area)
  29. }
  30. assert.Panics(t, func() {
  31. checkCellInArea("AA0", "Z0:AB1")
  32. })
  33. }
  34. func TestSetCellFloat(t *testing.T) {
  35. sheet := "Sheet1"
  36. t.Run("with no decimal", func(t *testing.T) {
  37. f := NewFile()
  38. f.SetCellFloat(sheet, "A1", 123.0, -1, 64)
  39. f.SetCellFloat(sheet, "A2", 123.0, 1, 64)
  40. assert.Equal(t, "123", f.GetCellValue(sheet, "A1"), "A1 should be 123")
  41. assert.Equal(t, "123.0", f.GetCellValue(sheet, "A2"), "A2 should be 123.0")
  42. })
  43. t.Run("with a decimal and precision limit", func(t *testing.T) {
  44. f := NewFile()
  45. f.SetCellFloat(sheet, "A1", 123.42, 1, 64)
  46. assert.Equal(t, "123.4", f.GetCellValue(sheet, "A1"), "A1 should be 123.4")
  47. })
  48. t.Run("with a decimal and no limit", func(t *testing.T) {
  49. f := NewFile()
  50. f.SetCellFloat(sheet, "A1", 123.42, -1, 64)
  51. assert.Equal(t, "123.42", f.GetCellValue(sheet, "A1"), "A1 should be 123.42")
  52. })
  53. }
  54. func ExampleFile_SetCellFloat() {
  55. f := NewFile()
  56. var x float64 = 3.14159265
  57. f.SetCellFloat("Sheet1", "A1", x, 2, 64)
  58. fmt.Println(f.GetCellValue("Sheet1", "A1"))
  59. // Output: 3.14
  60. }