1 #include "test/common.hpp"
2 
3 using namespace pajlada::Settings;
4 using namespace std;
5 
6 TEST_CASE("simple_signal", "[signal]")
7 {
8     int count = 0;
9     int currentValue = 0;
__anond31bc2840102(const int &newValue, auto) 10     auto cb = [&count, &currentValue](const int &newValue, auto) {
11         ++count;
12         currentValue = newValue;
13     };
14 
15     Setting<int> a("/simple_signal/a");
16 
17     REQUIRE(count == 0);
18     REQUIRE(currentValue == 0);
19 
20     a.connect(cb, false);
21 
22     REQUIRE(count == 0);
23     REQUIRE(currentValue == 0);
24 
25     a = 5;
26 
27     REQUIRE(count == 1);
28     REQUIRE(currentValue == 5);
29 }
30 
31 TEST_CASE("load_from_file", "[signal]")
32 {
33     int count = 0;
34     int currentValue = 0;
__anond31bc2840202(const int &newValue, auto) 35     auto cb = [&count, &currentValue](const int &newValue, auto) {
36         ++count;
37         currentValue = newValue;
38     };
39 
40     Setting<int> a("/signal/a");
41     Setting<int> b("/signal/b");
42 
43     REQUIRE(a.getValue() == 0);
44 
45     REQUIRE(count == 0);
46     REQUIRE(currentValue == 0);
47 
48     a.connect(cb, false);
49     b.connect(cb, false);
50 
51     REQUIRE(count == 0);
52     REQUIRE(currentValue == 0);
53 
54     a = 5;
55 
56     REQUIRE(count == 1);
57     REQUIRE(currentValue == 5);
58 
59     REQUIRE(LoadFile("in.signal.json"));
60 
61     REQUIRE(count == 2);
62     REQUIRE(currentValue == 3);
63 }
64 
65 TEST_CASE("scoped_connection", "[signal]")
66 {
67     int count = 0;
68     int currentValue = 0;
__anond31bc2840302(const int &newValue, auto) 69     auto cb = [&count, &currentValue](const int &newValue, auto) {
70         ++count;
71         currentValue = newValue;
72     };
73 
74     vector<pajlada::Signals::ScopedConnection> connections;
75 
76     {
77         Setting<int> c("/advancedSignals/c");
78 
79         REQUIRE(count == 0);
80 
81         c.connect(cb, connections, false);
82         c.connect(cb, connections, false);
83         c.connect(cb, connections, false);
84 
85         REQUIRE(count == 0);
86 
87         // c1, c2, and c3 are active
88         c = 1;
89 
90         REQUIRE(count == 3);
91 
92         connections.pop_back();
93 
94         // c1 and c2 are active
95         c = 2;
96 
97         REQUIRE(count == 5);
98 
99         connections.clear();
100 
101         // No connection is active
102         c = 3;
103 
104         REQUIRE(count == 5);
105     }
106 }
107 
108 TEST_CASE("scoped_connection2", "[signal]")
109 {
110     int count = 0;
111     int currentValue = 0;
__anond31bc2840402(const int &newValue, auto) 112     auto cb = [&count, &currentValue](const int &newValue, auto) {
113         ++count;
114         currentValue = newValue;
115     };
116 
117     vector<pajlada::Signals::ScopedConnection> connections;
118 
119     {
120         Setting<int> c("/advancedSignals/c");
121 
122         REQUIRE(count == 0);
123 
124         c.connect(cb, connections, false);
125         c.connect(cb, connections, false);
126         c.connect(cb, connections, false);
127 
128         REQUIRE(count == 0);
129 
130         // c1, c2, and c3 are active
131         c = 1;
132 
133         REQUIRE(count == 3);
134 
135         connections.pop_back();
136 
137         {
138             Setting<int> c2("/advancedSignals/c");
139         }
140 
141         // c1 and c2 are active
142         c = 2;
143 
144         REQUIRE(count == 5);
145 
146         connections.clear();
147 
148         // No connection is active
149         c = 3;
150 
151         REQUIRE(count == 5);
152     }
153 }
154