cell_test.go 1.9 KB
Newer Older
1 2
package excelize

V
Veniamin Albaev 已提交
3
import (
4
	"fmt"
V
Veniamin Albaev 已提交
5 6 7 8
	"testing"

	"github.com/stretchr/testify/assert"
)
9 10 11

func TestCheckCellInArea(t *testing.T) {
	expectedTrueCellInAreaList := [][2]string{
xurime's avatar
xurime 已提交
12 13 14
		{"c2", "A1:AAZ32"},
		{"B9", "A1:B9"},
		{"C2", "C2:C2"},
15 16 17 18 19 20
	}

	for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
		cell := expectedTrueCellInArea[0]
		area := expectedTrueCellInArea[1]

21
		assert.Truef(t, checkCellInArea(cell, area),
V
Veniamin Albaev 已提交
22
			"Expected cell %v to be in area %v, got false\n", cell, area)
23 24 25
	}

	expectedFalseCellInAreaList := [][2]string{
xurime's avatar
xurime 已提交
26 27 28
		{"c2", "A4:AAZ32"},
		{"C4", "D6:A1"}, // weird case, but you never know
		{"AEF42", "BZ40:AEF41"},
29 30 31 32 33 34
	}

	for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
		cell := expectedFalseCellInArea[0]
		area := expectedFalseCellInArea[1]

35
		assert.Falsef(t, checkCellInArea(cell, area),
V
Veniamin Albaev 已提交
36
			"Expected cell %v not to be inside of area %v, but got true\n", cell, area)
37
	}
38 39 40 41

	assert.Panics(t, func() {
		checkCellInArea("AA0", "Z0:AB1")
	})
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

func TestSetCellFloat(t *testing.T) {
	sheet := "Sheet1"
	t.Run("with no decimal", func(t *testing.T) {
		f := NewFile()
		f.SetCellFloat(sheet, "A1", 123.0, -1, 64)
		f.SetCellFloat(sheet, "A2", 123.0, 1, 64)
		assert.Equal(t, "123", f.GetCellValue(sheet, "A1"), "A1 should be 123")
		assert.Equal(t, "123.0", f.GetCellValue(sheet, "A2"), "A2 should be 123.0")
	})

	t.Run("with a decimal and precision limit", func(t *testing.T) {
		f := NewFile()
		f.SetCellFloat(sheet, "A1", 123.42, 1, 64)
		assert.Equal(t, "123.4", f.GetCellValue(sheet, "A1"), "A1 should be 123.4")
	})

	t.Run("with a decimal and no limit", func(t *testing.T) {
		f := NewFile()
		f.SetCellFloat(sheet, "A1", 123.42, -1, 64)
		assert.Equal(t, "123.42", f.GetCellValue(sheet, "A1"), "A1 should be 123.42")
	})
}

func ExampleFile_SetCellFloat() {
	f := NewFile()
69
	var x = 3.14159265
70 71 72 73
	f.SetCellFloat("Sheet1", "A1", x, 2, 64)
	fmt.Println(f.GetCellValue("Sheet1", "A1"))
	// Output: 3.14
}