sheet_test.go 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
package excelize_test

import (
	"fmt"
	"testing"

	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mohae/deepcopy"
	"github.com/stretchr/testify/assert"
)

func ExampleFile_SetPageLayout() {
	xl := excelize.NewFile()
	const sheet = "Sheet1"

	if err := xl.SetPageLayout(
		"Sheet1",
		excelize.PageLayoutOrientation(excelize.OrientationLandscape),
	); err != nil {
		panic(err)
	}
	// Output:
}

func ExampleFile_GetPageLayout() {
	xl := excelize.NewFile()
	const sheet = "Sheet1"
	var (
		orientation excelize.PageLayoutOrientation
	)
	if err := xl.GetPageLayout("Sheet1", &orientation); err != nil {
		panic(err)
	}
	fmt.Println("Defaults:")
	fmt.Printf("- orientation: %q\n", orientation)
	// Output:
	// Defaults:
	// - orientation: "portrait"
}

func TestPageLayoutOption(t *testing.T) {
	const sheet = "Sheet1"

	testData := []struct {
		container  excelize.PageLayoutOptionPtr
		nonDefault excelize.PageLayoutOption
	}{
		{new(excelize.PageLayoutOrientation), excelize.PageLayoutOrientation(excelize.OrientationLandscape)},
	}

	for i, test := range testData {
		t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {

			opt := test.nonDefault
			t.Logf("option %T", opt)

			def := deepcopy.Copy(test.container).(excelize.PageLayoutOptionPtr)
			val1 := deepcopy.Copy(def).(excelize.PageLayoutOptionPtr)
			val2 := deepcopy.Copy(def).(excelize.PageLayoutOptionPtr)

			xl := excelize.NewFile()
			// Get the default value
			if !assert.NoError(t, xl.GetPageLayout(sheet, def), opt) {
				t.FailNow()
			}
			// Get again and check
			if !assert.NoError(t, xl.GetPageLayout(sheet, val1), opt) {
				t.FailNow()
			}
			if !assert.Equal(t, val1, def, opt) {
				t.FailNow()
			}
			// Set the same value
			if !assert.NoError(t, xl.SetPageLayout(sheet, val1), opt) {
				t.FailNow()
			}
			// Get again and check
			if !assert.NoError(t, xl.GetPageLayout(sheet, val1), opt) {
				t.FailNow()
			}
			if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
				t.FailNow()
			}
			// Set a different value
			if !assert.NoError(t, xl.SetPageLayout(sheet, test.nonDefault), opt) {
				t.FailNow()
			}
			if !assert.NoError(t, xl.GetPageLayout(sheet, val1), opt) {
				t.FailNow()
			}
			// Get again and compare
			if !assert.NoError(t, xl.GetPageLayout(sheet, val2), opt) {
				t.FailNow()
			}
			if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
				t.FailNow()
			}
			// Value should not be the same as the default
			if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
				t.FailNow()
			}
			// Restore the default value
			if !assert.NoError(t, xl.SetPageLayout(sheet, def), opt) {
				t.FailNow()
			}
			if !assert.NoError(t, xl.GetPageLayout(sheet, val1), opt) {
				t.FailNow()
			}
			if !assert.Equal(t, def, val1) {
				t.FailNow()
			}
		})
	}
}