platform_build_blueprint/blueprint/ninja_strings_test.go
Jamie Gennis 1bc967ed43 Initial Blueprint commit.
Blueprint is a build system component that reads Blueprints files defining
modules to be built, and generates a Ninja build manifest that can be used to
perform all the build actions.  It does not dictate or implement much build
policy itself, but rather provides a framework to ease the process of defining
build logic in Go.

The "blueprint" and "blueprint/parser" Go packages contain the functionality
for reading Blueprint files and invoking build logic functions defined in other
Go packages.

The "blueprint/bootstrap" Go package contains just enough build logic to build
a binary that includes Blueprint and any pure-Go (i.e. no cgo) build logic
defined in external Go packages.  This can be used to create a minimal Ninja
file that's capable of bootstrapping a Blueprint-based build system from
source.

The "blueprint/bootstrap/minibp" Go package contains code for a minimal binary
that includes the build logic defined in the "blueprint/bootstrap" package.
This binary can then create the Ninja file for the bootstrapping process.

Change-Id: I8d8390042372a72d225785cda738525001b009f1
2014-06-04 14:23:32 -07:00

114 lines
2.5 KiB
Go

package blueprint
import (
"reflect"
"testing"
)
var ninjaParseTestCases = []struct {
input string
output []string
err string
}{
{
input: "abc def $ghi jkl",
output: []string{"ghi"},
},
{
input: "abc def $ghi$jkl",
output: []string{"ghi", "jkl"},
},
{
input: "foo $012_-345xyz_! bar",
output: []string{"012_-345xyz_"},
},
{
input: "foo ${012_-345xyz_} bar",
output: []string{"012_-345xyz_"},
},
{
input: "foo ${012_-345xyz_} bar",
output: []string{"012_-345xyz_"},
},
{
input: "foo $$ bar",
output: nil,
},
{
input: "foo $ bar",
err: "invalid character after '$' at byte offset 5",
},
{
input: "foo $",
err: "unexpected end of string after '$'",
},
{
input: "foo ${} bar",
err: "empty variable name at byte offset 6",
},
{
input: "foo ${abc!} bar",
err: "invalid character in variable name at byte offset 9",
},
{
input: "foo ${abc",
err: "unexpected end of string in variable name",
},
}
func TestParseNinjaString(t *testing.T) {
for _, testCase := range ninjaParseTestCases {
scope := newLocalScope(nil, "namespace")
var expectedVars []Variable
for _, varName := range testCase.output {
v, err := scope.LookupVariable(varName)
if err != nil {
v, err = scope.AddLocalVariable(varName, "")
if err != nil {
t.Fatalf("error creating scope: %s", err)
}
}
expectedVars = append(expectedVars, v)
}
output, err := parseNinjaString(scope, testCase.input)
if err == nil && !reflect.DeepEqual(output.variables, expectedVars) {
t.Errorf("incorrect output:")
t.Errorf(" input: %q", testCase.input)
t.Errorf(" expected: %#v", testCase.output)
t.Errorf(" got: %#v", output)
}
var errStr string
if err != nil {
errStr = err.Error()
}
if err != nil && err.Error() != testCase.err {
t.Errorf("unexpected error:")
t.Errorf(" input: %q", testCase.input)
t.Errorf(" expected: %q", testCase.err)
t.Errorf(" got: %q", errStr)
}
}
}
func TestParseNinjaStringWithImportedVar(t *testing.T) {
ImpVar := &staticVariable{name_: "ImpVar"}
impScope := newScope(nil)
impScope.AddVariable(ImpVar)
scope := newScope(nil)
scope.AddImport("impPkg", impScope)
input := "abc def ${impPkg.ImpVar} ghi"
output, err := parseNinjaString(scope, input)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expect := []Variable{ImpVar}
if !reflect.DeepEqual(output.variables, expect) {
t.Errorf("incorrect output:")
t.Errorf(" input: %q", input)
t.Errorf(" expected: %#v", expect)
t.Errorf(" got: %#v", output)
}
}