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