From e735fddd0db4a56deb4b1b02f8e1116bec45b5f4 Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 4 Nov 2024 09:40:49 +0800 Subject: [PATCH] Update unit test for SetLegacyDrawingHF function and simplify code --- vml.go | 27 +++++++++++++-------------- vml_test.go | 30 +++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/vml.go b/vml.go index a4e07e4a6e..53ef815884 100644 --- a/vml.go +++ b/vml.go @@ -1097,18 +1097,18 @@ func (f *File) SetLegacyDrawingHF(sheet string, g *HeaderFooterGraphics) error { Stroke: &xlsxStroke{JoinStyle: "miter"}, VFormulas: &vFormulas{ Formulas: []vFormula{ - vFormula{Equation: "if lineDrawn pixelLineWidth 0"}, - vFormula{Equation: "sum @0 1 0"}, - vFormula{Equation: "sum 0 0 @1"}, - vFormula{Equation: "prod @2 1 2"}, - vFormula{Equation: "prod @3 21600 pixelWidth"}, - vFormula{Equation: "prod @3 21600 pixelHeight"}, - vFormula{Equation: "sum @0 0 1"}, - vFormula{Equation: "prod @6 1 2"}, - vFormula{Equation: "prod @7 21600 pixelWidth"}, - vFormula{Equation: "sum @8 21600 0"}, - vFormula{Equation: "prod @7 21600 pixelHeight"}, - vFormula{Equation: "sum @10 21600 0"}, + {Equation: "if lineDrawn pixelLineWidth 0"}, + {Equation: "sum @0 1 0"}, + {Equation: "sum 0 0 @1"}, + {Equation: "prod @2 1 2"}, + {Equation: "prod @3 21600 pixelWidth"}, + {Equation: "prod @3 21600 pixelHeight"}, + {Equation: "sum @0 0 1"}, + {Equation: "prod @6 1 2"}, + {Equation: "prod @7 21600 pixelWidth"}, + {Equation: "sum @8 21600 0"}, + {Equation: "prod @7 21600 pixelHeight"}, + {Equation: "sum @10 21600 0"}, }, }, VPath: &vPath{ExtrusionOK: "f", GradientShapeOK: "t", ConnectType: "rect"}, @@ -1144,8 +1144,7 @@ func (f *File) SetLegacyDrawingHF(sheet string, g *HeaderFooterGraphics) error { drawingID := f.addRels(sheetRels, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "") f.addSheetNameSpace(sheet, SourceRelationship) f.addSheetLegacyDrawingHF(sheet, drawingID) - err := f.setContentTypePartImageExtensions() - if err != nil { + if err := f.setContentTypePartImageExtensions(); err != nil { return err } return f.setContentTypePartVMLExtensions() diff --git a/vml_test.go b/vml_test.go index 6b75136b1c..b79e63dafd 100644 --- a/vml_test.go +++ b/vml_test.go @@ -413,22 +413,34 @@ func TestExtractFormControl(t *testing.T) { assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8") } -func ExampleFile_SetLegacyDrawingHF() { +func TestSetLegacyDrawingHF(t *testing.T) { f := NewFile() sheet := "Sheet1" headerFooterOptions := HeaderFooterOptions{ OddHeader: "&LExcelize&R&G", } - f.SetHeaderFooter(sheet, &headerFooterOptions) - file, _ := os.ReadFile("test/images/excel.png") - f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{ + assert.NoError(t, f.SetHeaderFooter(sheet, &headerFooterOptions)) + file, err := os.ReadFile(filepath.Join("test", "images", "excel.png")) + assert.NoError(t, err) + assert.NoError(t, f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{ + Extension: ".png", + File: file, + Width: "50pt", + Height: "32pt", + })) + assert.NoError(t, f.SetCellValue(sheet, "A1", "Example")) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetLegacyDrawingHF.xlsx"))) + assert.NoError(t, f.Close()) + + // Test set legacy drawing header/footer with unsupported charset content types + f = NewFile() + f.ContentTypes = nil + f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) + assert.EqualError(t, f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{ Extension: ".png", File: file, Width: "50pt", Height: "32pt", - }) - f.SetCellValue(sheet, "A1", "Example") - out, _ := os.CreateTemp("", "header-graphics-*.xlsx") - f.Write(out) - f.Close() + }), "XML syntax error on line 1: invalid UTF-8") + assert.NoError(t, f.Close()) }