• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
20 
getMainComponent()21 fruit::Component<fruit::Annotated<Cached, Greeter>> getMainComponent() {
22   return fruit::createComponent()
23       // Note: order matters here. This replace().with() must be before the install. Otherwise Fruit will report the
24       // wrong order as a run-time error.
25       .replace(getKeyValueStorageComponent).with(getFakeKeyValueStorageComponent)
26       .install(getCachedGreeterComponent);
27 }
28 
createInjector()29 fruit::Injector<fruit::Annotated<Cached, Greeter>> createInjector() {
30   return fruit::Injector<fruit::Annotated<Cached, Greeter>>(getMainComponent);
31 }
32 
TEST(CachedGreeter,NotYetCached)33 TEST(CachedGreeter, NotYetCached) {
34   fruit::Injector<fruit::Annotated<Cached, Greeter>> injector = createInjector();
35   Greeter* greeter = injector.get<fruit::Annotated<Cached, Greeter*>>();
36   ASSERT_EQ(greeter->greet(), "Hello, world!");
37 }
38 
TEST(CachedGreeter,Cached)39 TEST(CachedGreeter, Cached) {
40   fruit::Injector<fruit::Annotated<Cached, Greeter>> injector = createInjector();
41   Greeter* greeter = injector.get<fruit::Annotated<Cached, Greeter*>>();
42   greeter->greet();
43   ASSERT_EQ(greeter->greet(), "Hello, world!");
44 }
45 
main(int argc,char ** argv)46 int main(int argc, char **argv) {
47   ::testing::InitGoogleTest(&argc, argv);
48   return RUN_ALL_TESTS();
49 }
50