conformance_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 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. "strings"
  8. "testing"
  9. "golang.org/x/text/internal/gen"
  10. "golang.org/x/text/internal/testtext"
  11. "golang.org/x/text/internal/ucd"
  12. )
  13. func TestConformance(t *testing.T) {
  14. testtext.SkipIfNotLong(t)
  15. r := gen.OpenUnicodeFile("idna", "10.0.0", "IdnaTest.txt")
  16. defer r.Close()
  17. section := "main"
  18. p := ucd.New(r)
  19. transitional := New(Transitional(true), VerifyDNSLength(true), BidiRule(), MapForLookup())
  20. nonTransitional := New(VerifyDNSLength(true), BidiRule(), MapForLookup())
  21. for p.Next() {
  22. // What to test
  23. profiles := []*Profile{}
  24. switch p.String(0) {
  25. case "T":
  26. profiles = append(profiles, transitional)
  27. case "N":
  28. profiles = append(profiles, nonTransitional)
  29. case "B":
  30. profiles = append(profiles, transitional)
  31. profiles = append(profiles, nonTransitional)
  32. }
  33. src := unescape(p.String(1))
  34. wantToUnicode := unescape(p.String(2))
  35. if wantToUnicode == "" {
  36. wantToUnicode = src
  37. }
  38. wantToASCII := unescape(p.String(3))
  39. if wantToASCII == "" {
  40. wantToASCII = wantToUnicode
  41. }
  42. wantErrToUnicode := ""
  43. if strings.HasPrefix(wantToUnicode, "[") {
  44. wantErrToUnicode = wantToUnicode
  45. wantToUnicode = ""
  46. }
  47. wantErrToASCII := ""
  48. if strings.HasPrefix(wantToASCII, "[") {
  49. wantErrToASCII = wantToASCII
  50. wantToASCII = ""
  51. }
  52. // TODO: also do IDNA tests.
  53. // invalidInIDNA2008 := p.String(4) == "NV8"
  54. for _, p := range profiles {
  55. name := fmt.Sprintf("%s:%s", section, p)
  56. doTest(t, p.ToUnicode, name+":ToUnicode", src, wantToUnicode, wantErrToUnicode)
  57. doTest(t, p.ToASCII, name+":ToASCII", src, wantToASCII, wantErrToASCII)
  58. }
  59. }
  60. }