runenames_test.go 1004 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 runenames
  5. import (
  6. "strings"
  7. "testing"
  8. "unicode"
  9. "golang.org/x/text/internal/gen"
  10. "golang.org/x/text/internal/testtext"
  11. "golang.org/x/text/internal/ucd"
  12. )
  13. func TestName(t *testing.T) {
  14. testtext.SkipIfNotLong(t)
  15. wants := make([]string, 1+unicode.MaxRune)
  16. ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
  17. wants[p.Rune(0)] = getName(p)
  18. })
  19. nErrors := 0
  20. for r, want := range wants {
  21. got := Name(rune(r))
  22. if got != want {
  23. t.Errorf("r=%#08x: got %q, want %q", r, got, want)
  24. nErrors++
  25. if nErrors == 100 {
  26. t.Fatal("too many errors")
  27. }
  28. }
  29. }
  30. }
  31. // Copied from gen.go.
  32. func getName(p *ucd.Parser) string {
  33. s := p.String(ucd.Name)
  34. if s == "" {
  35. return ""
  36. }
  37. if s[0] == '<' {
  38. const first = ", First>"
  39. if i := strings.Index(s, first); i >= 0 {
  40. s = s[:i] + ">"
  41. }
  42. }
  43. return s
  44. }