Skip to content

Commit 4274191

Browse files
committed
Add comment API to Target
1 parent 438d349 commit 4274191

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

include/scratchcpp/target.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class IEngine;
1414
class Variable;
1515
class List;
1616
class Block;
17+
class Comment;
1718
class Costume;
1819
class Sound;
1920
class TargetPrivate;
@@ -50,6 +51,11 @@ class LIBSCRATCHCPP_EXPORT Target
5051
int findBlock(const std::string &id) const;
5152
std::vector<std::shared_ptr<Block>> greenFlagBlocks() const;
5253

54+
const std::vector<std::shared_ptr<Comment>> &comments() const;
55+
int addComment(std::shared_ptr<Comment> comment);
56+
std::shared_ptr<Comment> commentAt(int index) const;
57+
int findComment(const std::string &id) const;
58+
5359
int costumeIndex() const;
5460
void setCostumeIndex(int newCostumeIndex);
5561

@@ -75,7 +81,7 @@ class LIBSCRATCHCPP_EXPORT Target
7581
void setEngine(IEngine *engine);
7682

7783
protected:
78-
/*! Override this method to set a custom data source for blocks and assets. */
84+
/*! Override this method to set a custom data source for blocks, assets, comments, etc. */
7985
virtual Target *dataSource() const { return nullptr; }
8086

8187
private:

src/scratch/target.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/variable.h>
55
#include <scratchcpp/list.h>
66
#include <scratchcpp/block.h>
7+
#include <scratchcpp/comment.h>
78
#include <scratchcpp/iengine.h>
89

910
#include "target_p.h"
@@ -199,6 +200,60 @@ std::vector<std::shared_ptr<Block>> Target::greenFlagBlocks() const
199200
return ret;
200201
}
201202

203+
/*! Returns the list of comments in the code area. */
204+
const std::vector<std::shared_ptr<Comment>> &Target::comments() const
205+
{
206+
if (Target *source = dataSource())
207+
return source->comments();
208+
209+
return impl->comments;
210+
}
211+
212+
/*! Adds a comment and returns its index. */
213+
int Target::addComment(std::shared_ptr<Comment> comment)
214+
{
215+
if (Target *source = dataSource())
216+
return source->addComment(comment);
217+
218+
auto it = std::find(impl->comments.begin(), impl->comments.end(), comment);
219+
220+
if (it != impl->comments.end())
221+
return it - impl->comments.begin();
222+
223+
impl->comments.push_back(comment);
224+
return impl->comments.size() - 1;
225+
}
226+
227+
/*! Returns the comment at index. */
228+
std::shared_ptr<Comment> Target::commentAt(int index) const
229+
{
230+
if (Target *source = dataSource())
231+
return source->commentAt(index);
232+
233+
if (index < 0 || index >= impl->comments.size())
234+
return nullptr;
235+
236+
return impl->comments[index];
237+
}
238+
239+
/*! Returns the index of the comment with the given ID. */
240+
int Target::findComment(const std::string &id) const
241+
{
242+
if (Target *source = dataSource())
243+
return source->findComment(id);
244+
245+
int i = 0;
246+
247+
for (auto comment : impl->comments) {
248+
if (comment->id() == id)
249+
return i;
250+
251+
i++;
252+
}
253+
254+
return -1;
255+
}
256+
202257
/*! Returns the index of the current costume. */
203258
int Target::costumeIndex() const
204259
{

src/scratch/target_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class IEngine;
1515
class Variable;
1616
class List;
1717
class Block;
18+
class Comment;
1819

1920
struct TargetPrivate
2021
{
@@ -26,6 +27,7 @@ struct TargetPrivate
2627
std::vector<std::shared_ptr<Variable>> variables;
2728
std::vector<std::shared_ptr<List>> lists;
2829
std::vector<std::shared_ptr<Block>> blocks;
30+
std::vector<std::shared_ptr<Comment>> comments;
2931
int costumeIndex = -1;
3032
std::vector<std::shared_ptr<Costume>> costumes;
3133
std::vector<std::shared_ptr<Sound>> sounds;

test/scratch_classes/target_test.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/variable.h>
33
#include <scratchcpp/list.h>
44
#include <scratchcpp/block.h>
5+
#include <scratchcpp/comment.h>
56
#include <scratchcpp/costume.h>
67
#include <scratchcpp/sound.h>
78
#include <enginemock.h>
@@ -183,6 +184,89 @@ TEST(TargetTest, Blocks)
183184
ASSERT_EQ(source.greenFlagBlocks(), std::vector<std::shared_ptr<Block>>({ b1, b4 }));
184185
}
185186

187+
TEST(TargetTest, Comments)
188+
{
189+
auto c1 = std::make_shared<Comment>("a");
190+
auto c2 = std::make_shared<Comment>("b");
191+
auto c3 = std::make_shared<Comment>("c");
192+
auto c4 = std::make_shared<Comment>("d");
193+
194+
TargetMock target;
195+
EXPECT_CALL(target, dataSource()).Times(17).WillRepeatedly(Return(nullptr));
196+
197+
ASSERT_EQ(target.addComment(c1), 0);
198+
ASSERT_EQ(target.addComment(c2), 1);
199+
ASSERT_EQ(target.addComment(c3), 2);
200+
ASSERT_EQ(target.addComment(c4), 3);
201+
ASSERT_EQ(target.addComment(c2), 1); // add existing block
202+
203+
ASSERT_EQ(target.comments(), std::vector<std::shared_ptr<Comment>>({ c1, c2, c3, c4 }));
204+
ASSERT_EQ(target.commentAt(0), c1);
205+
ASSERT_EQ(target.commentAt(1), c2);
206+
ASSERT_EQ(target.commentAt(2), c3);
207+
ASSERT_EQ(target.commentAt(3), c4);
208+
ASSERT_EQ(target.commentAt(4), nullptr);
209+
ASSERT_EQ(target.commentAt(-1), nullptr);
210+
211+
ASSERT_EQ(target.findComment("e"), -1);
212+
ASSERT_EQ(target.findComment("a"), 0);
213+
ASSERT_EQ(target.findComment("b"), 1);
214+
ASSERT_EQ(target.findComment("c"), 2);
215+
ASSERT_EQ(target.findComment("d"), 3);
216+
217+
// Test with custom data source
218+
Target source;
219+
220+
EXPECT_CALL(target, dataSource()).WillOnce(Return(&source));
221+
222+
ASSERT_TRUE(target.comments().empty());
223+
224+
TargetMock target2;
225+
EXPECT_CALL(target2, dataSource()).Times(17).WillRepeatedly(Return(&source));
226+
227+
ASSERT_EQ(target2.addComment(c1), 0);
228+
ASSERT_EQ(target2.addComment(c2), 1);
229+
ASSERT_EQ(target2.addComment(c3), 2);
230+
ASSERT_EQ(target2.addComment(c4), 3);
231+
ASSERT_EQ(target2.addComment(c2), 1); // add existing block
232+
233+
ASSERT_EQ(target2.commentAt(0), c1);
234+
ASSERT_EQ(target2.commentAt(1), c2);
235+
ASSERT_EQ(target2.commentAt(2), c3);
236+
ASSERT_EQ(target2.commentAt(3), c4);
237+
ASSERT_EQ(target2.commentAt(4), nullptr);
238+
ASSERT_EQ(target2.commentAt(-1), nullptr);
239+
240+
ASSERT_EQ(target2.findComment("e"), -1);
241+
ASSERT_EQ(target2.findComment("a"), 0);
242+
ASSERT_EQ(target2.findComment("b"), 1);
243+
ASSERT_EQ(target2.findComment("c"), 2);
244+
ASSERT_EQ(target2.findComment("d"), 3);
245+
246+
ASSERT_EQ(target2.comments(), source.comments());
247+
248+
auto c5 = std::make_shared<Comment>("e");
249+
ASSERT_EQ(source.addComment(c5), 4);
250+
251+
EXPECT_CALL(target2, dataSource()).WillOnce(Return(&source));
252+
ASSERT_EQ(target2.comments(), source.comments());
253+
254+
ASSERT_EQ(source.commentAt(0), c1);
255+
ASSERT_EQ(source.commentAt(1), c2);
256+
ASSERT_EQ(source.commentAt(2), c3);
257+
ASSERT_EQ(source.commentAt(3), c4);
258+
ASSERT_EQ(source.commentAt(4), c5);
259+
ASSERT_EQ(source.commentAt(5), nullptr);
260+
ASSERT_EQ(source.commentAt(-1), nullptr);
261+
262+
ASSERT_EQ(source.findComment("f"), -1);
263+
ASSERT_EQ(source.findComment("a"), 0);
264+
ASSERT_EQ(source.findComment("b"), 1);
265+
ASSERT_EQ(source.findComment("c"), 2);
266+
ASSERT_EQ(source.findComment("d"), 3);
267+
ASSERT_EQ(source.findComment("e"), 4);
268+
}
269+
186270
TEST(TargetTest, CostumeIndex)
187271
{
188272
Target target;

0 commit comments

Comments
 (0)