diff options
author | Logan Chien <loganchien@google.com> | 2018-03-08 18:14:19 +0800 |
---|---|---|
committer | Logan Chien <loganchien@google.com> | 2018-03-16 17:45:25 +0800 |
commit | 3b33eaa1c90cdf1af7204affb8e30a387e57ee76 (patch) | |
tree | 4e7d026a6d45eaae6b5bb3cb55cdbf6f45148028 | |
parent | 070f60548581d2da66ad3fb91df5bd4d88cceb26 (diff) | |
download | build_soong-3b33eaa1c90cdf1af7204affb8e30a387e57ee76.tar.gz build_soong-3b33eaa1c90cdf1af7204affb8e30a387e57ee76.tar.bz2 build_soong-3b33eaa1c90cdf1af7204affb8e30a387e57ee76.zip |
Add unit tests for android/util.go
Bug: 62815515
Test: lunch aosp_sailfish-userdebug && make # runs soong unit tests
Merged-In: I34f06abdc78bfb28af3b5250491bbcddb7595c9b
Change-Id: I34f06abdc78bfb28af3b5250491bbcddb7595c9b
-rw-r--r-- | android/util_test.go | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/android/util_test.go b/android/util_test.go index 32f92b46..dc8b9080 100644 --- a/android/util_test.go +++ b/android/util_test.go @@ -118,3 +118,185 @@ func TestLastUniqueStrings(t *testing.T) { } } } + +func TestJoinWithPrefix(t *testing.T) { + testcases := []struct { + name string + input []string + expected string + }{ + { + name: "zero_inputs", + input: []string{}, + expected: "", + }, + { + name: "one_input", + input: []string{"a"}, + expected: "prefix:a", + }, + { + name: "two_inputs", + input: []string{"a", "b"}, + expected: "prefix:a prefix:b", + }, + } + + prefix := "prefix:" + + for _, testCase := range testcases { + t.Run(testCase.name, func(t *testing.T) { + out := JoinWithPrefix(testCase.input, prefix) + if out != testCase.expected { + t.Errorf("incorrect output:") + t.Errorf(" input: %#v", testCase.input) + t.Errorf(" prefix: %#v", prefix) + t.Errorf(" expected: %#v", testCase.expected) + t.Errorf(" got: %#v", out) + } + }) + } +} + +func TestIndexList(t *testing.T) { + input := []string{"a", "b", "c"} + + testcases := []struct { + key string + expected int + }{ + { + key: "a", + expected: 0, + }, + { + key: "b", + expected: 1, + }, + { + key: "c", + expected: 2, + }, + { + key: "X", + expected: -1, + }, + } + + for _, testCase := range testcases { + t.Run(testCase.key, func(t *testing.T) { + out := IndexList(testCase.key, input) + if out != testCase.expected { + t.Errorf("incorrect output:") + t.Errorf(" key: %#v", testCase.key) + t.Errorf(" input: %#v", input) + t.Errorf(" expected: %#v", testCase.expected) + t.Errorf(" got: %#v", out) + } + }) + } +} + +func TestInList(t *testing.T) { + input := []string{"a"} + + testcases := []struct { + key string + expected bool + }{ + { + key: "a", + expected: true, + }, + { + key: "X", + expected: false, + }, + } + + for _, testCase := range testcases { + t.Run(testCase.key, func(t *testing.T) { + out := InList(testCase.key, input) + if out != testCase.expected { + t.Errorf("incorrect output:") + t.Errorf(" key: %#v", testCase.key) + t.Errorf(" input: %#v", input) + t.Errorf(" expected: %#v", testCase.expected) + t.Errorf(" got: %#v", out) + } + }) + } +} + +func TestPrefixInList(t *testing.T) { + prefixes := []string{"a", "b"} + + testcases := []struct { + str string + expected bool + }{ + { + str: "a-example", + expected: true, + }, + { + str: "b-example", + expected: true, + }, + { + str: "X-example", + expected: false, + }, + } + + for _, testCase := range testcases { + t.Run(testCase.str, func(t *testing.T) { + out := PrefixInList(testCase.str, prefixes) + if out != testCase.expected { + t.Errorf("incorrect output:") + t.Errorf(" str: %#v", testCase.str) + t.Errorf(" prefixes: %#v", prefixes) + t.Errorf(" expected: %#v", testCase.expected) + t.Errorf(" got: %#v", out) + } + }) + } +} + +func TestFilterList(t *testing.T) { + input := []string{"a", "b", "c", "c", "b", "d", "a"} + filter := []string{"a", "c"} + remainder, filtered := FilterList(input, filter) + + expected := []string{"b", "b", "d"} + if !reflect.DeepEqual(remainder, expected) { + t.Errorf("incorrect remainder output:") + t.Errorf(" input: %#v", input) + t.Errorf(" filter: %#v", filter) + t.Errorf(" expected: %#v", expected) + t.Errorf(" got: %#v", remainder) + } + + expected = []string{"a", "c", "c", "a"} + if !reflect.DeepEqual(filtered, expected) { + t.Errorf("incorrect filtered output:") + t.Errorf(" input: %#v", input) + t.Errorf(" filter: %#v", filter) + t.Errorf(" expected: %#v", expected) + t.Errorf(" got: %#v", filtered) + } +} + +func TestRemoveListFromList(t *testing.T) { + input := []string{"a", "b", "c", "d", "a", "c", "d"} + filter := []string{"a", "c"} + expected := []string{"b", "d", "d"} + out := RemoveListFromList(input, filter) + if !reflect.DeepEqual(out, expected) { + t.Errorf("incorrect output:") + t.Errorf(" input: %#v", input) + t.Errorf(" filter: %#v", filter) + t.Errorf(" expected: %#v", expected) + t.Errorf(" got: %#v", out) + } +} |