golang.org/x/text@v0.14.0/internal/export/idna/idna_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package idna 6 7 import ( 8 "encoding/hex" 9 "fmt" 10 "regexp" 11 "strconv" 12 "strings" 13 "testing" 14 15 "golang.org/x/text/internal/testtext" 16 ) 17 18 func TestAllocToUnicode(t *testing.T) { 19 avg := testtext.AllocsPerRun(1000, func() { 20 ToUnicode("www.golang.org") 21 }) 22 if avg > 0 { 23 t.Errorf("got %f; want 0", avg) 24 } 25 } 26 27 func TestAllocToASCII(t *testing.T) { 28 avg := testtext.AllocsPerRun(1000, func() { 29 ToASCII("www.golang.org") 30 }) 31 if avg > 0 { 32 t.Errorf("got %f; want 0", avg) 33 } 34 } 35 36 func TestProfiles(t *testing.T) { 37 testCases := []struct { 38 name string 39 want, got *Profile 40 }{ 41 {"Punycode", punycode, New()}, 42 {"Registration", registration, New(ValidateForRegistration())}, 43 {"Registration", registration, New( 44 ValidateForRegistration(), 45 VerifyDNSLength(true), 46 BidiRule(), 47 )}, 48 {"Lookup", lookup, New(MapForLookup(), BidiRule(), Transitional(transitionalLookup))}, 49 {"Display", display, New(MapForLookup(), BidiRule())}, 50 } 51 for _, tc := range testCases { 52 // Functions are not comparable, but the printed version will include 53 // their pointers. 54 got := fmt.Sprintf("%#v", tc.got) 55 want := fmt.Sprintf("%#v", tc.want) 56 if got != want { 57 t.Errorf("%s: \ngot %#v,\nwant %#v", tc.name, got, want) 58 } 59 } 60 } 61 62 // doTest performs a single test f(input) and verifies that the output matches 63 // out and that the returned error is expected. The errors string contains 64 // all allowed error codes as categorized in 65 // https://www.unicode.org/Public/idna/9.0.0/IdnaTest.txt: 66 // P: Processing 67 // V: Validity 68 // A: to ASCII 69 // B: Bidi 70 // C: Context J 71 func doTest(t *testing.T, f func(string) (string, error), name, input, want, errors string) { 72 errors = strings.Trim(errors, "[]") 73 test := "ok" 74 if errors != "" { 75 test = "err:" + errors 76 } 77 // Replace some of the escape sequences to make it easier to single out 78 // tests on the command name. 79 in := strings.Trim(strconv.QuoteToASCII(input), `"`) 80 in = strings.Replace(in, `\u`, "#", -1) 81 in = strings.Replace(in, `\U`, "#", -1) 82 name = fmt.Sprintf("%s/%s/%s", name, in, test) 83 84 testtext.Run(t, name, func(t *testing.T) { 85 got, err := f(input) 86 87 if err != nil { 88 code := err.(interface { 89 code() string 90 }).code() 91 if strings.Index(errors, code) == -1 { 92 t.Errorf("error %q not in set of expected errors {%v}", code, errors) 93 } 94 } else if errors != "" { 95 t.Errorf("no errors; want error in {%v}", errors) 96 } 97 98 if want != "" && got != want { 99 t.Errorf(`string: got %+q; want %+q`, got, want) 100 } 101 }) 102 } 103 104 var unescapeRE = regexp.MustCompile(`\\u([0-9a-zA-Z]{4})`) 105 106 func unescape(s string) string { 107 return unescapeRE.ReplaceAllStringFunc(s, func(v string) string { 108 var d [2]byte 109 hex.Decode(d[:], []byte(v[2:])) 110 return string(rune(d[0])<<8 | rune(d[1])) 111 }) 112 } 113 114 func BenchmarkProfile(b *testing.B) { 115 for i := 0; i < b.N; i++ { 116 Lookup.ToASCII("www.yahoogle.com") 117 } 118 }