Skip to content

Commit a148c4b

Browse files
committed
Add comment property to Block
1 parent 4274191 commit a148c4b

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

include/scratchcpp/block.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class IEngine;
1212
class Target;
1313
class Input;
1414
class Field;
15+
class Comment;
1516
class InputValue;
1617
class BlockPrivate;
1718

@@ -55,6 +56,11 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
5556

5657
bool topLevel() const;
5758

59+
std::shared_ptr<Comment> comment() const;
60+
std::string commentId() const;
61+
void setComment(std::shared_ptr<Comment> comment);
62+
void setCommentId(const std::string &commentId);
63+
5864
void setEngine(IEngine *newEngine);
5965
IEngine *engine() const;
6066

src/scratch/block.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/block.h>
44
#include <scratchcpp/input.h>
55
#include <scratchcpp/field.h>
6+
#include <scratchcpp/comment.h>
67

78
#include "block_p.h"
89

@@ -295,6 +296,36 @@ bool Block::topLevel() const
295296
return (impl->parentId == "" && !impl->parent);
296297
}
297298

299+
/*! Returns the comment which is attached to this block. */
300+
std::shared_ptr<Comment> Block::comment() const
301+
{
302+
return impl->comment;
303+
}
304+
305+
/*! Returns the ID of the comment which is attached to this block. */
306+
std::string Block::commentId() const
307+
{
308+
return impl->commentId;
309+
}
310+
311+
/*! Sets the comment which is attached to this block. */
312+
void Block::setComment(std::shared_ptr<Comment> comment)
313+
{
314+
impl->comment = comment;
315+
316+
if (comment)
317+
impl->commentId = comment->id();
318+
else
319+
impl->commentId = "";
320+
}
321+
322+
/*! Sets the ID of the comment which is attached to this block. */
323+
void Block::setCommentId(const std::string &commentId)
324+
{
325+
impl->commentId = commentId;
326+
impl->comment = nullptr;
327+
}
328+
298329
/*! Sets the Engine. */
299330
void Block::setEngine(IEngine *newEngine)
300331
{

src/scratch/block_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class IEngine;
1414
class Target;
1515
class Input;
1616
class Field;
17+
class Comment;
1718

1819
struct BlockPrivate
1920
{
@@ -31,6 +32,8 @@ struct BlockPrivate
3132
std::vector<std::shared_ptr<Field>> fields;
3233
std::unordered_map<int, Field *> fieldMap;
3334
bool shadow = false;
35+
std::string commentId;
36+
std::shared_ptr<Comment> comment = nullptr;
3437
IEngine *engine = nullptr;
3538
Target *target = nullptr;
3639
BlockPrototype mutationPrototype;

test/scratch_classes/block_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <scratchcpp/block.h>
22
#include <scratchcpp/input.h>
33
#include <scratchcpp/field.h>
4+
#include <scratchcpp/comment.h>
45
#include <scratchcpp/target.h>
56
#include <scratchcpp/compiler.h>
67
#include <enginemock.h>
@@ -193,6 +194,30 @@ TEST_F(BlockTest, TopLevel)
193194
ASSERT_TRUE(block.topLevel());
194195
}
195196

197+
TEST_F(BlockTest, Comment)
198+
{
199+
Block block("", "");
200+
ASSERT_EQ(block.comment(), nullptr);
201+
ASSERT_TRUE(block.commentId().empty());
202+
203+
block.setCommentId("hello");
204+
ASSERT_EQ(block.comment(), nullptr);
205+
ASSERT_EQ(block.commentId(), "hello");
206+
207+
auto comment = std::make_shared<Comment>("abc");
208+
block.setComment(comment);
209+
ASSERT_EQ(block.comment(), comment);
210+
ASSERT_EQ(block.commentId(), "abc");
211+
212+
block.setCommentId("def");
213+
ASSERT_EQ(block.comment(), nullptr);
214+
ASSERT_EQ(block.commentId(), "def");
215+
216+
block.setComment(nullptr);
217+
ASSERT_EQ(block.comment(), nullptr);
218+
ASSERT_EQ(block.commentId(), "");
219+
}
220+
196221
TEST_F(BlockTest, Engine)
197222
{
198223
Block block("", "");

0 commit comments

Comments
 (0)