all repos

viye @ 91ebee3

my shot at reimplementing xiki

viye/internal/plumbing/json/json_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
update some inconsistencies, 1 month ago
1
package json
2
3
import (
4
	"testing"
5
6
	"github.com/olexsmir/viye/internal/viye"
7
8
	"olexsmir.xyz/x/is"
9
)
10
11
func TestMatch(t *testing.T) {
12
	got := (Tool{}).Match(&viye.Context{Cmd: "json"})
13
	is.Equal(t, true, got)
14
}
15
16
func TestExecute(t *testing.T) {
17
	tests := map[string]struct {
18
		body []string
19
		want string
20
	}{
21
		"no body":          {want: ": name: your name\n: age: 0\n"},
22
		"empty body slice": {want: ": name: your name\n: age: 0\n"},
23
		"single key": {
24
			body: []string{"name: olex"},
25
			want: `| {"name":"olex"}` + "\n",
26
		},
27
		"multiple keys": {
28
			body: []string{"name: olex", "age: 30"},
29
			want: `| {"age":"30","name":"olex"}` + "\n",
30
		},
31
		"values with spaces": {
32
			body: []string{"title: hello world"},
33
			want: `| {"title":"hello world"}` + "\n",
34
		},
35
		"keys with spaces": {
36
			body: []string{"my key: value"},
37
			want: `| {"my key":"value"}` + "\n",
38
		},
39
		"line without colon separator": {
40
			body: []string{"justtext"},
41
			want: `| {}` + "\n",
42
		},
43
		"line without : prefix": {
44
			body: []string{"name: olex"},
45
			want: `| {"name":"olex"}` + "\n",
46
		},
47
		"mixed valid and invalid": {
48
			body: []string{"name: olex", "garbage", "age: 30"},
49
			want: `| {"age":"30","name":"olex"}` + "\n",
50
		},
51
		"empty key": {
52
			body: []string{": value"},
53
			want: `| {}` + "\n",
54
		},
55
		"trailing whitespace in key": {
56
			body: []string{"name  : olex"},
57
			want: `| {"name":"olex"}` + "\n",
58
		},
59
		"trailing whitespace in value": {
60
			body: []string{"name: olex  "},
61
			want: `| {"name":"olex"}` + "\n",
62
		},
63
		"empty value": {
64
			body: []string{"name: "},
65
			want: `| {"name":""}` + "\n",
66
		},
67
	}
68
	for tname, tt := range tests {
69
		t.Run(tname, func(t *testing.T) {
70
			got, err := (Tool{}).Execute(&viye.Context{Cmd: "json", Body: tt.body})
71
			is.Err(t, err, nil)
72
			is.Equal(t, tt.want, got)
73
		})
74
	}
75
}