export common bp2build testing functions

bp2buildTestCase
attrNameToString
runBp2BuildTestCase
makeBazelTargetNoRestrictions

The testing framework defined in the bp2build package can only be used
from within the package because many common testing functions are
private to the package. This prevents modules defined in Soong
plugins (e.g. system/tools/aidl/build) from testing bp2build conversions.

Test: go test ./bp2build
Change-Id: Ia867081327c5181d04687b13c4550e68e6a11f86
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 580bac4..3ee5096 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -53,16 +53,16 @@
 	return false
 }
 
-func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool {
+func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool {
 	t.Helper()
-	if tc.expectedErr != nil {
+	if tc.ExpectedErr != nil {
 		// Rely on checkErrors, as this test case is expected to have an error.
 		return false
 	}
 
 	if len(errs) > 0 {
 		for _, err := range errs {
-			t.Errorf("%s: %s", tc.description, err)
+			t.Errorf("%s: %s", tc.Description, err)
 		}
 		return true
 	}
@@ -71,42 +71,42 @@
 	return false
 }
 
-func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
+func runBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) {
 	t.Helper()
-	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
+	RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
 }
 
-type bp2buildTestCase struct {
-	description                string
-	moduleTypeUnderTest        string
-	moduleTypeUnderTestFactory android.ModuleFactory
-	blueprint                  string
-	expectedBazelTargets       []string
-	filesystem                 map[string]string
-	dir                        string
+type Bp2buildTestCase struct {
+	Description                string
+	ModuleTypeUnderTest        string
+	ModuleTypeUnderTestFactory android.ModuleFactory
+	Blueprint                  string
+	ExpectedBazelTargets       []string
+	Filesystem                 map[string]string
+	Dir                        string
 	// An error with a string contained within the string of the expected error
-	expectedErr         error
-	unconvertedDepsMode unconvertedDepsMode
+	ExpectedErr         error
+	UnconvertedDepsMode unconvertedDepsMode
 }
 
-func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
+func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
 	t.Helper()
 	dir := "."
 	filesystem := make(map[string][]byte)
 	toParse := []string{
 		"Android.bp",
 	}
-	for f, content := range tc.filesystem {
+	for f, content := range tc.Filesystem {
 		if strings.HasSuffix(f, "Android.bp") {
 			toParse = append(toParse, f)
 		}
 		filesystem[f] = []byte(content)
 	}
-	config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
+	config := android.TestConfig(buildDir, nil, tc.Blueprint, filesystem)
 	ctx := android.NewTestContext(config)
 
 	registerModuleTypes(ctx)
-	ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
+	ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
 	ctx.RegisterBp2BuildConfig(bp2buildConfig)
 	ctx.RegisterForBazelConversion()
 
@@ -120,35 +120,35 @@
 	}
 
 	parseAndResolveErrs := append(parseErrs, resolveDepsErrs...)
-	if tc.expectedErr != nil && checkError(t, parseAndResolveErrs, tc.expectedErr) {
+	if tc.ExpectedErr != nil && checkError(t, parseAndResolveErrs, tc.ExpectedErr) {
 		return
 	}
 
 	checkDir := dir
-	if tc.dir != "" {
-		checkDir = tc.dir
+	if tc.Dir != "" {
+		checkDir = tc.Dir
 	}
 	codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
-	codegenCtx.unconvertedDepMode = tc.unconvertedDepsMode
+	codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode
 	bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
-	if tc.expectedErr != nil {
-		if checkError(t, errs, tc.expectedErr) {
+	if tc.ExpectedErr != nil {
+		if checkError(t, errs, tc.ExpectedErr) {
 			return
 		} else {
-			t.Errorf("Expected error: %q, got: %q and %q", tc.expectedErr, errs, parseAndResolveErrs)
+			t.Errorf("Expected error: %q, got: %q and %q", tc.ExpectedErr, errs, parseAndResolveErrs)
 		}
 	} else {
 		android.FailIfErrored(t, errs)
 	}
-	if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
+	if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount {
 		t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)",
-			tc.description, expectedCount, tc.expectedBazelTargets, actualCount, bazelTargets)
+			tc.Description, expectedCount, tc.ExpectedBazelTargets, actualCount, bazelTargets)
 	} else {
 		for i, target := range bazelTargets {
-			if w, g := tc.expectedBazelTargets[i], target.content; w != g {
+			if w, g := tc.ExpectedBazelTargets[i], target.content; w != g {
 				t.Errorf(
 					"%s: Expected generated Bazel target to be `%s`, got `%s`",
-					tc.description, w, g)
+					tc.Description, w, g)
 			}
 		}
 	}
@@ -391,10 +391,10 @@
 }`, typ, name)
 }
 
-type attrNameToString map[string]string
+type AttrNameToString map[string]string
 
-func (a attrNameToString) clone() attrNameToString {
-	newAttrs := make(attrNameToString, len(a))
+func (a AttrNameToString) clone() AttrNameToString {
+	newAttrs := make(AttrNameToString, len(a))
 	for k, v := range a {
 		newAttrs[k] = v
 	}
@@ -403,7 +403,7 @@
 
 // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
 // device specific, or independent of host/device.
-func makeBazelTargetHostOrDevice(typ, name string, attrs attrNameToString, hod android.HostOrDeviceSupported) string {
+func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
 	if _, ok := attrs["target_compatible_with"]; !ok {
 		switch hod {
 		case android.HostSupported:
@@ -426,15 +426,15 @@
 )`, typ, strings.Join(attrStrings, "\n"))
 }
 
-// makeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
+// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
 // target_compatible_with.  This is useful for module types like filegroup and genrule that arch not
 // arch variant
-func makeBazelTargetNoRestrictions(typ, name string, attrs attrNameToString) string {
+func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
 	return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
 }
 
 // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
 // as this is the most common default in Soong.
-func makeBazelTarget(typ, name string, attrs attrNameToString) string {
+func makeBazelTarget(typ, name string, attrs AttrNameToString) string {
 	return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
 }