parser_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package parser
  15. import (
  16. "bytes"
  17. "testing"
  18. )
  19. var parserTestCases = []struct {
  20. name string
  21. in string
  22. out []Node
  23. }{
  24. {
  25. name: "Escaped $",
  26. in: `a$$ b: c`,
  27. out: []Node{
  28. &Rule{
  29. Target: SimpleMakeString("a$ b", NoPos),
  30. Prerequisites: SimpleMakeString("c", NoPos),
  31. },
  32. },
  33. },
  34. }
  35. func TestParse(t *testing.T) {
  36. for _, test := range parserTestCases {
  37. t.Run(test.name, func(t *testing.T) {
  38. p := NewParser(test.name, bytes.NewBufferString(test.in))
  39. got, errs := p.Parse()
  40. if len(errs) != 0 {
  41. t.Fatalf("Unexpected errors while parsing: %v", errs)
  42. }
  43. if len(got) != len(test.out) {
  44. t.Fatalf("length mismatch, expected %d nodes, got %d", len(test.out), len(got))
  45. }
  46. for i := range got {
  47. if got[i].Dump() != test.out[i].Dump() {
  48. t.Errorf("incorrect node %d:\nexpected: %#v (%s)\n got: %#v (%s)",
  49. i, test.out[i], test.out[i].Dump(), got[i], got[i].Dump())
  50. }
  51. }
  52. })
  53. }
  54. }