@ -2,11 +2,12 @@ package excelize_test
import (
import (
"fmt"
"fmt"
"reflect"
"testing"
"testing"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/mohae/deepcopy"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
"github.com/360EntSecGroup-Skylar/excelize"
)
)
var _ = [ ] excelize . SheetPrOption {
var _ = [ ] excelize . SheetPrOption {
@ -86,7 +87,8 @@ func ExampleFile_GetSheetPrOptions() {
func TestSheetPrOptions ( t * testing . T ) {
func TestSheetPrOptions ( t * testing . T ) {
const sheet = "Sheet1"
const sheet = "Sheet1"
for _ , test := range [ ] struct {
testData := [ ] struct {
container excelize . SheetPrOptionPtr
container excelize . SheetPrOptionPtr
nonDefault excelize . SheetPrOption
nonDefault excelize . SheetPrOption
} {
} {
@ -96,66 +98,69 @@ func TestSheetPrOptions(t *testing.T) {
{ new ( excelize . FitToPage ) , excelize . FitToPage ( true ) } ,
{ new ( excelize . FitToPage ) , excelize . FitToPage ( true ) } ,
{ new ( excelize . AutoPageBreaks ) , excelize . AutoPageBreaks ( true ) } ,
{ new ( excelize . AutoPageBreaks ) , excelize . AutoPageBreaks ( true ) } ,
{ new ( excelize . OutlineSummaryBelow ) , excelize . OutlineSummaryBelow ( false ) } ,
{ new ( excelize . OutlineSummaryBelow ) , excelize . OutlineSummaryBelow ( false ) } ,
} {
}
opt := test . nonDefault
t . Logf ( "option %T" , opt )
for i , test := range testData {
t . Run ( fmt . Sprintf ( "TestData%d" , i ) , func ( t * testing . T ) {
def := deepcopy . Copy ( test . container ) . ( excelize . SheetPrOptionPtr )
val1 := deepcopy . Copy ( def ) . ( excelize . SheetPrOptionPtr )
opt := test . nonDefault
val2 := deepcopy . Copy ( def ) . ( excelize . SheetPrOptionPtr )
t . Logf ( "option %T" , opt )
xl := excelize . NewFile ( )
def := deepcopy . Copy ( test . container ) . ( excelize . SheetPrOptionPtr )
// Get the default value
val1 := deepcopy . Copy ( def ) . ( excelize . SheetPrOptionPtr )
if err := xl . GetSheetPrOptions ( sheet , def ) ; err != nil {
val2 := deepcopy . Copy ( def ) . ( excelize . SheetPrOptionPtr )
t . Fatalf ( "%T: %s" , opt , err )
}
xl := excelize . NewFile ( )
// Get again and check
// Get the default value
if err := xl . GetSheetPrOptions ( sheet , val1 ) ; err != nil {
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , def ) , opt ) {
t . Fatalf ( "%T: %s" , opt , err )
t . FailNow ( )
}
}
if ! reflect . DeepEqual ( val1 , def ) {
// Get again and check
t . Fatalf ( "%T: value should not have changed" , opt )
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , val1 ) , opt ) {
}
t . FailNow ( )
// Set the same value
}
if err := xl . SetSheetPrOptions ( sheet , val1 ) ; err != nil {
if ! assert . Equal ( t , val1 , def , opt ) {
t . Fatalf ( "%T: %s" , opt , err )
t . FailNow ( )
}
}
// Get again and check
// Set the same value
if err := xl . GetSheetPrOptions ( sheet , val1 ) ; err != nil {
if ! assert . NoError ( t , xl . SetSheetPrOptions ( sheet , val1 ) , opt ) {
t . Fatalf ( "%T: %s" , opt , err )
t . FailNow ( )
}
}
if ! reflect . DeepEqual ( val1 , def ) {
// Get again and check
t . Fatalf ( "%T: value should not have changed" , opt )
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , val1 ) , opt ) {
}
t . FailNow ( )
}
// Set a different value
if ! assert . Equal ( t , val1 , def , "%T: value should not have changed" , opt ) {
if err := xl . SetSheetPrOptions ( sheet , test . nonDefault ) ; err != nil {
t . FailNow ( )
t . Fatalf ( "%T: %s" , opt , err )
}
}
// Set a different value
if err := xl . GetSheetPrOptions ( sheet , val1 ) ; err != nil {
if ! assert . NoError ( t , xl . SetSheetPrOptions ( sheet , test . nonDefault ) , opt ) {
t . Fatalf ( "%T: %s" , opt , err )
t . FailNow ( )
}
}
// Get again and compare
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , val1 ) , opt ) {
if err := xl . GetSheetPrOptions ( sheet , val2 ) ; err != nil {
t . FailNow ( )
t . Fatalf ( "%T: %s" , opt , err )
}
}
// Get again and compare
if ! reflect . DeepEqual ( val2 , val1 ) {
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , val2 ) , opt ) {
t . Fatalf ( "%T: value should not have changed" , opt )
t . FailNow ( )
}
}
// Value should not be the same as the default
if ! assert . Equal ( t , val1 , val2 , "%T: value should not have changed" , opt ) {
if reflect . DeepEqual ( val1 , def ) {
t . FailNow ( )
t . Fatalf ( "%T: value should have changed from default" , opt )
}
}
// Value should not be the same as the default
if ! assert . NotEqual ( t , def , val1 , "%T: value should have changed from default" , opt ) {
// Restore the default value
t . FailNow ( )
if err := xl . SetSheetPrOptions ( sheet , def ) ; err != nil {
}
t . Fatalf ( "%T: %s" , opt , err )
// Restore the default value
}
if ! assert . NoError ( t , xl . SetSheetPrOptions ( sheet , def ) , opt ) {
if err := xl . GetSheetPrOptions ( sheet , val1 ) ; err != nil {
t . FailNow ( )
t . Fatalf ( "%T: %s" , opt , err )
}
}
if ! assert . NoError ( t , xl . GetSheetPrOptions ( sheet , val1 ) , opt ) {
if ! reflect . DeepEqual ( val1 , def ) {
t . FailNow ( )
t . Fatalf ( "%T: value should now be the same as default" , opt )
}
}
if ! assert . Equal ( t , def , val1 ) {
t . FailNow ( )
}
} )
}
}
}
}