Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 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 | |
| 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var 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 Faust | e309a91 | 2022-03-16 13:42:34 -0700 | [diff] [blame] | 37 | { |
| 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 |
| 67 | endif`, |
| 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 | }, |
Min Yun | 53ca8b2 | 2024-02-23 23:16:14 +0900 | [diff] [blame^] | 87 | { |
| 88 | name: "Blank line in rule's command", |
| 89 | in: `all: |
| 90 | echo first line |
| 91 | |
| 92 | echo second line`, |
| 93 | out: []Node{ |
| 94 | &Rule{ |
| 95 | Target: SimpleMakeString("all", NoPos), |
| 96 | RecipePos: NoPos, |
| 97 | Recipe: "echo first line\necho second line", |
| 98 | Prerequisites: SimpleMakeString("", NoPos), |
| 99 | }, |
| 100 | }, |
| 101 | }, |
| 102 | |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func TestParse(t *testing.T) { |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 106 | for _, test := range parserTestCases { |
| 107 | t.Run(test.name, func(t *testing.T) { |
| 108 | p := NewParser(test.name, bytes.NewBufferString(test.in)) |
| 109 | got, errs := p.Parse() |
| 110 | |
| 111 | if len(errs) != 0 { |
| 112 | t.Fatalf("Unexpected errors while parsing: %v", errs) |
| 113 | } |
| 114 | |
| 115 | if len(got) != len(test.out) { |
| 116 | t.Fatalf("length mismatch, expected %d nodes, got %d", len(test.out), len(got)) |
| 117 | } |
| 118 | |
| 119 | for i := range got { |
| 120 | if got[i].Dump() != test.out[i].Dump() { |
| 121 | t.Errorf("incorrect node %d:\nexpected: %#v (%s)\n got: %#v (%s)", |
| 122 | i, test.out[i], test.out[i].Dump(), got[i], got[i].Dump()) |
| 123 | } |
| 124 | } |
| 125 | }) |
| 126 | } |
| 127 | } |