From 35ec0f5afb9800b25bd813bccc57a16bc9f837c4 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Mon, 28 Apr 2025 23:34:42 -0400 Subject: Add document output formatting --- pkg/query/outputs_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pkg/query/outputs_test.go (limited to 'pkg/query/outputs_test.go') diff --git a/pkg/query/outputs_test.go b/pkg/query/outputs_test.go new file mode 100644 index 0000000..b5fdba7 --- /dev/null +++ b/pkg/query/outputs_test.go @@ -0,0 +1,62 @@ +package query + +import ( + "errors" + "slices" + "testing" +) + +func Test_parseOutputFormat(t *testing.T) { + tests := []struct { + name string + formatStr string + wantToks []OutputToken + wantStrToks []string + wantErr error + }{ + { + "one big string", + "here is a string with no placeholders", + []OutputToken{OUT_TOK_STR}, + []string{"here is a string with no placeholders"}, + nil, + }, + { + "default format", + "%p %T %d authors:%a tags:%t", + []OutputToken{OUT_TOK_PATH, OUT_TOK_STR, OUT_TOK_TITLE, OUT_TOK_STR, OUT_TOK_DATE, OUT_TOK_STR, OUT_TOK_AUTHORS, OUT_TOK_STR, OUT_TOK_TAGS}, + []string{" ", " ", " authors:", " tags:"}, + nil, + }, + { + "literal percents", + "%%%p%%%T%%", + []OutputToken{OUT_TOK_STR, OUT_TOK_PATH, OUT_TOK_STR, OUT_TOK_TITLE, OUT_TOK_STR}, + []string{"%", "%", "%"}, + nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotToks, gotStrToks, gotErr := parseOutputFormat(tt.formatStr) + + if !errors.Is(gotErr, tt.wantErr) { + t.Errorf("Recieved unexpected error: got %v want %v", gotErr, tt.wantErr) + } else if gotErr != nil { + return + } + + if !slices.Equal(gotToks, tt.wantToks) { + t.Error("Unequal output tokens") + t.Log("Got:", gotToks) + t.Log("Want:", tt.wantToks) + } + + if !slices.Equal(gotStrToks, tt.wantStrToks) { + t.Error("Unequal string tokens") + t.Log("Got:", gotStrToks) + t.Log("Want:", tt.wantStrToks) + } + }) + } +} -- cgit v1.2.3