~seirdy/moac

moac/charsets/buildcharsets_test.go -rw-r--r-- 5.6 KiB
97035daeRohan Kumar Chore(deps): bump deps 7 months 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package charsets_test

import (
	"testing"

	"git.sr.ht/~seirdy/moac/v2/charsets"
)

type buildCharsetsTestCase struct {
	charsetsExpected charsets.CharsetCollection
	name             string
	charsetsNamed    []string
}

func buildCharsetsTables() []buildCharsetsTestCase { //nolint:funlen // single statement; length from tables
	return []buildCharsetsTestCase{
		{
			name: "many dupe numbers",
			charsetsNamed: []string{
				"uppercase", "lowercase", "numbers", "symbols", "latin", "🧛",
				"000000",
				"000000",
				"0",
				"000000000000000000000000000000",
				"1234000",
				"000000",
				"ascii",
				"000000000000000000000000000000",
				"898",
			},
			charsetsExpected: stringsToCharsetCollection([]string{
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
				"abcdefghijklmnopqrstuvwxyz",
				"567",
				"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
				"¡¢£¤¥¦§¨©ª«¬®¯°±²³´μ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ",
				"ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ",
				"ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏƐƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯǰDZDzdzǴǵǶǷǸǹǺǻǼǽǾǿȀȁȂȃȄȅȆȇȈȉȊȋȌȍȎȏȐȑȒȓȔȕȖȗȘșȚțȜȝȞȟȠȡȢȣȤȥȦȧȨȩȪȫȬȭȮȯȰȱȲȳȴȵȶȷȸȹȺȻȼȽȾȿɀɁɂɃɄɅɆɇɈɉɊɋɌɍɎɏ",
				"ɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟɠɡɢɣɤɥɦɧɨɩɪɫɬɭɮɯɰɱɲɳɴɵɶɷɸɹɺɻɼɽɾɿʀʁʂʃʄʅʆʇʈʉʊʋʌʍʎʏʐʑʒʓʔʕʖʗʘʙʚʛʜʝʞʟʠʡʢʣʤʥʦʧʨʩʪʫʬʭʮʯ",
				"🧛",
				"0",
				"1234",
				"89",
			}),
		},
		{
			name: "empty entries",
			charsetsNamed: []string{
				"uppercase", "lowercase", "numbers", "ascii",
				"000000",
				"000000",
				"",
				"000000000000000000000000000000",
				"",
				"000000",
				"0",
				"89",
			},
			charsetsExpected: stringsToCharsetCollection([]string{
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
				"abcdefghijklmnopqrstuvwxyz",
				"1234567",
				"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
				"0",
				"89",
			}),
		},
		{
			name: "unprintable gibberish",
			charsetsNamed: []string{
				charsets.Uppercase.String() + charsets.Lowercase.String(),
				"lowercase", "numbers", `„"O4UÞjÖÿ.…ßòºÒˆ&Û¨„5ü4äMƒî3îÌ`,
			},
			charsetsExpected: stringsToCharsetCollection([]string{
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
				"abcdefghiklmnopqrstuvwxyz",
				"0123456789",
				`"&.jƒ„…ˆ¨ºÌÒÖÛÞßäîòüÿ`,
			}),
		},
		{
			name: "subset and composite",
			charsetsNamed: []string{
				charsets.Uppercase.String() + charsets.Lowercase.String(),
				"lowercase", "numbers",
			},
			charsetsExpected: []charsets.Charset{charsets.Lowercase, charsets.Uppercase, charsets.Numbers},
		},
		{
			// say a user specifies a custom charset that shadows *almost* an entire previous charset, save for a single element
			// we shouldn't get a charset that includes only that single element
			name: "composite missing one letter",
			charsetsNamed: []string{
				"lowercase", "numbers",
				"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy",
			},
			charsetsExpected: []charsets.Charset{charsets.Lowercase, charsets.Uppercase, charsets.Numbers},
		},
	}
}

func formerNotFoundInLatter(former, latter charsets.CharsetCollection) (missing charsets.CharsetCollection) {
	// for each charset in former: add to missing if it isn't in latter
	for _, formerItem := range former {
		isMissing := true

		for _, latterItem := range latter {
			if formerItem.String() == latterItem.String() {
				isMissing = false

				break
			}
		}

		if isMissing {
			missing = append(missing, formerItem)
		}
	}

	return missing
}

func expectedMatchesActual(t *testing.T, expected, actual charsets.CharsetCollection) {
	t.Helper()

	var errStr string

	errStrFirstHalf, pass := handleMissingCharsets(
		formerNotFoundInLatter(expected, actual), "missing expected entries:   ")

	if !pass {
		errStr += "\n" + errStrFirstHalf
	}

	errStrSecondHalf, passSecondHalf := handleMissingCharsets(
		formerNotFoundInLatter(actual, expected), "contains unexpected entries:")

	if !passSecondHalf {
		pass = false
		errStr += "\n" + errStrSecondHalf
	}

	if pass {
		return
	}

	errStr = "generated charsets don't match expected:" + errStr + "\nactually got:"
	for _, actualCharset := range actual {
		errStr += "\n\"" + actualCharset.String() + `"`
	}

	if len(actual) == 0 {
		errStr = "built empty charset collection: " + errStr
	}

	t.Error(errStr)
}

func handleMissingCharsets(missing charsets.CharsetCollection, errType string) (errStr string, pass bool) {
	if len(missing) > 0 {
		errStr = "actual charset " + errType + " ["
		for _, missingCharset := range missing {
			errStr += `"` + missingCharset.String() + `", `
		}

		errStr += "]"

		return errStr, false
	}

	return "", true
}

func TestBuildCharsets(t *testing.T) {
	t.Parallel()

	for _, testCase := range buildCharsetsTables() {
		testCase := testCase
		t.Run(testCase.name, func(t *testing.T) {
			t.Parallel()
			charsetsActual := charsets.ParseCharsets(testCase.charsetsNamed)
			expectedMatchesActual(t, testCase.charsetsExpected, charsetsActual)
		})
	}
}

func stringsToCharsetCollection(s []string) (cs charsets.CharsetCollection) {
	cs = make([]charsets.Charset, len(s))

	for i, ccContents := range s {
		cs[i] = charsets.CustomCharset([]rune(ccContents))
	}

	return cs
}