OpenShot Audio Library | OpenShotAudio  0.6.0
juce_CachedValue.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 #if JUCE_UNIT_TESTS
30 
31 class CachedValueTests final : public UnitTest
32 {
33 public:
34  CachedValueTests()
35  : UnitTest ("CachedValues", UnitTestCategories::values)
36  {}
37 
38  void runTest() override
39  {
40  beginTest ("default constructor");
41  {
42  CachedValue<String> cv;
43  expect (cv.isUsingDefault());
44  expect (cv.get() == String());
45  }
46 
47  beginTest ("without default value");
48  {
49  ValueTree t ("root");
50  t.setProperty ("testkey", "testvalue", nullptr);
51 
52  CachedValue<String> cv (t, "testkey", nullptr);
53 
54  expect (! cv.isUsingDefault());
55  expect (cv.get() == "testvalue");
56 
57  cv.resetToDefault();
58 
59  expect (cv.isUsingDefault());
60  expect (cv.get() == String());
61  }
62 
63  beginTest ("with default value");
64  {
65  ValueTree t ("root");
66  t.setProperty ("testkey", "testvalue", nullptr);
67 
68  CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
69 
70  expect (! cv.isUsingDefault());
71  expect (cv.get() == "testvalue");
72 
73  cv.resetToDefault();
74 
75  expect (cv.isUsingDefault());
76  expect (cv.get() == "defaultvalue");
77  }
78 
79  beginTest ("with default value (int)");
80  {
81  ValueTree t ("root");
82  t.setProperty ("testkey", 23, nullptr);
83 
84  CachedValue<int> cv (t, "testkey", nullptr, 34);
85 
86  expect (! cv.isUsingDefault());
87  expect (cv == 23);
88  expectEquals (cv.get(), 23);
89 
90  cv.resetToDefault();
91 
92  expect (cv.isUsingDefault());
93  expect (cv == 34);
94  }
95 
96  beginTest ("with void value");
97  {
98  ValueTree t ("root");
99  t.setProperty ("testkey", var(), nullptr);
100 
101  CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
102 
103  expect (! cv.isUsingDefault());
104  expect (cv == "");
105  expectEquals (cv.get(), String());
106  }
107 
108  beginTest ("with non-existent value");
109  {
110  ValueTree t ("root");
111 
112  CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
113 
114  expect (cv.isUsingDefault());
115  expect (cv == "defaultvalue");
116  expect (cv.get() == "defaultvalue");
117  }
118 
119  beginTest ("with value changing");
120  {
121  ValueTree t ("root");
122  t.setProperty ("testkey", "oldvalue", nullptr);
123 
124  CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
125  expect (cv == "oldvalue");
126 
127  t.setProperty ("testkey", "newvalue", nullptr);
128  expect (cv != "oldvalue");
129  expect (cv == "newvalue");
130  }
131 
132  beginTest ("set value");
133  {
134  ValueTree t ("root");
135  t.setProperty ("testkey", 23, nullptr);
136 
137  CachedValue<int> cv (t, "testkey", nullptr, 45);
138  cv = 34;
139 
140  expectEquals ((int) t["testkey"], 34);
141 
142  cv.resetToDefault();
143  expect (cv == 45);
144  expectEquals (cv.get(), 45);
145 
146  expect (t["testkey"] == var());
147  }
148  }
149 };
150 
151 static CachedValueTests cachedValueTests;
152 
153 #endif
154 
155 } // namespace juce