You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
2.7 KiB

  1. package ucd
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. const file = `
  7. # Comments should be skipped
  8. # rune; bool; uint; int; float; runes; # Y
  9. 0..0005; Y; 0; 2; -5.25 ; 0 1 2 3 4 5;
  10. 6..0007; Yes ; 6; 1; -4.25 ; 0006 0007;
  11. 8; T ; 8 ; 0 ;-3.25 ;;# T
  12. 9; True ;9 ; -1;-2.25 ; 0009;
  13. # more comments to be ignored
  14. @Part0
  15. A; N; 10 ; -2; -1.25; ;# N
  16. B; No; 11 ; -3; -0.25;
  17. C; False;12; -4; 0.75;
  18. D; ;13;-5;1.75;
  19. @Part1 # Another part.
  20. # We test part comments get removed by not commenting the next line.
  21. E..10FFFF; F; 14 ; -6; 2.75;
  22. `
  23. var want = []struct {
  24. start, end rune
  25. }{
  26. {0x00, 0x05},
  27. {0x06, 0x07},
  28. {0x08, 0x08},
  29. {0x09, 0x09},
  30. {0x0A, 0x0A},
  31. {0x0B, 0x0B},
  32. {0x0C, 0x0C},
  33. {0x0D, 0x0D},
  34. {0x0E, 0x10FFFF},
  35. }
  36. func TestGetters(t *testing.T) {
  37. parts := [][2]string{
  38. {"Part0", ""},
  39. {"Part1", "Another part."},
  40. }
  41. handler := func(p *Parser) {
  42. if len(parts) == 0 {
  43. t.Error("Part handler invoked too many times.")
  44. return
  45. }
  46. want := parts[0]
  47. parts = parts[1:]
  48. if got0, got1 := p.String(0), p.Comment(); got0 != want[0] || got1 != want[1] {
  49. t.Errorf(`part: got %q, %q; want %q"`, got0, got1, want)
  50. }
  51. }
  52. p := New(strings.NewReader(file), KeepRanges, Part(handler))
  53. for i := 0; p.Next(); i++ {
  54. start, end := p.Range(0)
  55. w := want[i]
  56. if start != w.start || end != w.end {
  57. t.Fatalf("%d:Range(0); got %#x..%#x; want %#x..%#x", i, start, end, w.start, w.end)
  58. }
  59. if w.start == w.end && p.Rune(0) != w.start {
  60. t.Errorf("%d:Range(0).start: got %U; want %U", i, p.Rune(0), w.start)
  61. }
  62. if got, want := p.Bool(1), w.start <= 9; got != want {
  63. t.Errorf("%d:Bool(1): got %v; want %v", i, got, want)
  64. }
  65. if got := p.Rune(4); got != 0 || p.Err() == nil {
  66. t.Errorf("%d:Rune(%q): got no error; want error", i, p.String(1))
  67. }
  68. p.err = nil
  69. if got := p.Uint(2); rune(got) != start {
  70. t.Errorf("%d:Uint(2): got %v; want %v", i, got, start)
  71. }
  72. if got, want := p.Int(3), 2-i; got != want {
  73. t.Errorf("%d:Int(3): got %v; want %v", i, got, want)
  74. }
  75. if got, want := p.Float(4), -5.25+float64(i); got != want {
  76. t.Errorf("%d:Int(3): got %v; want %v", i, got, want)
  77. }
  78. if got := p.Runes(5); got == nil {
  79. if p.String(5) != "" {
  80. t.Errorf("%d:Runes(5): expected non-empty list", i)
  81. }
  82. } else {
  83. if got[0] != start || got[len(got)-1] != end {
  84. t.Errorf("%d:Runes(5): got %#x; want %#x..%#x", i, got, start, end)
  85. }
  86. }
  87. if got := p.Comment(); got != "" && got != p.String(1) {
  88. t.Errorf("%d:Comment(): got %v; want %v", i, got, p.String(1))
  89. }
  90. }
  91. if err := p.Err(); err != nil {
  92. t.Errorf("Parser error: %v", err)
  93. }
  94. if len(parts) != 0 {
  95. t.Errorf("expected %d more invocations of part handler", len(parts))
  96. }
  97. }