blob: f562c29e881342849b8b6dd431ebeec52988f673 [file] [log] [blame]
Dan Willemsen43398532018-02-21 02:10:29 -08001// 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
15package parser
16
17import (
18 "bytes"
19 "testing"
20)
21
22var parserTestCases = []struct {
23 name string
24 in string
25 out []Node
26}{
27 {
28 name: "Escaped $",
29 in: `a$$ b: c`,
30 out: []Node{
31 &Rule{
32 Target: SimpleMakeString("a$ b", NoPos),
33 Prerequisites: SimpleMakeString("c", NoPos),
34 },
35 },
36 },
37}
38
39func TestParse(t *testing.T) {
40 for _, test := range parserTestCases {
41 t.Run(test.name, func(t *testing.T) {
42 p := NewParser(test.name, bytes.NewBufferString(test.in))
43 got, errs := p.Parse()
44
45 if len(errs) != 0 {
46 t.Fatalf("Unexpected errors while parsing: %v", errs)
47 }
48
49 if len(got) != len(test.out) {
50 t.Fatalf("length mismatch, expected %d nodes, got %d", len(test.out), len(got))
51 }
52
53 for i := range got {
54 if got[i].Dump() != test.out[i].Dump() {
55 t.Errorf("incorrect node %d:\nexpected: %#v (%s)\n got: %#v (%s)",
56 i, test.out[i], test.out[i].Dump(), got[i], got[i].Dump())
57 }
58 }
59 })
60 }
61}