2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
31 class CachedValueTests
: public UnitTest
35 : UnitTest ("CachedValues", UnitTestCategories::values
)
38 void runTest() override
40 beginTest ("default constructor");
42 CachedValue
<String
> cv
;
43 expect (cv
.isUsingDefault());
44 expect (cv
.get() == String());
47 beginTest ("without default value");
50 t
.setProperty ("testkey", "testvalue", nullptr);
52 CachedValue
<String
> cv (t
, "testkey", nullptr);
54 expect (! cv
.isUsingDefault());
55 expect (cv
.get() == "testvalue");
59 expect (cv
.isUsingDefault());
60 expect (cv
.get() == String());
63 beginTest ("with default value");
66 t
.setProperty ("testkey", "testvalue", nullptr);
68 CachedValue
<String
> cv (t
, "testkey", nullptr, "defaultvalue");
70 expect (! cv
.isUsingDefault());
71 expect (cv
.get() == "testvalue");
75 expect (cv
.isUsingDefault());
76 expect (cv
.get() == "defaultvalue");
79 beginTest ("with default value (int)");
82 t
.setProperty ("testkey", 23, nullptr);
84 CachedValue
<int> cv (t
, "testkey", nullptr, 34);
86 expect (! cv
.isUsingDefault());
88 expectEquals (cv
.get(), 23);
92 expect (cv
.isUsingDefault());
96 beginTest ("with void value");
99 t
.setProperty ("testkey", var(), nullptr);
101 CachedValue
<String
> cv (t
, "testkey", nullptr, "defaultvalue");
103 expect (! cv
.isUsingDefault());
105 expectEquals (cv
.get(), String());
108 beginTest ("with non-existent value");
110 ValueTree
t ("root");
112 CachedValue
<String
> cv (t
, "testkey", nullptr, "defaultvalue");
114 expect (cv
.isUsingDefault());
115 expect (cv
== "defaultvalue");
116 expect (cv
.get() == "defaultvalue");
119 beginTest ("with value changing");
121 ValueTree
t ("root");
122 t
.setProperty ("testkey", "oldvalue", nullptr);
124 CachedValue
<String
> cv (t
, "testkey", nullptr, "defaultvalue");
125 expect (cv
== "oldvalue");
127 t
.setProperty ("testkey", "newvalue", nullptr);
128 expect (cv
!= "oldvalue");
129 expect (cv
== "newvalue");
132 beginTest ("set value");
134 ValueTree
t ("root");
135 t
.setProperty ("testkey", 23, nullptr);
137 CachedValue
<int> cv (t
, "testkey", nullptr, 45);
140 expectEquals ((int) t
["testkey"], 34);
144 expectEquals (cv
.get(), 45);
146 expect (t
["testkey"] == var());
151 static CachedValueTests cachedValueTests
;