parser_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package cascadia
  2. import (
  3. "testing"
  4. )
  5. var identifierTests = map[string]string{
  6. "x": "x",
  7. "96": "",
  8. "-x": "-x",
  9. `r\e9 sumé`: "résumé",
  10. `a\"b`: `a"b`,
  11. }
  12. func TestParseIdentifier(t *testing.T) {
  13. for source, want := range identifierTests {
  14. p := &parser{s: source}
  15. got, err := p.parseIdentifier()
  16. if err != nil {
  17. if want == "" {
  18. // It was supposed to be an error.
  19. continue
  20. }
  21. t.Errorf("parsing %q: got error (%s), want %q", source, err, want)
  22. continue
  23. }
  24. if want == "" {
  25. if err == nil {
  26. t.Errorf("parsing %q: got %q, want error", source, got)
  27. }
  28. continue
  29. }
  30. if p.i < len(source) {
  31. t.Errorf("parsing %q: %d bytes left over", source, len(source)-p.i)
  32. continue
  33. }
  34. if got != want {
  35. t.Errorf("parsing %q: got %q, want %q", source, got, want)
  36. }
  37. }
  38. }
  39. var stringTests = map[string]string{
  40. `"x"`: "x",
  41. `'x'`: "x",
  42. `'x`: "",
  43. "'x\\\r\nx'": "xx",
  44. `"r\e9 sumé"`: "résumé",
  45. `"a\"b"`: `a"b`,
  46. }
  47. func TestParseString(t *testing.T) {
  48. for source, want := range stringTests {
  49. p := &parser{s: source}
  50. got, err := p.parseString()
  51. if err != nil {
  52. if want == "" {
  53. // It was supposed to be an error.
  54. continue
  55. }
  56. t.Errorf("parsing %q: got error (%s), want %q", source, err, want)
  57. continue
  58. }
  59. if want == "" {
  60. if err == nil {
  61. t.Errorf("parsing %q: got %q, want error", source, got)
  62. }
  63. continue
  64. }
  65. if p.i < len(source) {
  66. t.Errorf("parsing %q: %d bytes left over", source, len(source)-p.i)
  67. continue
  68. }
  69. if got != want {
  70. t.Errorf("parsing %q: got %q, want %q", source, got, want)
  71. }
  72. }
  73. }