blob: 9efebf8e1db0ad8a4a4b044c0ed98b7783a91749 [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 },
Cole Fauste309a912022-03-16 13:42:34 -070037 {
38 name: "Simple warning",
39 in: `$(warning A warning)`,
40 out: []Node{
41 &Variable{
42 Name: SimpleMakeString("warning A warning", NoPos),
43 },
44 },
45 },
46 {
47 name: "Warning with #",
48 in: `$(warning # A warning)`,
49 out: []Node{
50 &Variable{
51 Name: SimpleMakeString("warning # A warning", NoPos),
52 },
53 },
54 },
55 {
56 name: "Findstring with #",
57 in: `$(findstring x,x a #)`,
58 out: []Node{
59 &Variable{
60 Name: SimpleMakeString("findstring x,x a #", NoPos),
61 },
62 },
63 },
64 {
65 name: "If statement",
66 in: `ifeq (a,b) # comment
67endif`,
68 out: []Node{
69 &Directive{
70 NamePos: NoPos,
71 Name: "ifeq",
72 Args: SimpleMakeString("(a,b) ", NoPos),
73 EndPos: NoPos,
74 },
75 &Comment{
76 CommentPos: NoPos,
77 Comment: " comment",
78 },
79 &Directive{
80 NamePos: NoPos,
81 Name: "endif",
82 Args: SimpleMakeString("", NoPos),
83 EndPos: NoPos,
84 },
85 },
86 },
Dan Willemsen43398532018-02-21 02:10:29 -080087}
88
89func TestParse(t *testing.T) {
Dan Willemsen43398532018-02-21 02:10:29 -080090 for _, test := range parserTestCases {
91 t.Run(test.name, func(t *testing.T) {
92 p := NewParser(test.name, bytes.NewBufferString(test.in))
93 got, errs := p.Parse()
94
95 if len(errs) != 0 {
96 t.Fatalf("Unexpected errors while parsing: %v", errs)
97 }
98
99 if len(got) != len(test.out) {
100 t.Fatalf("length mismatch, expected %d nodes, got %d", len(test.out), len(got))
101 }
102
103 for i := range got {
104 if got[i].Dump() != test.out[i].Dump() {
105 t.Errorf("incorrect node %d:\nexpected: %#v (%s)\n got: %#v (%s)",
106 i, test.out[i], test.out[i].Dump(), got[i], got[i].Dump())
107 }
108 }
109 })
110 }
111}