observable_object_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. 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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 
21 
22 const int INITIAL_VALUE = 0;
23 
24 class ObservableObjectTest : public ::testing::Test {
25 public:
26  void SetUp() override
27  {
28  testIntObservable = std::make_shared<ObservableObject<int>>(INITIAL_VALUE);
29  }
30 
31  void TearDown() override
32  {}
33 
34 protected:
35  std::shared_ptr<ObservableObject<int>> testIntObservable;
36 };
37 
39  ASSERT_TRUE(true);
40 }
41 
43  EXPECT_EQ(INITIAL_VALUE, testIntObservable->getValue());
44 }
45 
47  EXPECT_EQ(INITIAL_VALUE, testIntObservable->getValue());
48  int first_set = 42;
49  testIntObservable->setValue(first_set);
50  EXPECT_EQ(first_set, testIntObservable->getValue());
51 }
52 
53 TEST_F(ObservableObjectTest, TestListener) {
54  EXPECT_EQ(INITIAL_VALUE, testIntObservable->getValue());
55  int first_set = 42;
56  testIntObservable->setValue(first_set);
57  EXPECT_EQ(first_set, testIntObservable->getValue());
58 
59  // register listener
60  int listened_value;
61  std::function<void(const int&)> lambda = [&listened_value](const int &currentValue){listened_value = currentValue;};
62  testIntObservable->addListener(lambda);
63  EXPECT_EQ(1u, testIntObservable->getNumberOfListeners());
64 
65  int second_set = 242;
66  testIntObservable->setValue(second_set); // currently synchronous
67 
68  EXPECT_EQ(second_set, listened_value);
69  EXPECT_EQ(second_set, testIntObservable->getValue());
70 
71  // test clear
72 
73  testIntObservable->clearListeners();
74  EXPECT_EQ(0u, testIntObservable->getNumberOfListeners());
75 
76  int third_set = 1999;
77  testIntObservable->setValue(third_set); // currently synchronous
78 
79  EXPECT_EQ(second_set, listened_value);
80  EXPECT_EQ(third_set, testIntObservable->getValue());
81 }
82 
83 TEST_F(ObservableObjectTest, TestFaultyListener) {
84 
85  enum Value {
86  VALID_1 = 1,
87  VALID_2 = 42,
88  INVALID = 999999999
89  };
90 
91  EXPECT_EQ(INITIAL_VALUE, testIntObservable->getValue());
92  int first_set = VALID_2;
93  testIntObservable->setValue(first_set);
94  EXPECT_EQ(first_set, testIntObservable->getValue());
95 
96  // register listener
97  int listened_value;
98  auto bad_lambda = [&listened_value](const int &currentValue){
99  switch(currentValue) {
100  case VALID_1:
101  case VALID_2:
102  listened_value = currentValue;
103  break;
104  default:
105  throw "imsorryjon";
106  }
107  };
108 
109  int good_listened_value;
110  auto lambda = [&good_listened_value](const int &currentValue) {good_listened_value = currentValue;};
111 
112  testIntObservable->addListener(bad_lambda);
113  EXPECT_EQ(1u, testIntObservable->getNumberOfListeners());
114 
115  testIntObservable->addListener(lambda);
116  EXPECT_EQ(2u, testIntObservable->getNumberOfListeners());
117 
118  testIntObservable->setValue(1); // currently synchronous
119 
120  EXPECT_EQ(VALID_1, listened_value);
121  EXPECT_EQ(VALID_1, good_listened_value);
122  EXPECT_EQ(VALID_1, testIntObservable->getValue());
123 
124  testIntObservable->setValue(INVALID); // currently synchronous
125 
126  EXPECT_EQ(1u, testIntObservable->getNumberOfListeners());
127  EXPECT_EQ(INVALID, testIntObservable->getValue());
128  EXPECT_EQ(INVALID, good_listened_value);
129  EXPECT_EQ(VALID_1 , listened_value);
130 
131  // ensure the bad listener is not added if it immediately throws
132  bool added = testIntObservable->addListener(bad_lambda);
133  EXPECT_EQ(1u, testIntObservable->getNumberOfListeners());
134  EXPECT_FALSE(added);
135  EXPECT_EQ(INVALID, testIntObservable->getValue());
136 }
137 
138 int main(int argc, char **argv) {
139  ::testing::InitGoogleTest(&argc, argv);
140  return RUN_ALL_TESTS();
141 }
std::shared_ptr< ObservableObject< int > > testIntObservable
TEST_F(ObservableObjectTest, Sanity)
const int INITIAL_VALUE
int main(int argc, char **argv)


dataflow_lite
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:22