From 274133bcfc638636786c08e5740a7573e7eae3d4 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sat, 11 Feb 2023 11:33:06 -0800 Subject: [PATCH] ~fix magic union autoselect Stop of after first successful conversion --- src/data.cpp | 1 + test/testdata.cpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/data.cpp b/src/data.cpp index e85db2b..f270c23 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -696,6 +696,7 @@ void Value::copyIn(const void *ptr, StoreType type) continue; } val = std::move(temp); + break; } if(!val) throw NoConvert("Unsupported assignment to unselected union"); diff --git a/test/testdata.cpp b/test/testdata.cpp index 79ff926..3e9cd31 100644 --- a/test/testdata.cpp +++ b/test/testdata.cpp @@ -329,6 +329,27 @@ void testAssignSimilar() } } +void testUnionMagicAssign() +{ + testShow()<<__func__; + + using namespace members; + auto val(TypeDef(TypeCode::Union, { + UInt16("x"), + Float64("y"), + }).create()); + + val = 5; + testEq(val.nameOf(val["->"]), "x"); + testEq(val.as(), "5"); + + val = unselect; + + testThrows([&val](){ + val = "invalid"; + }); +} + void testExtract() { testShow()<<__func__; @@ -395,7 +416,7 @@ void testClear() MAIN(testdata) { - testPlan(143); + testPlan(146); testSetup(); testTraverse(); testAssign(); @@ -444,6 +465,7 @@ MAIN(testdata) testConvertScalar2(0, 0x100000000llu, -0); testAssignSimilar(); + testUnionMagicAssign(); testExtract(); testClear(); cleanup_for_valgrind();