Skip to content

Commit 8389127

Browse files
committed
Add unit tests for Cvar::Modified
Note: previously I was thinking that I would need a Modified latched cvar so there are a bunch of tests for that case. But we may end up not using it.
1 parent 62ad9e0 commit 8389127

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ endif()
9090
set(COMMONTESTLIST
9191
${LIB_DIR}/tinyformat/TinyformatTest.cpp
9292
${COMMON_DIR}/ColorTest.cpp
93+
${COMMON_DIR}/CvarTest.cpp
9394
${COMMON_DIR}/FileSystemTest.cpp
9495
${COMMON_DIR}/StringTest.cpp
9596
${COMMON_DIR}/cm/unittest.cpp

src/common/CvarTest.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
===========================================================================
3+
Daemon BSD Source Code
4+
Copyright (c) 2024, Daemon Developers
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
* Neither the name of the Daemon developers nor the
15+
names of its contributors may be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
22+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
===========================================================================
29+
*/
30+
31+
#include <gtest/gtest.h>
32+
33+
#include "common/Common.h"
34+
#include "engine/framework/CvarSystem.h"
35+
36+
namespace Cvar {
37+
namespace {
38+
39+
TEST(ModifiedCvarTest, Normal)
40+
{
41+
Modified<Cvar<int>> cv("test_modified", "desc", NONE, 3);
42+
Util::optional<int> modified = cv.GetModifiedValue();
43+
ASSERT_TRUE(modified); // modified flag is true on birth
44+
ASSERT_EQ(modified.value(), 3);
45+
46+
// now cleared
47+
ASSERT_FALSE(cv.GetModifiedValue());
48+
ASSERT_FALSE(cv.GetModifiedValue());
49+
50+
// test setting via the cvar object
51+
cv.Set(9);
52+
modified = cv.GetModifiedValue();
53+
ASSERT_TRUE(modified);
54+
ASSERT_EQ(modified.value(), 9);
55+
56+
ASSERT_FALSE(cv.GetModifiedValue());
57+
58+
// test setting externally
59+
SetValue("test_modified", "1");
60+
modified = cv.GetModifiedValue();
61+
ASSERT_TRUE(modified);
62+
ASSERT_EQ(modified.value(), 1);
63+
64+
ASSERT_FALSE(cv.GetModifiedValue());
65+
66+
// test that invalid set is ignored
67+
SetValue("test_modified", "a");
68+
ASSERT_FALSE(cv.GetModifiedValue());
69+
}
70+
71+
TEST(ModifiedCvarTest, Latch)
72+
{
73+
Modified<Cvar<float>> cv("test_modifiedLatched", "desc", NONE, 1.5f);
74+
Latch(cv);
75+
ASSERT_TRUE(cv.GetModifiedValue());
76+
ASSERT_FALSE(cv.GetModifiedValue());
77+
78+
cv.Set(-5.0f);
79+
ASSERT_EQ(cv.Get(), 1.5f);
80+
81+
// Clear latch flag. It's probably true now because the implementation of latched cvars
82+
// sets them to the new value and back to the current value, but let's not make this
83+
// part of the contract.
84+
cv.GetModifiedValue();
85+
86+
// Make sure modified flag is set upon unlatching
87+
Latch(cv);
88+
Util::optional<float> modified = cv.GetModifiedValue();
89+
ASSERT_TRUE(modified);
90+
ASSERT_EQ(modified.value(), -5.0f);
91+
}
92+
93+
} // namespace Cvar
94+
} // namespace

0 commit comments

Comments
 (0)