diff options
author | Liz Kammer <eakammer@google.com> | 2021-04-09 16:17:05 -0400 |
---|---|---|
committer | Liz Kammer <eakammer@google.com> | 2021-04-12 16:23:53 -0400 |
commit | fe23bf3eb0a8b767c8d3bd720821f9cb9a689dd1 (patch) | |
tree | 343cd8b922b1325f64d4eb63ecefc1360c171f8c /bazel | |
parent | b71794d8e6f83cab2af25a4807b1dd10ac2adda9 (diff) | |
download | soong-fe23bf3eb0a8b767c8d3bd720821f9cb9a689dd1.tar.gz |
Add tests for request_type ParseResult functions.
Test: go test request_type_test
Test: bp2build generate & sync; mixed build libc
Change-Id: Id0b813e9de4d02d8625e42549999659ccb005c6d
Diffstat (limited to 'bazel')
-rw-r--r-- | bazel/cquery/request_type.go | 10 | ||||
-rw-r--r-- | bazel/cquery/request_type_test.go | 89 |
2 files changed, 96 insertions, 3 deletions
diff --git a/bazel/cquery/request_type.go b/bazel/cquery/request_type.go index 7bd12a8c2..80491089d 100644 --- a/bazel/cquery/request_type.go +++ b/bazel/cquery/request_type.go @@ -1,6 +1,7 @@ package cquery import ( + "fmt" "strings" ) @@ -39,7 +40,7 @@ func (g getOutputFilesRequestType) StarlarkFunctionBody() string { // The given rawString must correspond to the string output which was created by evaluating the // Starlark given in StarlarkFunctionBody. func (g getOutputFilesRequestType) ParseResult(rawString string) []string { - return strings.Split(rawString, ", ") + return splitOrEmpty(rawString, ", ") } type getCcInfoType struct{} @@ -85,11 +86,14 @@ return "|".join([", ".join(r) for r in returns])` // ParseResult returns a value obtained by parsing the result of the request's Starlark function. // The given rawString must correspond to the string output which was created by evaluating the // Starlark given in StarlarkFunctionBody. -func (g getCcInfoType) ParseResult(rawString string) CcInfo { +func (g getCcInfoType) ParseResult(rawString string) (CcInfo, error) { var outputFiles []string var ccObjects []string splitString := strings.Split(rawString, "|") + if expectedLen := 3; len(splitString) != expectedLen { + return CcInfo{}, fmt.Errorf("Expected %d items, got %q", expectedLen, splitString) + } outputFilesString := splitString[0] ccStaticLibrariesString := splitString[1] ccObjectsString := splitString[2] @@ -100,7 +104,7 @@ func (g getCcInfoType) ParseResult(rawString string) CcInfo { OutputFiles: outputFiles, CcObjectFiles: ccObjects, CcStaticLibraryFiles: ccStaticLibraries, - } + }, nil } // splitOrEmpty is a modification of strings.Split() that returns an empty list diff --git a/bazel/cquery/request_type_test.go b/bazel/cquery/request_type_test.go new file mode 100644 index 000000000..56e03e26b --- /dev/null +++ b/bazel/cquery/request_type_test.go @@ -0,0 +1,89 @@ +package cquery + +import ( + "fmt" + "reflect" + "testing" +) + +func TestGetOutputFilesParseResults(t *testing.T) { + testCases := []struct { + description string + input string + expectedOutput []string + }{ + { + description: "no result", + input: "", + expectedOutput: []string{}, + }, + { + description: "one result", + input: "test", + expectedOutput: []string{"test"}, + }, + { + description: "splits on comma with space", + input: "foo, bar", + expectedOutput: []string{"foo", "bar"}, + }, + } + for _, tc := range testCases { + actualOutput := GetOutputFiles.ParseResult(tc.input) + if !reflect.DeepEqual(tc.expectedOutput, actualOutput) { + t.Errorf("%q: expected %#v != actual %#v", tc.description, tc.expectedOutput, actualOutput) + } + } +} + +func TestGetCcInfoParseResults(t *testing.T) { + testCases := []struct { + description string + input string + expectedOutput CcInfo + expectedErrorMessage string + }{ + { + description: "no result", + input: "||", + expectedOutput: CcInfo{ + OutputFiles: []string{}, + CcObjectFiles: []string{}, + CcStaticLibraryFiles: []string{}, + }, + }, + { + description: "only output", + input: "test||", + expectedOutput: CcInfo{ + OutputFiles: []string{"test"}, + CcObjectFiles: []string{}, + CcStaticLibraryFiles: []string{}, + }, + }, + { + description: "all items set", + input: "out1, out2|static_lib1, static_lib2|object1, object2", + expectedOutput: CcInfo{ + OutputFiles: []string{"out1", "out2"}, + CcObjectFiles: []string{"object1", "object2"}, + CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"}, + }, + }, + { + description: "too few result splits", + input: "|", + expectedOutput: CcInfo{}, + expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 3, []string{"", ""}), + }, + } + for _, tc := range testCases { + actualOutput, err := GetCcInfo.ParseResult(tc.input) + if (err == nil && tc.expectedErrorMessage != "") || + (err != nil && err.Error() != tc.expectedErrorMessage) { + t.Errorf("%q: expected Error %s, got %s", tc.description, tc.expectedErrorMessage, err) + } else if err == nil && !reflect.DeepEqual(tc.expectedOutput, actualOutput) { + t.Errorf("%q: expected %#v != actual %#v", tc.description, tc.expectedOutput, actualOutput) + } + } +} |