Skip to content

Commit b71e9b3

Browse files
committed
Added test for variant attributes.
1 parent 9030ae3 commit b71e9b3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

nui/include/nui/frontend/event_system/observed_value.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ namespace Nui
217217
}
218218

219219
template <typename T = ContainedT>
220-
requires std::equality_comparable<T> && Fundamental<T>
220+
requires std::equality_comparable_with<ContainedT, T> && Fundamental<T> && Fundamental<ContainedT>
221221
ModifiableObserved& operator=(T&& t)
222222
{
223223
return assignChecked(t);
224224
}
225225

226226
template <typename T = ContainedT>
227-
requires std::equality_comparable<T>
227+
requires std::equality_comparable_with<ContainedT, T>
228228
ModifiableObserved& assignChecked(T&& other)
229229
{
230230
if (contained_ != other)

nui/test/nui/test_attributes.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,42 @@ namespace Nui::Tests
474474
globalEventContext.executeActiveEventsImmediately();
475475
EXPECT_EQ(Nui::val::global("document")["body"]["attributes"]["id"].as<std::string>(), "B");
476476
}
477+
478+
TEST_F(TestAttributes, AttributeCanBeVariant1)
479+
{
480+
using Nui::Elements::div;
481+
using Nui::Attributes::id;
482+
483+
std::variant<std::string, int> idValue{"A"};
484+
485+
render(div{id = idValue}());
486+
EXPECT_EQ(Nui::val::global("document")["body"]["attributes"]["id"].as<std::string>(), "A");
487+
}
488+
489+
TEST_F(TestAttributes, AttributeCanBeVariant2)
490+
{
491+
using Nui::Elements::div;
492+
using Nui::Attributes::id;
493+
494+
std::variant<std::string, int> idValue{2};
495+
496+
render(div{id = idValue}());
497+
EXPECT_EQ(Nui::val::global("document")["body"]["attributes"]["id"].as<long long>(), 2);
498+
}
499+
500+
TEST_F(TestAttributes, AttributeCanBeObservedVariant)
501+
{
502+
using Nui::Elements::div;
503+
using Nui::Attributes::id;
504+
505+
Observed<std::variant<std::string, int>> idValue{"A"};
506+
507+
render(div{id = idValue}());
508+
EXPECT_EQ(Nui::val::global("document")["body"]["attributes"]["id"].as<std::string>(), "A");
509+
510+
idValue = 2;
511+
globalEventContext.executeActiveEventsImmediately();
512+
513+
EXPECT_EQ(Nui::val::global("document")["body"]["attributes"]["id"].as<long long>(), 2);
514+
}
477515
}

0 commit comments

Comments
 (0)