1// Copyright 2021 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.
14package android
15
16import "testing"
17
18func TestConvertAllModulesInPackage(t *testing.T) {
19	testCases := []struct {
20		prefixes   Bp2BuildConfig
21		packageDir string
22	}{
23		{
24			prefixes: Bp2BuildConfig{
25				"a": Bp2BuildDefaultTrueRecursively,
26			},
27			packageDir: "a",
28		},
29		{
30			prefixes: Bp2BuildConfig{
31				"a/b": Bp2BuildDefaultTrueRecursively,
32			},
33			packageDir: "a/b",
34		},
35		{
36			prefixes: Bp2BuildConfig{
37				"a/b":   Bp2BuildDefaultTrueRecursively,
38				"a/b/c": Bp2BuildDefaultTrueRecursively,
39			},
40			packageDir: "a/b",
41		},
42		{
43			prefixes: Bp2BuildConfig{
44				"a":     Bp2BuildDefaultTrueRecursively,
45				"d/e/f": Bp2BuildDefaultTrueRecursively,
46			},
47			packageDir: "a/b",
48		},
49		{
50			prefixes: Bp2BuildConfig{
51				"a":     Bp2BuildDefaultFalse,
52				"a/b":   Bp2BuildDefaultTrueRecursively,
53				"a/b/c": Bp2BuildDefaultFalse,
54			},
55			packageDir: "a/b",
56		},
57		{
58			prefixes: Bp2BuildConfig{
59				"a":     Bp2BuildDefaultTrueRecursively,
60				"a/b":   Bp2BuildDefaultFalse,
61				"a/b/c": Bp2BuildDefaultTrueRecursively,
62			},
63			packageDir: "a",
64		},
65	}
66
67	for _, test := range testCases {
68		if !bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes) {
69			t.Errorf("Expected to convert all modules in %s based on %v, but failed.", test.packageDir, test.prefixes)
70		}
71	}
72}
73
74func TestModuleOptIn(t *testing.T) {
75	testCases := []struct {
76		prefixes   Bp2BuildConfig
77		packageDir string
78	}{
79		{
80			prefixes: Bp2BuildConfig{
81				"a/b": Bp2BuildDefaultFalse,
82			},
83			packageDir: "a/b",
84		},
85		{
86			prefixes: Bp2BuildConfig{
87				"a":   Bp2BuildDefaultFalse,
88				"a/b": Bp2BuildDefaultTrueRecursively,
89			},
90			packageDir: "a",
91		},
92		{
93			prefixes: Bp2BuildConfig{
94				"a/b": Bp2BuildDefaultTrueRecursively,
95			},
96			packageDir: "a", // opt-in by default
97		},
98		{
99			prefixes: Bp2BuildConfig{
100				"a/b/c": Bp2BuildDefaultTrueRecursively,
101			},
102			packageDir: "a/b",
103		},
104		{
105			prefixes: Bp2BuildConfig{
106				"a":     Bp2BuildDefaultTrueRecursively,
107				"d/e/f": Bp2BuildDefaultTrueRecursively,
108			},
109			packageDir: "foo/bar",
110		},
111		{
112			prefixes: Bp2BuildConfig{
113				"a":     Bp2BuildDefaultTrueRecursively,
114				"a/b":   Bp2BuildDefaultFalse,
115				"a/b/c": Bp2BuildDefaultTrueRecursively,
116			},
117			packageDir: "a/b",
118		},
119		{
120			prefixes: Bp2BuildConfig{
121				"a":     Bp2BuildDefaultFalse,
122				"a/b":   Bp2BuildDefaultTrueRecursively,
123				"a/b/c": Bp2BuildDefaultFalse,
124			},
125			packageDir: "a",
126		},
127	}
128
129	for _, test := range testCases {
130		if bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes) {
131			t.Errorf("Expected to allow module opt-in in %s based on %v, but failed.", test.packageDir, test.prefixes)
132		}
133	}
134}
135