Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var ninjaParseTestCases = []struct { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 23 | input string |
| 24 | vars []string |
| 25 | strs []string |
| 26 | err string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 27 | }{ |
| 28 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 29 | input: "abc def $ghi jkl", |
| 30 | vars: []string{"ghi"}, |
| 31 | strs: []string{"abc def ", " jkl"}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 32 | }, |
| 33 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 34 | input: "abc def $ghi$jkl", |
| 35 | vars: []string{"ghi", "jkl"}, |
| 36 | strs: []string{"abc def ", "", ""}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 37 | }, |
| 38 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 39 | input: "foo $012_-345xyz_! bar", |
| 40 | vars: []string{"012_-345xyz_"}, |
| 41 | strs: []string{"foo ", "! bar"}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 42 | }, |
| 43 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 44 | input: "foo ${012_-345xyz_} bar", |
| 45 | vars: []string{"012_-345xyz_"}, |
| 46 | strs: []string{"foo ", " bar"}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 47 | }, |
| 48 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 49 | input: "foo ${012_-345xyz_} bar", |
| 50 | vars: []string{"012_-345xyz_"}, |
| 51 | strs: []string{"foo ", " bar"}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 52 | }, |
| 53 | { |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 54 | input: "foo $$ bar", |
| 55 | vars: nil, |
| 56 | strs: []string{"foo $$ bar"}, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 57 | }, |
| 58 | { |
Colin Cross | b247893 | 2015-04-14 16:10:21 -0700 | [diff] [blame] | 59 | input: "$foo${bar}", |
| 60 | vars: []string{"foo", "bar"}, |
Colin Cross | 63d5d4d | 2015-04-20 16:41:55 -0700 | [diff] [blame] | 61 | strs: []string{"", "", ""}, |
Colin Cross | b247893 | 2015-04-14 16:10:21 -0700 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | input: "$foo$$", |
| 65 | vars: []string{"foo"}, |
Colin Cross | 63d5d4d | 2015-04-20 16:41:55 -0700 | [diff] [blame] | 66 | strs: []string{"", "$$"}, |
Colin Cross | b247893 | 2015-04-14 16:10:21 -0700 | [diff] [blame] | 67 | }, |
| 68 | { |
Colin Cross | 8c1c6c0 | 2015-04-14 16:28:51 -0700 | [diff] [blame] | 69 | input: "foo bar", |
| 70 | vars: nil, |
| 71 | strs: []string{"foo bar"}, |
| 72 | }, |
| 73 | { |
Colin Cross | 8de48af | 2017-05-09 10:03:00 -0700 | [diff] [blame] | 74 | input: " foo ", |
| 75 | vars: nil, |
| 76 | strs: []string{"$ foo "}, |
| 77 | }, |
| 78 | { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 79 | input: "foo $ bar", |
| 80 | err: "invalid character after '$' at byte offset 5", |
| 81 | }, |
| 82 | { |
| 83 | input: "foo $", |
| 84 | err: "unexpected end of string after '$'", |
| 85 | }, |
| 86 | { |
| 87 | input: "foo ${} bar", |
| 88 | err: "empty variable name at byte offset 6", |
| 89 | }, |
| 90 | { |
| 91 | input: "foo ${abc!} bar", |
| 92 | err: "invalid character in variable name at byte offset 9", |
| 93 | }, |
| 94 | { |
| 95 | input: "foo ${abc", |
| 96 | err: "unexpected end of string in variable name", |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | func TestParseNinjaString(t *testing.T) { |
| 101 | for _, testCase := range ninjaParseTestCases { |
| 102 | scope := newLocalScope(nil, "namespace") |
Colin Cross | 8c1c6c0 | 2015-04-14 16:28:51 -0700 | [diff] [blame] | 103 | expectedVars := []Variable{} |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 104 | for _, varName := range testCase.vars { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 105 | v, err := scope.LookupVariable(varName) |
| 106 | if err != nil { |
| 107 | v, err = scope.AddLocalVariable(varName, "") |
| 108 | if err != nil { |
| 109 | t.Fatalf("error creating scope: %s", err) |
| 110 | } |
| 111 | } |
| 112 | expectedVars = append(expectedVars, v) |
| 113 | } |
| 114 | |
| 115 | output, err := parseNinjaString(scope, testCase.input) |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 116 | if err == nil { |
| 117 | if !reflect.DeepEqual(output.variables, expectedVars) { |
| 118 | t.Errorf("incorrect variable list:") |
| 119 | t.Errorf(" input: %q", testCase.input) |
Colin Cross | 8c1c6c0 | 2015-04-14 16:28:51 -0700 | [diff] [blame] | 120 | t.Errorf(" expected: %#v", expectedVars) |
Jamie Gennis | 30d8a3a | 2014-10-11 10:03:17 -0700 | [diff] [blame] | 121 | t.Errorf(" got: %#v", output.variables) |
| 122 | } |
| 123 | if !reflect.DeepEqual(output.strings, testCase.strs) { |
| 124 | t.Errorf("incorrect string list:") |
| 125 | t.Errorf(" input: %q", testCase.input) |
| 126 | t.Errorf(" expected: %#v", testCase.strs) |
| 127 | t.Errorf(" got: %#v", output.strings) |
| 128 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 129 | } |
| 130 | var errStr string |
| 131 | if err != nil { |
| 132 | errStr = err.Error() |
| 133 | } |
| 134 | if err != nil && err.Error() != testCase.err { |
| 135 | t.Errorf("unexpected error:") |
| 136 | t.Errorf(" input: %q", testCase.input) |
| 137 | t.Errorf(" expected: %q", testCase.err) |
| 138 | t.Errorf(" got: %q", errStr) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestParseNinjaStringWithImportedVar(t *testing.T) { |
| 144 | ImpVar := &staticVariable{name_: "ImpVar"} |
| 145 | impScope := newScope(nil) |
| 146 | impScope.AddVariable(ImpVar) |
| 147 | scope := newScope(nil) |
| 148 | scope.AddImport("impPkg", impScope) |
| 149 | |
| 150 | input := "abc def ${impPkg.ImpVar} ghi" |
| 151 | output, err := parseNinjaString(scope, input) |
| 152 | if err != nil { |
| 153 | t.Fatalf("unexpected error: %s", err) |
| 154 | } |
| 155 | |
| 156 | expect := []Variable{ImpVar} |
| 157 | if !reflect.DeepEqual(output.variables, expect) { |
| 158 | t.Errorf("incorrect output:") |
| 159 | t.Errorf(" input: %q", input) |
| 160 | t.Errorf(" expected: %#v", expect) |
| 161 | t.Errorf(" got: %#v", output) |
| 162 | } |
| 163 | } |