Skip to content

Commit

Permalink
Check document type on New
Browse files Browse the repository at this point in the history
  • Loading branch information
angelodlfrtr committed Jan 6, 2023
1 parent 3498a76 commit 28189cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
package generator

import (
"errors"

"github.com/creasty/defaults"
"github.com/go-pdf/fpdf"
"github.com/leekchan/accounting"
)

var ErrInvalidDocumentType = errors.New("invalid document type")

// New return a new documents with provided types and defaults
func New(docType string, options *Options) (*Document, error) {
_ = defaults.Set(options)

if docType != Invoice && docType != Quotation && docType != DeliveryNote {
return nil, ErrInvalidDocumentType
}

doc := &Document{
Options: options,
Type: docType,
Expand Down
32 changes: 24 additions & 8 deletions generator_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package generator

import (
"errors"
"os"
"testing"
)

func TestNewWithInvalidType(t *testing.T) {
_, err := New("INVALID", &Options{})

if errors.Is(err, ErrInvalidDocumentType) {
return
}

t.Fatalf("expected ErrInvalidDocumentType, got %v", err)
}

func TestNew(t *testing.T) {
doc, _ := New(Invoice, &Options{
TextTypeInvoice: "FACTURE",
TextRefTitle: "Réàf.",
AutoPrint: true,
BaseTextColor: []int{6, 63, 156},
GreyTextColor: []int{161, 96, 149},
GreyBgColor: []int{171, 240, 129},
DarkBgColor: []int{176, 12, 20},
doc, err := New(Invoice, &Options{
TextTypeInvoice: "FACTURE",
TextRefTitle: "Réàf.",
AutoPrint: true,
BaseTextColor: []int{6, 63, 156},
GreyTextColor: []int{161, 96, 149},
GreyBgColor: []int{171, 240, 129},
DarkBgColor: []int{176, 12, 20},
CurrencyPrecision: 2,
})

if err != nil {
t.Fatalf("got error %v", err)
}

doc.SetHeader(&HeaderFooter{
Text: "<center>Cupcake ipsum dolor sit amet bonbon. I love croissant cotton candy. Carrot cake sweet I love sweet roll cake powder.</center>",
Pagination: true,
Expand Down

0 comments on commit 28189cc

Please sign in to comment.