From 4e03238144476af112279ab28d4a4df10076237f Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:29:54 +0200 Subject: [PATCH 1/2] fix #188: Join digits without spaces in List::toString() --- src/scratch/list.cpp | 32 +++++++++++++++++++++++++----- test/scratch_classes/list_test.cpp | 18 +++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/scratch/list.cpp b/src/scratch/list.cpp index 1f2109f1..db958547 100644 --- a/src/scratch/list.cpp +++ b/src/scratch/list.cpp @@ -43,14 +43,36 @@ bool List::contains(const Value &value) const return (indexOf(value) != -1); } -/*! Joins the list items with spaces. */ +/*! Joins the list items with spaces or without any separator if there are only digits. */ std::string List::toString() const { std::string ret; - for (int i = 0; i < size(); i++) { - ret.append(at(i).toString()); - if (i + 1 < size()) - ret.push_back(' '); + bool digits = true; + + for (const auto &item : *this) { + if (item.type() == Value::Type::Integer) { + long num = item.toLong(); + + if (num < 0 || num >= 10) { + digits = false; + break; + } + } else { + digits = false; + break; + } } + + if (digits) { + for (const auto &item : *this) + ret.append(item.toString()); + } else { + for (int i = 0; i < size(); i++) { + ret.append(at(i).toString()); + if (i + 1 < size()) + ret.push_back(' '); + } + } + return ret; } diff --git a/test/scratch_classes/list_test.cpp b/test/scratch_classes/list_test.cpp index c42240c9..4464d666 100644 --- a/test/scratch_classes/list_test.cpp +++ b/test/scratch_classes/list_test.cpp @@ -151,4 +151,22 @@ TEST(ListTest, ToString) list.push_back("áä"); list.push_back("ľ š"); ASSERT_EQ(list.toString(), "áä ľ š"); + + list.clear(); + list.push_back(-2); + list.push_back(5); + list.push_back(8); + ASSERT_EQ(list.toString(), "-2 5 8"); + + list.clear(); + list.push_back(2); + list.push_back(10); + list.push_back(8); + ASSERT_EQ(list.toString(), "2 10 8"); + + list.clear(); + list.push_back(0); + list.push_back(9); + list.push_back(8); + ASSERT_EQ(list.toString(), "098"); } From 38f7b17d4766a069b5553f54d91daeef1e147a75 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:32:43 +0200 Subject: [PATCH 2/2] Update engine test after fixing #188 --- test/engine/engine_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/engine/engine_test.cpp b/test/engine/engine_test.cpp index b9653b18..cd97cd4a 100644 --- a/test/engine/engine_test.cpp +++ b/test/engine/engine_test.cpp @@ -422,7 +422,7 @@ TEST(EngineTest, Clones) if (i < 10) ASSERT_EQ((*list)[i].toInt(), 1); else - ASSERT_EQ((*list)[i].toString(), "1 2"); // TODO: Change this to "12" after #188 is fixed + ASSERT_EQ((*list)[i].toString(), "12"); } }