form_test.go

 1package form
 2
 3import (
 4	"testing"
 5)
 6
 7func TestNotEmpty(t *testing.T) {
 8	t.Parallel()
 9
10	validate := notEmpty("thing")
11
12	tests := []struct {
13		name    string
14		input   string
15		wantErr bool
16	}{
17		{name: "empty string", input: "", wantErr: true},
18		{name: "non-empty string", input: "hello", wantErr: false},
19		{name: "whitespace only", input: "  ", wantErr: true},
20	}
21
22	for _, tt := range tests {
23		t.Run(tt.name, func(t *testing.T) {
24			t.Parallel()
25			err := validate(tt.input)
26			if (err != nil) != tt.wantErr {
27				t.Errorf("notEmpty(%q): got err=%v, wantErr=%v", tt.input, err, tt.wantErr)
28			}
29		})
30	}
31}
32
33func TestSplitFields(t *testing.T) {
34	t.Parallel()
35
36	tests := []struct {
37		name  string
38		input string
39		want  []string
40	}{
41		{name: "empty", input: "", want: nil},
42		{name: "single path", input: "/home/user", want: []string{"/home/user"}},
43		{name: "two paths", input: "/home/user /etc", want: []string{"/home/user", "/etc"}},
44		{name: "leading whitespace", input: "  /home", want: []string{"/home"}},
45		{name: "trailing whitespace", input: "/home  ", want: []string{"/home"}},
46		{name: "tabs", input: "/a\t/b", want: []string{"/a", "/b"}},
47		{name: "multiple spaces", input: "/a   /b   /c", want: []string{"/a", "/b", "/c"}},
48	}
49
50	for _, tt := range tests {
51		t.Run(tt.name, func(t *testing.T) {
52			t.Parallel()
53			got := splitFields(tt.input)
54			if len(got) != len(tt.want) {
55				t.Fatalf("splitFields(%q): got %v, want %v", tt.input, got, tt.want)
56			}
57			for i := range got {
58				if got[i] != tt.want[i] {
59					t.Errorf("splitFields(%q)[%d]: got %q, want %q", tt.input, i, got[i], tt.want[i])
60				}
61			}
62		})
63	}
64}