aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/query/outputs_test.go
blob: 8a1bb2940dd85a47b92babbd368077c795c9be1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package query_test

import (
	"errors"
	"slices"
	"testing"

	"github.com/jpappel/atlas/pkg/query"
)

const (
	OUT_TOK_STR      = query.OUT_TOK_STR
	OUT_TOK_PATH     = query.OUT_TOK_PATH
	OUT_TOK_TITLE    = query.OUT_TOK_TITLE
	OUT_TOK_DATE     = query.OUT_TOK_DATE
	OUT_TOK_FILETIME = query.OUT_TOK_FILETIME
	OUT_TOK_AUTHORS  = query.OUT_TOK_AUTHORS
	OUT_TOK_TAGS     = query.OUT_TOK_TAGS
	OUT_TOK_LINKS    = query.OUT_TOK_LINKS
	OUT_TOK_META     = query.OUT_TOK_META
)

func Test_parseOutputFormat(t *testing.T) {
	tests := []struct {
		name        string
		formatStr   string
		wantToks    []query.OutputToken
		wantStrToks []string
		wantErr     error
	}{
		{
			"one big string",
			"here is a string with no placeholders",
			[]query.OutputToken{OUT_TOK_STR},
			[]string{"here is a string with no placeholders"},
			nil,
		},
		{
			"default format",
			"%p %T %d authors:%a tags:%t",
			[]query.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%%",
			[]query.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 := query.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)
			}
		})
	}
}