| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package puny |
| 6 |
|
| 7 |
import "testing" |
| 8 |
|
| 9 |
type testCase struct { |
| 10 |
desc string |
| 11 |
encoded string |
| 12 |
decoded string |
| 13 |
} |
| 14 |
|
| 15 |
var testStrings = []testCase{ |
| 16 |
{encoded: "maana-pta", decoded: "mañana"}, |
| 17 |
{ |
| 18 |
encoded: "", decoded: "", |
| 19 |
}, |
| 20 |
{ |
| 21 |
desc: "a single basic code point", |
| 22 |
encoded: "Bach-", |
| 23 |
decoded: "Bach", |
| 24 |
}, |
| 25 |
{ |
| 26 |
desc: "a single non-ASCII character", |
| 27 |
encoded: "tda", |
| 28 |
decoded: "ü", |
| 29 |
}, |
| 30 |
{ |
| 31 |
desc: "multiple non-ASCII characters", |
| 32 |
encoded: "4can8av2009b", |
| 33 |
decoded: "üëäö♥", |
| 34 |
}, |
| 35 |
{ |
| 36 |
desc: "mix of ASCII and non-ASCII characters", |
| 37 |
encoded: "bcher-kva", |
| 38 |
decoded: "bücher", |
| 39 |
}, |
| 40 |
{ |
| 41 |
desc: "long string with both ASCII and non-ASCII characters", |
| 42 |
encoded: "Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal", |
| 43 |
decoded: "Willst du die Blüthe des frühen, die Früchte des späteren Jahres", |
| 44 |
}, |
| 45 |
{ |
| 46 |
desc: "Arabic (Egyptian)", |
| 47 |
encoded: "egbpdaj6bu4bxfgehfvwxn", |
| 48 |
decoded: "ليهمابتكلموشعربي؟", |
| 49 |
}, |
| 50 |
{ |
| 51 |
desc: "Chinese (simplified)", |
| 52 |
encoded: "ihqwcrb4cv8a8dqg056pqjye", |
| 53 |
decoded: "他们为什么不说中文", |
| 54 |
}, |
| 55 |
{ |
| 56 |
desc: "Chinese (traditional)", |
| 57 |
encoded: "ihqwctvzc91f659drss3x8bo0yb", |
| 58 |
decoded: "他們爲什麽不說中文", |
| 59 |
}, |
| 60 |
{ |
| 61 |
desc: "Czech", |
| 62 |
encoded: "Proprostnemluvesky-uyb24dma41a", |
| 63 |
decoded: "Pročprostěnemluvíčesky", |
| 64 |
}, |
| 65 |
{ |
| 66 |
desc: "Hebrew", |
| 67 |
encoded: "4dbcagdahymbxekheh6e0a7fei0b", |
| 68 |
decoded: "למההםפשוטלאמדבריםעברית", |
| 69 |
}, |
| 70 |
{ |
| 71 |
desc: "Hindi (Devanagari)", |
| 72 |
encoded: "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd", |
| 73 |
decoded: "यहलोगहिन्दीक्योंनहींबोलसकतेहैं", |
| 74 |
}, |
| 75 |
{ |
| 76 |
desc: "Japanese (kanji and hiragana)", |
| 77 |
encoded: "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa", |
| 78 |
decoded: "なぜみんな日本語を話してくれないのか", |
| 79 |
}, |
| 80 |
{ |
| 81 |
desc: "Korean (Hangul syllables)", |
| 82 |
encoded: "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c", |
| 83 |
decoded: "세계의모든사람들이한국어를이해한다면얼마나좋을까", |
| 84 |
}, |
| 85 |
{ |
| 86 |
desc: "Russian (Cyrillic)", |
| 87 |
encoded: "b1abfaaepdrnnbgefbadotcwatmq2g4l", |
| 88 |
decoded: "почемужеонинеговорятпорусски", |
| 89 |
}, |
| 90 |
{ |
| 91 |
desc: "Spanish", |
| 92 |
encoded: "PorqunopuedensimplementehablarenEspaol-fmd56a", |
| 93 |
decoded: "PorquénopuedensimplementehablarenEspañol", |
| 94 |
}, |
| 95 |
{ |
| 96 |
desc: "Vietnamese", |
| 97 |
encoded: "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g", |
| 98 |
decoded: "TạisaohọkhôngthểchỉnóitiếngViệt", |
| 99 |
}, |
| 100 |
{ |
| 101 |
encoded: "3B-ww4c5e180e575a65lsy2b", |
| 102 |
decoded: "3年B組金八先生", |
| 103 |
}, |
| 104 |
{ |
| 105 |
encoded: "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n", |
| 106 |
decoded: "安室奈美恵-with-SUPER-MONKEYS", |
| 107 |
}, |
| 108 |
{ |
| 109 |
encoded: "Hello-Another-Way--fc4qua05auwb3674vfr0b", |
| 110 |
decoded: "Hello-Another-Way-それぞれの場所", |
| 111 |
}, |
| 112 |
{ |
| 113 |
encoded: "2-u9tlzr9756bt3uc0v", |
| 114 |
decoded: "ひとつ屋根の下2", |
| 115 |
}, |
| 116 |
{ |
| 117 |
encoded: "MajiKoi5-783gue6qz075azm5e", |
| 118 |
decoded: "MajiでKoiする5秒前", |
| 119 |
}, |
| 120 |
{ |
| 121 |
encoded: "de-jg4avhby1noc0d", |
| 122 |
decoded: "パフィーdeルンバ", |
| 123 |
}, |
| 124 |
{ |
| 125 |
encoded: "d9juau41awczczp", |
| 126 |
decoded: "そのスピードで", |
| 127 |
}, |
| 128 |
{ |
| 129 |
desc: "ASCII string that breaks the existing rules for host-name labels", |
| 130 |
encoded: "-> $1.00 <--", |
| 131 |
decoded: "-> $1.00 <-", |
| 132 |
}, |
| 133 |
} |
| 134 |
var testDomains = []testCase{ |
| 135 |
{ |
| 136 |
decoded: "mañana.com", |
| 137 |
encoded: "xn--maana-pta.com", |
| 138 |
}, |
| 139 |
{ |
| 140 |
decoded: "example.com.", |
| 141 |
encoded: "example.com.", |
| 142 |
}, |
| 143 |
{ |
| 144 |
decoded: "bücher.com", |
| 145 |
encoded: "xn--bcher-kva.com", |
| 146 |
}, |
| 147 |
{ |
| 148 |
decoded: "café.com", |
| 149 |
encoded: "xn--caf-dma.com", |
| 150 |
}, |
| 151 |
{ |
| 152 |
decoded: "☃-⌘.com", |
| 153 |
encoded: "xn----dqo34k.com", |
| 154 |
}, |
| 155 |
{ |
| 156 |
decoded: "퐀☃-⌘.com", |
| 157 |
encoded: "xn----dqo34kn65z.com", |
| 158 |
}, |
| 159 |
{ |
| 160 |
desc: "Emoji", |
| 161 |
decoded: "💩.la", |
| 162 |
encoded: "xn--ls8h.la", |
| 163 |
}, |
| 164 |
{ |
| 165 |
desc: "Non-printable ASCII", |
| 166 |
decoded: "\x00\x01\x02foo.bar", |
| 167 |
encoded: "\x00\x01\x02foo.bar", |
| 168 |
}, |
| 169 |
} |
| 170 |
|
| 171 |
var testSeparators = []testCase{ |
| 172 |
{ |
| 173 |
desc: "Using U+002E as separator", |
| 174 |
decoded: "mañana.com", |
| 175 |
encoded: "xn--maana-pta.com", |
| 176 |
}, |
| 177 |
{ |
| 178 |
desc: "Using U+3002 as separator", |
| 179 |
decoded: "mañana\u3002com", |
| 180 |
encoded: "xn--maana-pta.com", |
| 181 |
}, |
| 182 |
{ |
| 183 |
desc: "Using U+FF0E as separator", |
| 184 |
decoded: "mañana\uFF0Ecom", |
| 185 |
encoded: "xn--maana-pta.com", |
| 186 |
}, |
| 187 |
{ |
| 188 |
desc: "Using U+FF61 as separator", |
| 189 |
decoded: "mañana\uFF61com", |
| 190 |
encoded: "xn--maana-pta.com", |
| 191 |
}, |
| 192 |
} |
| 193 |
|
| 194 |
func TestDecode(t *testing.T) { |
| 195 |
for _, tc := range testStrings { |
| 196 |
got, _ := Decode(tc.encoded) |
| 197 |
if got != tc.decoded { |
| 198 |
t.Errorf("Decode(%q) = %q, want %q", tc.encoded, got, tc.decoded) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func TestEncode(t *testing.T) { |
| 204 |
for _, tc := range testStrings { |
| 205 |
got, _ := Encode(tc.decoded) |
| 206 |
if got != tc.encoded { |
| 207 |
t.Errorf("Encode(%q) = %q, want %q", tc.decoded, got, tc.encoded) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestToUnicode(t *testing.T) { |
| 213 |
for _, tc := range testDomains { |
| 214 |
got := ToUnicode(tc.encoded) |
| 215 |
if got != tc.decoded { |
| 216 |
t.Errorf("ToUnicode(%q) = %q, want %q", tc.encoded, got, tc.decoded) |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func TestToASCII(t *testing.T) { |
| 222 |
for _, tc := range testDomains { |
| 223 |
got := ToASCII(tc.decoded) |
| 224 |
if got != tc.encoded { |
| 225 |
t.Errorf("ToASCII(%q) = %q, want %q", tc.decoded, got, tc.encoded) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func TestSeparators(t *testing.T) { |
| 231 |
for _, tc := range testSeparators { |
| 232 |
got := ToASCII(tc.decoded) |
| 233 |
if got != tc.encoded { |
| 234 |
t.Errorf("ToASCII(%q) = %q, want %q", tc.decoded, got, tc.encoded) |
| 235 |
} |
| 236 |
} |
| 237 |
} |