From 459f5935aab505cdb8174e5f9f04a9a3c426f37e Mon Sep 17 00:00:00 2001 From: Lena Date: Wed, 1 Apr 2026 00:00:00 +0000 Subject: Enter pdfstamp Terminal-only PDF stamp picker. Renders a coarse page preview in the terminal, places text and image stamps with the keyboard, saves the layout as a JSON plan, and burns it into an output PDF. MuPDF (via go-fitz) renders the preview; pdfcpu overlays the generated stamp PDF onto the source 1:1 and validates the result. --- pick_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 pick_test.go (limited to 'pick_test.go') diff --git a/pick_test.go b/pick_test.go new file mode 100644 index 0000000..c7ab985 --- /dev/null +++ b/pick_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestParsePages(t *testing.T) { + ok := map[string]struct { + in string + n int + want []int + }{ + "single": {"2", 5, []int{2}}, + "list": {"1,3", 5, []int{1, 3}}, + "range": {"2-4", 5, []int{2, 3, 4}}, + "mixed": {"1,3-5", 5, []int{1, 3, 4, 5}}, + "overlap": {"1-3,2-4", 5, []int{1, 2, 3, 4}}, + "all": {"all", 3, []int{1, 2, 3}}, + "spaces": {" 1 , 3 - 4 ", 5, []int{1, 3, 4}}, + "unordered": {"4,1", 5, []int{1, 4}}, + "full bounds": {"1-5", 5, []int{1, 2, 3, 4, 5}}, + } + for name, c := range ok { + got, err := parsePages(c.in, c.n) + if err != nil { + t.Errorf("%s: unexpected error: %v", name, err) + continue + } + if !reflect.DeepEqual(got, c.want) { + t.Errorf("%s: parsePages(%q,%d) = %v, want %v", name, c.in, c.n, got, c.want) + } + } + + bad := map[string]struct { + in string + n int + }{ + "empty": {"", 5}, + "zero": {"0", 5}, + "beyond doc": {"6", 5}, + "range beyond": {"4-6", 5}, + "reversed": {"4-2", 5}, + "junk": {"x", 5}, + "trailing junk": {"1,", 5}, + "open range": {"3-", 5}, + } + for name, c := range bad { + if got, err := parsePages(c.in, c.n); err == nil { + t.Errorf("%s: parsePages(%q,%d) = %v, expected error", name, c.in, c.n, got) + } + } +} + +func TestFormatPages(t *testing.T) { + cases := map[string]struct { + in []int + want string + }{ + "single": {[]int{2}, "2"}, + "run": {[]int{1, 2, 3}, "1-3"}, + "mixed": {[]int{1, 3, 4, 5, 9}, "1,3-5,9"}, + "unsorted": {[]int{4, 1, 2}, "1-2,4"}, + "two apart": {[]int{1, 3}, "1,3"}, + } + for name, c := range cases { + if got := formatPages(c.in); got != c.want { + t.Errorf("%s: formatPages(%v) = %q, want %q", name, c.in, got, c.want) + } + } +} -- cgit v1.2.3