| Paul Duffin | 581f2e5 | 2021-09-22 13:25:23 +0100 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project | 
|  | 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. | 
|  | 14 |  | 
|  | 15 | package android | 
|  | 16 |  | 
|  | 17 | import "testing" | 
|  | 18 |  | 
|  | 19 | type testSdkRegisterable struct { | 
|  | 20 | name string | 
|  | 21 | } | 
|  | 22 |  | 
|  | 23 | func (t *testSdkRegisterable) SdkPropertyName() string { | 
|  | 24 | return t.name | 
|  | 25 | } | 
|  | 26 |  | 
|  | 27 | var _ sdkRegisterable = &testSdkRegisterable{} | 
|  | 28 |  | 
|  | 29 | func TestSdkRegistry(t *testing.T) { | 
|  | 30 | alpha := &testSdkRegisterable{"alpha"} | 
|  | 31 | beta := &testSdkRegisterable{"beta"} | 
|  | 32 | betaDup := &testSdkRegisterable{"beta"} | 
|  | 33 |  | 
|  | 34 | // Make sure that an empty registry is empty. | 
|  | 35 | emptyRegistry := &sdkRegistry{} | 
|  | 36 | AssertDeepEquals(t, "emptyRegistry should be empty", ([]sdkRegisterable)(nil), emptyRegistry.registeredObjects()) | 
|  | 37 |  | 
|  | 38 | // Add beta to the empty registry to create another registry, check that it contains beta and make | 
|  | 39 | // sure that it does not affect the creating registry. | 
|  | 40 | registry1 := emptyRegistry.copyAndAppend(beta) | 
|  | 41 | AssertDeepEquals(t, "emptyRegistry should still be empty", ([]sdkRegisterable)(nil), emptyRegistry.registeredObjects()) | 
|  | 42 | AssertDeepEquals(t, "registry1 should contain beta", []sdkRegisterable{beta}, registry1.registeredObjects()) | 
|  | 43 |  | 
|  | 44 | // Add alpha to the registry containing beta to create another registry, check that it contains | 
|  | 45 | // alpha,beta (in order) and make sure that it does not affect the creating registry. | 
|  | 46 | registry2 := registry1.copyAndAppend(alpha) | 
|  | 47 | AssertDeepEquals(t, "registry1 should still contain beta", []sdkRegisterable{beta}, registry1.registeredObjects()) | 
|  | 48 | AssertDeepEquals(t, "registry2 should contain alpha,beta", []sdkRegisterable{alpha, beta}, registry2.registeredObjects()) | 
|  | 49 |  | 
|  | 50 | AssertPanicMessageContains(t, "duplicate beta should be detected", `"beta" already exists in ["alpha" "beta"]`, func() { | 
|  | 51 | registry2.copyAndAppend(betaDup) | 
|  | 52 | }) | 
|  | 53 | } |