Add tests for settings classes (#1188)

This commit is contained in:
Eric Gregory authored and GitHub committed 2026-03-15 22:45:37 -04:00
1 parent 7a251c5fff
commit 12ab4c8753
1 file changed
+135 -27
+135 -27
View File
@@ -7,7 +7,7 @@ class TestSettings : public QObject
Q_OBJECT
private slots:
// SettingsValue tests
// SettingsValue
void testDefaultValueReturnedWhenNoExplicitValue();
void testExplicitValueOverridesDefault();
void testInvalidValueRestoresDefault();
@@ -17,7 +17,7 @@ private slots:
void testIsHiddenPlatformAny();
void testIsHiddenMismatchedPlatform();
// SettingsSection tests
// SettingsSection
void testRegisterSettingMakesValueRetrievable();
void testDuplicateRegisterSettingRejected();
void testValueForUnknownKeyReturnsInvalid();
@@ -29,58 +29,64 @@ private slots:
void testSectionIsHiddenPlatform();
void testSectionOrder();
void testValuesUpdatedSignalOnReset();
void testValuesUpdatedSignalNotFiredWhenNoChange();
void testResetValueNonExistentKeyIsNoop();
void testSectionDescriptionsFiltersHidden();
void testSectionDescriptionsSortsByIndexOrder();
void testSetValueHidden();
void testStorageFlag();
};
/*!
SettingsValue tests
SettingsValue
*/
void TestSettings::testDefaultValueReturnedWhenNoExplicitValue()
{
SettingsValue sv("keyTest", 42);
QCOMPARE(sv.value(), QVariant(42));
SettingsValue sv("testKey", 7);
QCOMPARE(sv.value(), QVariant(7));
}
void TestSettings::testExplicitValueOverridesDefault()
{
SettingsValue sv("keyTest", 42);
SettingsValue sv("testKey", 100);
sv.setValue(123);
QCOMPARE(sv.value(), QVariant(123));
}
void TestSettings::testInvalidValueRestoresDefault()
{
SettingsValue sv("keyTest", 42);
SettingsValue sv("testKey", 101);
sv.setValue(456);
QCOMPARE(sv.value(), QVariant(456));
sv.setValue(QVariant()); // invalid value
QCOMPARE(sv.value(), QVariant(42));
sv.setValue(QVariant()); // Not valid
QCOMPARE(sv.value(), QVariant(101));
}
void TestSettings::testPossibleValues()
{
SettingsValue sv("keyTest", QString("a"));
sv.addPossibleValue("Option A", QString("a"));
sv.addPossibleValue("Option B", QString("b"));
SettingsValue sv("testKey", QString("a"));
sv.addPossibleValue("Alpha", QString("a"));
sv.addPossibleValue("Beta", QString("b"));
QVariantList possible = sv.possibleValues();
QCOMPARE(possible.size(), 2);
QVariantMap first = possible[0].toMap();
QCOMPARE(first["title"].toString(), QString("Option A"));
QCOMPARE(first["title"].toString(), QString("Alpha"));
QCOMPARE(first["value"].toString(), QString("a"));
QVariantMap second = possible[1].toMap();
QCOMPARE(second["title"].toString(), QString("Option B"));
QCOMPARE(second["title"].toString(), QString("Beta"));
QCOMPARE(second["value"].toString(), QString("b"));
// setPossibleValues replaces the list
// Replace the list
QVariantList newList;
QVariantMap entry;
entry["value"] = QString("x");
entry["title"] = QString("Option X");
entry["title"] = QString("X-Ray");
newList << entry;
sv.setPossibleValues(newList);
QCOMPARE(sv.possibleValues().size(), 1);
@@ -88,16 +94,16 @@ void TestSettings::testPossibleValues()
void TestSettings::testDescriptionsOutput()
{
SettingsValue sv("myKey", 10);
SettingsValue sv("testKey", 10);
sv.setDisplayName("My Setting");
sv.setHelp("Sample help text");
sv.setHelp("Sample help");
sv.addPossibleValue("Ten", 10);
sv.addPossibleValue("Twenty", 20);
QVariantMap desc = sv.descriptions();
QCOMPARE(desc["key"].toString(), QString("myKey"));
QCOMPARE(desc["key"].toString(), QString("testKey"));
QCOMPARE(desc["displayName"].toString(), QString("My Setting"));
QCOMPARE(desc["help"].toString(), QString("Sample help text"));
QCOMPARE(desc["help"].toString(), QString("Sample help"));
QVERIFY(desc.contains("options"));
QCOMPARE(desc["options"].toList().size(), 2);
QVERIFY(!desc.contains("inputType"));
@@ -105,7 +111,7 @@ void TestSettings::testDescriptionsOutput()
void TestSettings::testDescriptionsWithInputType()
{
SettingsValue sv("myKey", QString(""));
SettingsValue sv("testKey", QString(""));
sv.setDisplayName("Name");
sv.setHelp("Help");
sv.setInputType("numeric");
@@ -118,8 +124,8 @@ void TestSettings::testDescriptionsWithInputType()
void TestSettings::testIsHiddenPlatformAny()
{
SettingsValue sv("key", 0, PLATFORM_ANY);
// default hidden=true
SettingsValue sv("testKey", 0, PLATFORM_ANY);
// Defaults tp hidden for PLATFORM_ANY
QVERIFY(sv.isHidden());
sv.setHidden(false);
@@ -130,7 +136,7 @@ void TestSettings::testIsHiddenMismatchedPlatform()
{
// Use a platform that is not the current one.
quint8 wrongPlatform = PLATFORM_ANY & ~Utils::CurrentPlatform();
SettingsValue sv("key", 0, wrongPlatform);
SettingsValue sv("testKey", 0, wrongPlatform);
sv.setHidden(false);
// Wrong platform should override hidden = false.
@@ -178,7 +184,7 @@ void TestSettings::testDefaultValueReturnsSettingDefault()
auto* sv = new SettingsValue("moose", 55);
section.registerSetting(sv);
// Set a value on the SettingsValue directly
// Set value directly
sv->setValue(77);
QCOMPARE(section.value("moose"), QVariant(77));
QCOMPARE(section.defaultValue("moose"), QVariant(55));
@@ -243,10 +249,10 @@ void TestSettings::testResetValuesResetsAll()
section.resetValues();
// Described setting resets to default.
// The described setting resets back to default.
QCOMPARE(section.value("described"), QVariant(10));
// Dynamic setting removed.
// Dynamic setting is removed.
QVERIFY(!section.value("dynamic").isValid());
}
@@ -294,5 +300,107 @@ void TestSettings::testValuesUpdatedSignalOnReset()
QCOMPARE(updated["delta"], QVariant(42));
}
void TestSettings::testValuesUpdatedSignalNotFiredWhenNoChange()
{
SettingsSection section("testSection", PLATFORM_ANY, 0);
auto* sv = new SettingsValue("epsilon", 42);
sv->setHasDescription(true);
section.registerSetting(sv);
// Value is already at default — reset should be a no-op.
QSignalSpy spy(&section, &SettingsSection::valuesUpdated);
section.resetValue("epsilon");
QCOMPARE(spy.count(), 0);
}
void TestSettings::testResetValueNonExistentKeyIsNoop()
{
SettingsSection section("testSection", PLATFORM_ANY, 0);
QSignalSpy spy(&section, &SettingsSection::valuesUpdated);
// Should not fire signal.
section.resetValue("ghost");
QCOMPARE(spy.count(), 0);
}
void TestSettings::testSectionDescriptionsFiltersHidden()
{
SettingsSection section("mySection", PLATFORM_ANY, 0);
auto* visible = new SettingsValue("vis", 1, PLATFORM_ANY);
visible->setHidden(false);
visible->setHasDescription(true);
visible->setDisplayName("Visible");
section.registerSetting(visible);
auto* hidden = new SettingsValue("hid", 2, PLATFORM_ANY);
hidden->setHidden(true);
hidden->setHasDescription(true);
hidden->setDisplayName("Hidden");
section.registerSetting(hidden);
QVariantMap desc = section.descriptions();
QCOMPARE(desc["key"].toString(), QString("mySection"));
QVariantList settings = desc["settings"].toList();
QCOMPARE(settings.size(), 1);
QCOMPARE(settings[0].toMap()["key"].toString(), QString("vis"));
}
void TestSettings::testSectionDescriptionsSortsByIndexOrder()
{
SettingsSection section("mySection", PLATFORM_ANY, 0);
// Create settings in mixed-up order.
auto* second = new SettingsValue("second", 2, PLATFORM_ANY);
second->setHidden(false);
second->setHasDescription(true);
second->setIndexOrder(1); // 1 = second
section.registerSetting(second);
auto* first = new SettingsValue("first", 1, PLATFORM_ANY);
first->setHidden(false);
first->setHasDescription(true);
first->setIndexOrder(0); // 0 = first
section.registerSetting(first);
auto* third = new SettingsValue("third", 3, PLATFORM_ANY);
third->setHidden(false);
third->setHasDescription(true);
third->setIndexOrder(2); // 2 = third
section.registerSetting(third);
QVariantList settings = section.descriptions()["settings"].toList();
QCOMPARE(settings.size(), 3);
QCOMPARE(settings[0].toMap()["key"].toString(), QString("first"));
QCOMPARE(settings[1].toMap()["key"].toString(), QString("second"));
QCOMPARE(settings[2].toMap()["key"].toString(), QString("third"));
}
void TestSettings::testSetValueHidden()
{
SettingsSection section("testSection", PLATFORM_ANY, 0);
auto* sv = new SettingsValue("toggle", 0, PLATFORM_ANY);
sv->setHidden(false);
section.registerSetting(sv);
QVERIFY(!section.isValueHidden("toggle"));
section.setValueHidden("toggle", true);
QVERIFY(section.isValueHidden("toggle"));
section.setValueHidden("toggle", false);
QVERIFY(!section.isValueHidden("toggle"));
}
void TestSettings::testStorageFlag()
{
SettingsSection section("store", PLATFORM_ANY, 0);
QVERIFY(!section.isStorage());
section.setStorage(true);
QVERIFY(section.isStorage());
}
QTEST_APPLESS_MAIN(TestSettings)
#include "test_settings.moc"