OpenShot Audio Library | OpenShotAudio 0.3.2

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