diff options
| author | xuri <xuri.me@gmail.com> | 2020-05-22 16:53:46 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-22 16:53:46 +0800 |
| commit | ec14de32f0c06f7a26b6b79578f666c0cc50b72c (patch) | |
| tree | c6ebd61a7d9a7da5b993ffb82e4fb6c036e75d6a /styles_test.go | |
| parent | aa7eadbffe6ae2f9f86201bbaaa4c1d1e8829cae (diff) | |
| parent | 2efc7107ff30dc7f1e1a798082616ee488f99489 (diff) | |
Merge branch 'master' into fix/cell_lock
Diffstat (limited to 'styles_test.go')
| -rw-r--r-- | styles_test.go | 73 |
1 files changed, 70 insertions, 3 deletions
diff --git a/styles_test.go b/styles_test.go index 54321bb..1ff0e4e 100644 --- a/styles_test.go +++ b/styles_test.go @@ -1,6 +1,8 @@ package excelize import ( + "fmt" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -25,15 +27,15 @@ func TestStyleFill(t *testing.T) { xl := NewFile() styleID, err := xl.NewStyle(testCase.format) if err != nil { - t.Fatalf("%v", err) + t.Fatal(err) } styles := xl.stylesReader() style := styles.CellXfs.Xf[styleID] if testCase.expectFill { - assert.NotEqual(t, style.FillID, 0, testCase.label) + assert.NotEqual(t, *style.FillID, 0, testCase.label) } else { - assert.Equal(t, style.FillID, 0, testCase.label) + assert.Equal(t, *style.FillID, 0, testCase.label) } } } @@ -165,3 +167,68 @@ func TestSetConditionalFormat(t *testing.T) { assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label) } } + +func TestUnsetConditionalFormat(t *testing.T) { + f := NewFile() + assert.NoError(t, f.SetCellValue("Sheet1", "A1", 7)) + assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10")) + format, err := f.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`) + assert.NoError(t, err) + assert.NoError(t, f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format))) + assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10")) + // Test unset conditional format on not exists worksheet. + assert.EqualError(t, f.UnsetConditionalFormat("SheetN", "A1:A10"), "sheet SheetN is not exist") + // Save xlsx file by the given path. + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnsetConditionalFormat.xlsx"))) +} + +func TestNewStyle(t *testing.T) { + f := NewFile() + styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777"}}`) + assert.NoError(t, err) + styles := f.stylesReader() + fontID := styles.CellXfs.Xf[styleID].FontID + font := styles.Fonts.Font[*fontID] + assert.Contains(t, *font.Name.Val, "Times New Roman", "Stored font should contain font name") + assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles") + _, err = f.NewStyle(&Style{}) + assert.NoError(t, err) + _, err = f.NewStyle(Style{}) + assert.EqualError(t, err, "invalid parameter type") +} + +func TestGetDefaultFont(t *testing.T) { + f := NewFile() + s := f.GetDefaultFont() + assert.Equal(t, s, "Calibri", "Default font should be Calibri") +} + +func TestSetDefaultFont(t *testing.T) { + f := NewFile() + f.SetDefaultFont("Ariel") + styles := f.stylesReader() + s := f.GetDefaultFont() + assert.Equal(t, s, "Ariel", "Default font should change to Ariel") + assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true) +} + +func TestStylesReader(t *testing.T) { + f := NewFile() + // Test read styles with unsupport charset. + f.Styles = nil + f.XLSX["xl/styles.xml"] = MacintoshCyrillicCharset + assert.EqualValues(t, new(xlsxStyleSheet), f.stylesReader()) +} + +func TestThemeReader(t *testing.T) { + f := NewFile() + // Test read theme with unsupport charset. + f.XLSX["xl/theme/theme1.xml"] = MacintoshCyrillicCharset + assert.EqualValues(t, new(xlsxTheme), f.themeReader()) +} + +func TestSetCellStyle(t *testing.T) { + f := NewFile() + // Test set cell style on not exists worksheet. + assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN is not exist") +} |
