|
2 | 2 | #include <scratchcpp/variable.h> |
3 | 3 | #include <scratchcpp/list.h> |
4 | 4 | #include <scratchcpp/block.h> |
| 5 | +#include <scratchcpp/comment.h> |
5 | 6 | #include <scratchcpp/costume.h> |
6 | 7 | #include <scratchcpp/sound.h> |
7 | 8 | #include <enginemock.h> |
@@ -183,6 +184,89 @@ TEST(TargetTest, Blocks) |
183 | 184 | ASSERT_EQ(source.greenFlagBlocks(), std::vector<std::shared_ptr<Block>>({ b1, b4 })); |
184 | 185 | } |
185 | 186 |
|
| 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 | + |
186 | 270 | TEST(TargetTest, CostumeIndex) |
187 | 271 | { |
188 | 272 | Target target; |
|
0 commit comments