idna_test.go 2.8 KB

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