File: puny_test.go

package info (click to toggle)
golang-gitlab-golang-commonmark-puny 0.0~git20191124.9f83538-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 2
file content (237 lines) | stat: -rw-r--r-- 5,596 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found encoded the LICENSE file.

package puny

import "testing"

type testCase struct {
	desc    string //nolint:structcheck
	encoded string
	decoded string
}

var testStrings = []testCase{
	{encoded: "maana-pta", decoded: "mañana"},
	{
		encoded: "", decoded: "",
	},
	{
		desc:    "a single basic code point",
		encoded: "Bach-",
		decoded: "Bach",
	},
	{
		desc:    "a single non-ASCII character",
		encoded: "tda",
		decoded: "ü",
	},
	{
		desc:    "multiple non-ASCII characters",
		encoded: "4can8av2009b",
		decoded: "üëäö♥",
	},
	{
		desc:    "mix of ASCII and non-ASCII characters",
		encoded: "bcher-kva",
		decoded: "bücher",
	},
	{
		desc:    "long string with both ASCII and non-ASCII characters",
		encoded: "Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal",
		decoded: "Willst du die Blüthe des frühen, die Früchte des späteren Jahres",
	},
	{
		desc:    "Arabic (Egyptian)",
		encoded: "egbpdaj6bu4bxfgehfvwxn",
		decoded: "ليهمابتكلموشعربي؟",
	},
	{
		desc:    "Chinese (simplified)",
		encoded: "ihqwcrb4cv8a8dqg056pqjye",
		decoded: "他们为什么不说中文",
	},
	{
		desc:    "Chinese (traditional)",
		encoded: "ihqwctvzc91f659drss3x8bo0yb",
		decoded: "他們爲什麽不說中文",
	},
	{
		desc:    "Czech",
		encoded: "Proprostnemluvesky-uyb24dma41a",
		decoded: "Pročprostěnemluvíčesky",
	},
	{
		desc:    "Hebrew",
		encoded: "4dbcagdahymbxekheh6e0a7fei0b",
		decoded: "למההםפשוטלאמדבריםעברית",
	},
	{
		desc:    "Hindi (Devanagari)",
		encoded: "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd",
		decoded: "यहलोगहिन्दीक्योंनहींबोलसकतेहैं",
	},
	{
		desc:    "Japanese (kanji and hiragana)",
		encoded: "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa",
		decoded: "なぜみんな日本語を話してくれないのか",
	},
	{
		desc:    "Korean (Hangul syllables)",
		encoded: "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c",
		decoded: "세계의모든사람들이한국어를이해한다면얼마나좋을까",
	},
	{
		desc:    "Russian (Cyrillic)",
		encoded: "b1abfaaepdrnnbgefbadotcwatmq2g4l",
		decoded: "почемужеонинеговорятпорусски",
	},
	{
		desc:    "Spanish",
		encoded: "PorqunopuedensimplementehablarenEspaol-fmd56a",
		decoded: "PorquénopuedensimplementehablarenEspañol",
	},
	{
		desc:    "Vietnamese",
		encoded: "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g",
		decoded: "TạisaohọkhôngthểchỉnóitiếngViệt",
	},
	{
		encoded: "3B-ww4c5e180e575a65lsy2b",
		decoded: "3年B組金八先生",
	},
	{
		encoded: "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n",
		decoded: "安室奈美恵-with-SUPER-MONKEYS",
	},
	{
		encoded: "Hello-Another-Way--fc4qua05auwb3674vfr0b",
		decoded: "Hello-Another-Way-それぞれの場所",
	},
	{
		encoded: "2-u9tlzr9756bt3uc0v",
		decoded: "ひとつ屋根の下2",
	},
	{
		encoded: "MajiKoi5-783gue6qz075azm5e",
		decoded: "MajiでKoiする5秒前",
	},
	{
		encoded: "de-jg4avhby1noc0d",
		decoded: "パフィーdeルンバ",
	},
	{
		encoded: "d9juau41awczczp",
		decoded: "そのスピードで",
	},
	{
		desc:    "ASCII string that breaks the existing rules for host-name labels",
		encoded: "-> $1.00 <--",
		decoded: "-> $1.00 <-",
	},
}
var testDomains = []testCase{
	{
		decoded: "mañana.com",
		encoded: "xn--maana-pta.com",
	},
	{ // https://github.com/bestiejs/punycode.js/issues/17
		decoded: "example.com.",
		encoded: "example.com.",
	},
	{
		decoded: "bücher.com",
		encoded: "xn--bcher-kva.com",
	},
	{
		decoded: "café.com",
		encoded: "xn--caf-dma.com",
	},
	{
		decoded: "☃-⌘.com",
		encoded: "xn----dqo34k.com",
	},
	{
		decoded: "퐀☃-⌘.com",
		encoded: "xn----dqo34kn65z.com",
	},
	{
		desc:    "Emoji",
		decoded: "💩.la",
		encoded: "xn--ls8h.la",
	},
	{
		desc:    "Non-printable ASCII",
		decoded: "\x00\x01\x02foo.bar",
		encoded: "\x00\x01\x02foo.bar",
	},
}

var testSeparators = []testCase{
	{
		desc:    "Using U+002E as separator",
		decoded: "mañana.com",
		encoded: "xn--maana-pta.com",
	},
	{
		desc:    "Using U+3002 as separator",
		decoded: "mañana\u3002com",
		encoded: "xn--maana-pta.com",
	},
	{
		desc:    "Using U+FF0E as separator",
		decoded: "mañana\uFF0Ecom",
		encoded: "xn--maana-pta.com",
	},
	{
		desc:    "Using U+FF61 as separator",
		decoded: "mañana\uFF61com",
		encoded: "xn--maana-pta.com",
	},
}

func TestDecode(t *testing.T) {
	for _, tc := range testStrings {
		got, _ := Decode(tc.encoded)
		if got != tc.decoded {
			t.Errorf("Decode(%q) = %q, want %q", tc.encoded, got, tc.decoded)
		}
	}
}

func TestEncode(t *testing.T) {
	for _, tc := range testStrings {
		got, _ := Encode(tc.decoded)
		if got != tc.encoded {
			t.Errorf("Encode(%q) = %q, want %q", tc.decoded, got, tc.encoded)
		}
	}
}

func TestToUnicode(t *testing.T) {
	for _, tc := range testDomains {
		got := ToUnicode(tc.encoded)
		if got != tc.decoded {
			t.Errorf("ToUnicode(%q) = %q, want %q", tc.encoded, got, tc.decoded)
		}
	}
}

func TestToASCII(t *testing.T) {
	for _, tc := range testDomains {
		got := ToASCII(tc.decoded)
		if got != tc.encoded {
			t.Errorf("ToASCII(%q) = %q, want %q", tc.decoded, got, tc.encoded)
		}
	}
}

func TestSeparators(t *testing.T) {
	for _, tc := range testSeparators {
		got := ToASCII(tc.decoded)
		if got != tc.encoded {
			t.Errorf("ToASCII(%q) = %q, want %q", tc.decoded, got, tc.encoded)
		}
	}
}