~bigbes/sr-ht-compare

sr-ht-compare/core/names_test.go -rw-r--r-- 2.5 KiB
9720ccc2 — bigbes go.mod: take the shared libraries' current heads 2 days ago
                                                                                
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package core

import (
	"strings"
	"testing"
)

func TestValidOwner(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		{"bigbes", true},
		{"user_name", true},
		{"user-name", true},
		{"user.name", true},
		{"u123", true},
		{"a", true},

		{"", false},
		{"-leading", false},
		{"has/slash", false},
		{"has..dots", false},
		{"Upper", false},
		{"has space", false},
		{"tilde~", false},
		{"emoji😀", false},
	}
	for _, tc := range tests {
		if got := ValidOwner(tc.in); got != tc.want {
			t.Errorf("ValidOwner(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}

func TestValidRepoName(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		{"core-go", true},
		{"my.repo", true},
		{"repo_1", true},
		{strings.Repeat("a", 100), true},

		{"", false},
		{strings.Repeat("a", 101), false},
		{"-dashfirst", false},
		{"a/b", false},
		{"a..b", false},
		{"CamelCase", false},
	}
	for _, tc := range tests {
		if got := ValidRepoName(tc.in); got != tc.want {
			t.Errorf("ValidRepoName(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}

func TestValidRef(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		// Accepted.
		{"main", true},
		{"feature/foo", true},
		{"feature/with-slash", true},
		{"release/v1.0", true},
		{"v1.0.0", true},
		{"a/b/c/d", true},
		{"0123456789abcdef0123456789abcdef01234567", true},                         // SHA-1
		{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", true}, // SHA-256
		{"DEADBEEFdeadbeef0000111122223333abcdabcd", true},                         // mixed-case SHA-1
		{"foo.lockfile", true}, // ".lock" only rejected as a suffix

		// Rejected — structural.
		{"", false},
		{"@", false},
		{"-oops", false},
		{".hidden", false},
		{"/leading", false},
		{"trailing/", false},
		{"trailing.", false},
		{"foo.lock", false},
		{"feature/bar.lock", false}, // per-component .lock
		{"feature/.hidden", false},  // per-component leading dot
		{"a..b", false},
		{"a//b", false},
		{"main@{u}", false},

		// Rejected — forbidden bytes.
		{"has space", false},
		{"tilde~1", false},
		{"caret^", false},
		{"colon:x", false},
		{"star*", false},
		{"quest?", false},
		{"brack[et", false},
		{"back\\slash", false},
		{"ctrl\x01char", false},
		{"del\x7fchar", false},

		// Not a full-length SHA -> falls through to name rules (valid here).
		{"abcdef", true},
	}
	for _, tc := range tests {
		if got := ValidRef(tc.in); got != tc.want {
			t.Errorf("ValidRef(%q) = %v, want %v", tc.in, got, tc.want)
		}
	}
}