Skip to content

Commit 0639750

Browse files
authored
Merge pull request #194 from scratchcpp/fix_zero_string_comparison
Fix #142: Fix comparison of zero with string
2 parents bc84aad + 4c03062 commit 0639750

File tree

4 files changed

+375
-5
lines changed

4 files changed

+375
-5
lines changed

include/scratchcpp/value.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,18 +520,20 @@ class LIBSCRATCHCPP_EXPORT Value
520520
}
521521
}
522522
} else {
523-
if (v1.isNumber() || v2.isNumber())
523+
if (v1.isString() || v2.isString())
524+
return stringsEqual(v1.toUtf16(), v2.toUtf16());
525+
else if (v1.isNumber() || v2.isNumber())
524526
return v1.toDouble() == v2.toDouble();
525527
else if (v1.isBool() || v2.isBool())
526528
return ((v1.m_type != Type::NaN && v2.m_type != Type::NaN) && (v1.toBool() == v2.toBool()));
527-
else if (v1.isString() || v2.isString())
528-
return stringsEqual(v1.toUtf16(), v2.toUtf16());
529529
else
530530
return false;
531531
}
532532
return false;
533533
}
534534

535+
friend bool operator!=(const Value &v1, const Value &v2) { return !(v1 == v2); }
536+
535537
friend bool operator>(const Value &v1, const Value &v2)
536538
{
537539
if ((static_cast<int>(v1.m_type) < 0) || (static_cast<int>(v2.m_type) < 0)) {

test/load_project/load_project_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ TEST(LoadProjectTest, LoadTestProject)
196196
ASSERT_EQ(prototype->argumentIds().size(), 2);
197197
ASSERT_EQ(prototype->argumentNames(), std::vector<std::string>({ "num or text", "bool" }));
198198
ASSERT_EQ(prototype->argumentTypes(), std::vector<BlockPrototype::ArgType>({ BlockPrototype::ArgType::StringNum, BlockPrototype::ArgType::Bool }));
199-
ASSERT_EQ(prototype->argumentDefaults(), std::vector<Value>({ 0, false }));
199+
ASSERT_EQ(prototype->argumentDefaults(), std::vector<Value>({ "", false }));
200200
ASSERT_EQ(prototype->procCode(), "custom block %s %b");
201201
ASSERT_INPUT(blockPrototype, prototype->argumentIds()[0]);
202202
auto argBlock = GET_INPUT(blockPrototype, prototype->argumentIds()[0])->valueBlock();

0 commit comments

Comments
 (0)