1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "cached_greeter.h"
18 #include "fake_key_value_storage.h"
19
20 #include <gtest/gtest.h>
21
getMainComponent()22 fruit::Component<fruit::Annotated<Cached, Greeter>> getMainComponent() {
23 return fruit::createComponent()
24 // Note: order matters here. This replace().with() must be before the install. Otherwise Fruit will report the
25 // wrong order as a run-time error.
26 .replace(getKeyValueStorageComponent).with(getFakeKeyValueStorageComponent)
27 .install(getCachedGreeterComponent);
28 }
29
getEmptyComponent()30 fruit::Component<> getEmptyComponent() {
31 return fruit::createComponent();
32 }
33
createInjector()34 fruit::Injector<fruit::Annotated<Cached, Greeter>> createInjector() {
35 static fruit::NormalizedComponent<fruit::Annotated<Cached, Greeter>> normalizedComponent(getMainComponent);
36 return fruit::Injector<fruit::Annotated<Cached, Greeter>>(normalizedComponent, getEmptyComponent);
37 }
38
TEST(CachedGreeter,NotYetCached)39 TEST(CachedGreeter, NotYetCached) {
40 fruit::Injector<fruit::Annotated<Cached, Greeter>> injector = createInjector();
41 Greeter* greeter = injector.get<fruit::Annotated<Cached, Greeter*>>();
42 ASSERT_EQ(greeter->greet(), "Hello, world!");
43 }
44
TEST(CachedGreeter,Cached)45 TEST(CachedGreeter, Cached) {
46 fruit::Injector<fruit::Annotated<Cached, Greeter>> injector = createInjector();
47 Greeter* greeter = injector.get<fruit::Annotated<Cached, Greeter*>>();
48 greeter->greet();
49 ASSERT_EQ(greeter->greet(), "Hello, world!");
50 }
51
main(int argc,char ** argv)52 int main(int argc, char **argv) {
53 ::testing::InitGoogleTest(&argc, argv);
54 return RUN_ALL_TESTS();
55 }
56