Home | History | Annotate | Download | only in blueprint
      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 
     15 package blueprint
     16 
     17 import (
     18 	"reflect"
     19 	"testing"
     20 )
     21 
     22 var ninjaParseTestCases = []struct {
     23 	input string
     24 	vars  []string
     25 	strs  []string
     26 	err   string
     27 }{
     28 	{
     29 		input: "abc def $ghi jkl",
     30 		vars:  []string{"ghi"},
     31 		strs:  []string{"abc def ", " jkl"},
     32 	},
     33 	{
     34 		input: "abc def $ghi$jkl",
     35 		vars:  []string{"ghi", "jkl"},
     36 		strs:  []string{"abc def ", "", ""},
     37 	},
     38 	{
     39 		input: "foo $012_-345xyz_! bar",
     40 		vars:  []string{"012_-345xyz_"},
     41 		strs:  []string{"foo ", "! bar"},
     42 	},
     43 	{
     44 		input: "foo ${012_-345xyz_} bar",
     45 		vars:  []string{"012_-345xyz_"},
     46 		strs:  []string{"foo ", " bar"},
     47 	},
     48 	{
     49 		input: "foo ${012_-345xyz_} bar",
     50 		vars:  []string{"012_-345xyz_"},
     51 		strs:  []string{"foo ", " bar"},
     52 	},
     53 	{
     54 		input: "foo $$ bar",
     55 		vars:  nil,
     56 		strs:  []string{"foo $$ bar"},
     57 	},
     58 	{
     59 		input: "$foo${bar}",
     60 		vars:  []string{"foo", "bar"},
     61 		strs:  []string{"", "", ""},
     62 	},
     63 	{
     64 		input: "$foo$$",
     65 		vars:  []string{"foo"},
     66 		strs:  []string{"", "$$"},
     67 	},
     68 	{
     69 		input: "foo bar",
     70 		vars:  nil,
     71 		strs:  []string{"foo bar"},
     72 	},
     73 	{
     74 		input: " foo ",
     75 		vars:  nil,
     76 		strs:  []string{"$ foo "},
     77 	},
     78 	{
     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")
    103 		expectedVars := []Variable{}
    104 		for _, varName := range testCase.vars {
    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)
    116 		if err == nil {
    117 			if !reflect.DeepEqual(output.variables, expectedVars) {
    118 				t.Errorf("incorrect variable list:")
    119 				t.Errorf("     input: %q", testCase.input)
    120 				t.Errorf("  expected: %#v", expectedVars)
    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 			}
    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 }
    164