diff options
Diffstat (limited to 'tests/invoice_check.c')
| -rw-r--r-- | tests/invoice_check.c | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/tests/invoice_check.c b/tests/invoice_check.c index 46b1621..e61a41d 100644 --- a/tests/invoice_check.c +++ b/tests/invoice_check.c @@ -29,8 +29,8 @@ static void eq64(const char *what, int64_t got, int64_t want) static const struct invoice_line synthetic_lines[] = { { "", "Utvecklingsarbete\nPeriod 2026-01-01 tom 2026-01-31", 61000, "tim", - 120000, 7320000, "", "25" }, - { "A-1", "Konsult", 12500, "tim", 80000, 1000000, "Omvänd moms", "rc" }, + 120000, 7320000, "", "25", 0 }, + { "A-1", "Konsult", 12500, "tim", 80000, 1000000, "Omvänd moms", "rc", 0 }, }; static void make_doc(struct invoice_doc *d) @@ -65,12 +65,12 @@ static void make_doc(struct invoice_doc *d) static void test_totals(void) { static const struct invoice_line lines[] = { - { "", "a", 1000, "st", 1000, 100000, "", "25" }, - { "", "b", 1000, "st", 1000, 50000, "", "12" }, - { "", "c", 1000, "st", 1000, 10000, "", "6" }, - { "", "d", 1000, "st", 1000, 25000, "", "rc" }, - { "", "e", 1000, "st", 1000, 15000, "", "0" }, - { "", "f", 1000, "st", 1000, 4000, "", "eu" }, + { "", "a", 1000, "st", 1000, 100000, "", "25", 0 }, + { "", "b", 1000, "st", 1000, 50000, "", "12", 0 }, + { "", "c", 1000, "st", 1000, 10000, "", "6", 0 }, + { "", "d", 1000, "st", 1000, 25000, "", "rc", 0 }, + { "", "e", 1000, "st", 1000, 15000, "", "0", 0 }, + { "", "f", 1000, "st", 1000, 4000, "", "eu", 0 }, }; struct invoice_doc d; struct invoice_totals t; @@ -89,7 +89,7 @@ static void test_totals(void) { static const struct invoice_line half[] = { - { "", "half", 1000, "st", 100, 50, "", "25" }, + { "", "half", 1000, "st", 100, 50, "", "25", 0 }, }; d.lines = half; @@ -129,6 +129,19 @@ static int contains(const unsigned char *data, size_t len, const char *needle) return 0; } +static int count_of(const unsigned char *data, size_t len, const char *needle) +{ + size_t n = strlen(needle); + int count = 0; + + if (!n) + return 0; + for (size_t i = 0; i + n <= len; i++) + if (memcmp(data + i, needle, n) == 0) + count++; + return count; +} + static void test_render(const char *path) { struct invoice_doc d; @@ -147,6 +160,8 @@ static void test_render(const char *path) if (data) { check(memcmp(data, "%PDF-1.4", 8) == 0, "render header"); check(contains(data, len, "Testleverant"), "render seller text"); + check(count_of(data, len, "Testleverant") == 2, + "render seller name in header and footer"); check(contains(data, len, "Testkund AB"), "render customer text"); check(contains(data, len, "Summa att betala SEK"), "render summary"); check(contains(data, len, "Test Person"), "render our ref"); @@ -166,6 +181,45 @@ static void test_render(const char *path) free(data); { + unsigned char *colored = NULL; + size_t colored_len = 0; + + d.header_color = "#ff0000"; + check(invoice_render_pdf(&d, &colored, &colored_len) == 0, + "render custom colour returns 0"); + if (colored) + check(contains(colored, colored_len, "1 0 0 rg"), + "render custom header colour"); + free(colored); + + colored = NULL; + d.header_color = "not-a-colour"; + check(invoice_render_pdf(&d, &colored, &colored_len) == 0, + "render invalid colour returns 0"); + if (colored) + check(!contains(colored, colored_len, "1 0 0 rg"), + "render invalid colour falls back"); + free(colored); + d.header_color = NULL; + } + { + static char long_name[240]; + unsigned char *long_pdf = NULL; + size_t long_len = 0; + + memset(long_name, 'A', sizeof long_name - 1); + long_name[sizeof long_name - 1] = '\0'; + d.seller.name = long_name; + check(invoice_render_pdf(&d, &long_pdf, &long_len) == 0, + "render long name returns 0"); + if (long_pdf) + check(contains(long_pdf, long_len, "..."), + "render long name is truncated"); + free(long_pdf); + d.seller.name = "Testleverantör AB"; + } + + { unsigned char *bad = NULL; size_t n = 0; |
