@@ -695,3 +695,342 @@ func TestOptions_Validation(t *testing.T) {
695695 }
696696 })
697697}
698+
699+ func TestDocumentBuilder_Options (t * testing.T ) {
700+ t .Run ("WithPageSize sets page size" , func (t * testing.T ) {
701+ builder := NewDocumentBuilder (
702+ WithPageSize (Letter ),
703+ )
704+ builder .AddParagraph ().Text ("Test" ).End ()
705+
706+ doc , err := builder .Build ()
707+ if err != nil {
708+ t .Fatalf ("expected no error, got %v" , err )
709+ }
710+
711+ sections := doc .Sections ()
712+ if len (sections ) == 0 {
713+ t .Fatal ("expected at least one section" )
714+ }
715+
716+ // Note: Default is A4, so we just verify a document was created
717+ // The page size conversion is handled by the builder
718+ if doc == nil {
719+ t .Error ("expected document to be created" )
720+ }
721+ })
722+
723+ t .Run ("WithMargins sets margins" , func (t * testing.T ) {
724+ margins := Margins {
725+ Top : 1440 ,
726+ Bottom : 1440 ,
727+ Left : 1440 ,
728+ Right : 1440 ,
729+ }
730+ builder := NewDocumentBuilder (
731+ WithMargins (margins ),
732+ )
733+ builder .AddParagraph ().Text ("Test" ).End ()
734+
735+ doc , err := builder .Build ()
736+ if err != nil {
737+ t .Fatalf ("expected no error, got %v" , err )
738+ }
739+
740+ sections := doc .Sections ()
741+ if len (sections ) == 0 {
742+ t .Fatal ("expected at least one section" )
743+ }
744+
745+ actualMargins := sections [0 ].Margins ()
746+ if actualMargins .Top != margins .Top || actualMargins .Left != margins .Left {
747+ t .Errorf ("expected margins %+v, got %+v" , margins , actualMargins )
748+ }
749+ })
750+
751+ t .Run ("WithMetadata sets document metadata" , func (t * testing.T ) {
752+ meta := & domain.Metadata {
753+ Title : "Test Title" ,
754+ Creator : "Test Author" ,
755+ Subject : "Test Subject" ,
756+ }
757+ builder := NewDocumentBuilder (
758+ WithMetadata (meta ),
759+ )
760+ builder .AddParagraph ().Text ("Test" ).End ()
761+
762+ doc , err := builder .Build ()
763+ if err != nil {
764+ t .Fatalf ("expected no error, got %v" , err )
765+ }
766+
767+ metadata := doc .Metadata ()
768+ if metadata .Title != "Test Title" {
769+ t .Errorf ("expected title 'Test Title', got %s" , metadata .Title )
770+ }
771+ if metadata .Creator != "Test Author" {
772+ t .Errorf ("expected author 'Test Author', got %s" , metadata .Creator )
773+ }
774+ if metadata .Subject != "Test Subject" {
775+ t .Errorf ("expected subject 'Test Subject', got %s" , metadata .Subject )
776+ }
777+ })
778+
779+ t .Run ("WithSubject sets subject" , func (t * testing.T ) {
780+ builder := NewDocumentBuilder (
781+ WithSubject ("Test Subject" ),
782+ )
783+ builder .AddParagraph ().Text ("Test" ).End ()
784+
785+ doc , err := builder .Build ()
786+ if err != nil {
787+ t .Fatalf ("expected no error, got %v" , err )
788+ }
789+
790+ metadata := doc .Metadata ()
791+ if metadata .Subject != "Test Subject" {
792+ t .Errorf ("expected subject 'Test Subject', got %s" , metadata .Subject )
793+ }
794+ })
795+
796+ t .Run ("WithStrictValidation enables strict validation" , func (t * testing.T ) {
797+ builder := NewDocumentBuilder (
798+ WithStrictValidation (),
799+ )
800+ builder .AddParagraph ().Text ("Test" ).End ()
801+
802+ doc , err := builder .Build ()
803+ if err != nil {
804+ t .Fatalf ("expected no error, got %v" , err )
805+ }
806+ if doc == nil {
807+ t .Fatal ("expected document, got nil" )
808+ }
809+ })
810+ }
811+
812+ func TestDocumentBuilder_SetMetadata (t * testing.T ) {
813+ t .Run ("sets metadata on builder" , func (t * testing.T ) {
814+ builder := NewDocumentBuilder ()
815+ meta := & domain.Metadata {
816+ Title : "Title" ,
817+ Creator : "Author" ,
818+ Subject : "Subject" ,
819+ }
820+ builder .SetMetadata (meta )
821+ builder .AddParagraph ().Text ("Test" ).End ()
822+
823+ doc , err := builder .Build ()
824+ if err != nil {
825+ t .Fatalf ("expected no error, got %v" , err )
826+ }
827+
828+ metadata := doc .Metadata ()
829+ if metadata .Title != "Title" {
830+ t .Errorf ("expected title 'Title', got %s" , metadata .Title )
831+ }
832+ if metadata .Creator != "Author" {
833+ t .Errorf ("expected creator 'Author', got %s" , metadata .Creator )
834+ }
835+ })
836+ }
837+
838+ func TestDocumentBuilder_Footer (t * testing.T ) {
839+ t .Run ("adds footer to section" , func (t * testing.T ) {
840+ builder := NewDocumentBuilder ()
841+ secBuilder := builder .DefaultSection ()
842+
843+ footer , err := secBuilder .Footer (domain .FooterDefault )
844+ if err != nil {
845+ t .Fatalf ("expected footer, got error %v" , err )
846+ }
847+
848+ para , err := footer .AddParagraph ()
849+ if err != nil {
850+ t .Fatalf ("expected paragraph in footer, got %v" , err )
851+ }
852+
853+ run , err := para .AddRun ()
854+ if err != nil {
855+ t .Fatalf ("expected run in footer paragraph, got %v" , err )
856+ }
857+
858+ run .AddText ("Footer text" )
859+ secBuilder .End ()
860+ builder .AddParagraph ().Text ("Content" ).End ()
861+
862+ doc , err := builder .Build ()
863+ if err != nil {
864+ t .Fatalf ("expected no error, got %v" , err )
865+ }
866+
867+ sections := doc .Sections ()
868+ if len (sections ) == 0 {
869+ t .Fatal ("expected at least one section" )
870+ }
871+
872+ // Footer should be present
873+ footer2 , err := sections [0 ].Footer (domain .FooterDefault )
874+ if err != nil || footer2 == nil {
875+ t .Fatal ("expected footer in section" )
876+ }
877+ })
878+ }
879+
880+ func TestDocumentBuilder_SectionAccess (t * testing.T ) {
881+ t .Run ("Section returns current section" , func (t * testing.T ) {
882+ builder := NewDocumentBuilder ()
883+ builder .AddParagraph ().Text ("Test" ).End ()
884+
885+ secBuilder := builder .DefaultSection ()
886+ section := secBuilder .Section ()
887+ if section == nil {
888+ t .Fatal ("expected section, got nil" )
889+ }
890+
891+ // Should be able to use the returned section
892+ secBuilder .Orientation (domain .OrientationLandscape ).End ()
893+
894+ doc , err := builder .Build ()
895+ if err != nil {
896+ t .Fatalf ("expected no error, got %v" , err )
897+ }
898+
899+ sections := doc .Sections ()
900+ if len (sections ) == 0 {
901+ t .Fatal ("expected at least one section" )
902+ }
903+ })
904+ }
905+
906+ func TestFieldCreationFunctions (t * testing.T ) {
907+ t .Run ("NewField creates field" , func (t * testing.T ) {
908+ field := NewField (domain .FieldTypePageNumber )
909+ if field == nil {
910+ t .Fatal ("expected field, got nil" )
911+ }
912+ if field .Type () != domain .FieldTypePageNumber {
913+ t .Errorf ("expected FieldTypePageNumber, got %v" , field .Type ())
914+ }
915+ })
916+
917+ t .Run ("NewPageNumberField creates page number field" , func (t * testing.T ) {
918+ field := NewPageNumberField ()
919+ if field == nil {
920+ t .Fatal ("expected field, got nil" )
921+ }
922+ if field .Type () != domain .FieldTypePageNumber {
923+ t .Errorf ("expected FieldTypePageNumber, got %v" , field .Type ())
924+ }
925+ })
926+
927+ t .Run ("NewPageCountField creates page count field" , func (t * testing.T ) {
928+ field := NewPageCountField ()
929+ if field == nil {
930+ t .Fatal ("expected field, got nil" )
931+ }
932+ // FieldTypePageCount is an alias for FieldTypeNumPages
933+ fieldType := field .Type ()
934+ if fieldType != domain .FieldTypeNumPages && fieldType != domain .FieldTypePageCount {
935+ t .Errorf ("expected FieldTypeNumPages or FieldTypePageCount, got %v" , fieldType )
936+ }
937+ })
938+
939+ t .Run ("NewTOCField creates TOC field" , func (t * testing.T ) {
940+ switches := map [string ]string {
941+ "levels" : "1-3" ,
942+ "hyperlinks" : "true" ,
943+ }
944+ field := NewTOCField (switches )
945+ if field == nil {
946+ t .Fatal ("expected field, got nil" )
947+ }
948+ if field .Type () != domain .FieldTypeTOC {
949+ t .Errorf ("expected FieldTypeTOC, got %v" , field .Type ())
950+ }
951+ })
952+
953+ t .Run ("NewHyperlinkField creates hyperlink field" , func (t * testing.T ) {
954+ field := NewHyperlinkField ("https://example.com" , "Example" )
955+ if field == nil {
956+ t .Fatal ("expected field, got nil" )
957+ }
958+ if field .Type () != domain .FieldTypeHyperlink {
959+ t .Errorf ("expected FieldTypeHyperlink, got %v" , field .Type ())
960+ }
961+ })
962+
963+ t .Run ("NewStyleRefField creates style ref field" , func (t * testing.T ) {
964+ field := NewStyleRefField ("Heading 1" )
965+ if field == nil {
966+ t .Fatal ("expected field, got nil" )
967+ }
968+ if field .Type () != domain .FieldTypeStyleRef {
969+ t .Errorf ("expected FieldTypeStyleRef, got %v" , field .Type ())
970+ }
971+ })
972+ }
973+
974+ func TestTableBuilder_Style (t * testing.T ) {
975+ t .Run ("sets table style" , func (t * testing.T ) {
976+ builder := NewDocumentBuilder ()
977+ builder .AddTable (1 , 1 ).
978+ Style (domain .TableStyleGrid ).
979+ Row (0 ).Cell (0 ).Text ("Cell 1" ).End ().End ().
980+ End ()
981+
982+ doc , err := builder .Build ()
983+ if err != nil {
984+ t .Fatalf ("expected no error, got %v" , err )
985+ }
986+
987+ tables := doc .Tables ()
988+ if len (tables ) == 0 {
989+ t .Fatal ("expected at least one table" )
990+ }
991+ })
992+ }
993+
994+ func TestParagraphBuilder_AddImage (t * testing.T ) {
995+ t .Run ("AddImage fails with empty path" , func (t * testing.T ) {
996+ builder := NewDocumentBuilder ()
997+ builder .AddParagraph ().
998+ Text ("Before image" ).
999+ AddImage ("" ).
1000+ End ()
1001+
1002+ _ , err := builder .Build ()
1003+ if err == nil {
1004+ t .Fatal ("expected error for empty image path, got nil" )
1005+ }
1006+ })
1007+
1008+ t .Run ("AddImageWithSize fails with empty path" , func (t * testing.T ) {
1009+ builder := NewDocumentBuilder ()
1010+ size := domain .NewImageSize (100 , 100 )
1011+ builder .AddParagraph ().
1012+ Text ("Before image" ).
1013+ AddImageWithSize ("" , size ).
1014+ End ()
1015+
1016+ _ , err := builder .Build ()
1017+ if err == nil {
1018+ t .Fatal ("expected error for empty image path, got nil" )
1019+ }
1020+ })
1021+
1022+ t .Run ("AddImageWithPosition fails with empty path" , func (t * testing.T ) {
1023+ builder := NewDocumentBuilder ()
1024+ size := domain .NewImageSize (100 , 100 )
1025+ pos := domain .DefaultImagePosition ()
1026+ builder .AddParagraph ().
1027+ Text ("Before image" ).
1028+ AddImageWithPosition ("" , size , pos ).
1029+ End ()
1030+
1031+ _ , err := builder .Build ()
1032+ if err == nil {
1033+ t .Fatal ("expected error for empty image path, got nil" )
1034+ }
1035+ })
1036+ }
0 commit comments