3
3
#include < scratchcpp/list.h>
4
4
#include < scratchcpp/dev/compiler.h>
5
5
#include < scratchcpp/dev/test/scriptbuilder.h>
6
+ #include < scratchcpp/script.h>
7
+ #include < scratchcpp/thread.h>
8
+ #include < scratchcpp/dev/executioncontext.h>
9
+ #include < scratchcpp/dev/executablecode.h>
6
10
#include < enginemock.h>
11
+ #include < randomgeneratormock.h>
7
12
8
13
#include " ../common.h"
9
14
#include " dev/blocks/listblocks.h"
10
15
11
16
using namespace libscratchcpp ;
12
17
using namespace libscratchcpp ::test;
13
18
19
+ using ::testing::Return;
20
+
14
21
class ListBlocksTest : public testing ::Test
15
22
{
16
23
public:
@@ -25,6 +32,7 @@ class ListBlocksTest : public testing::Test
25
32
Project m_project;
26
33
IEngine *m_engine = nullptr ;
27
34
EngineMock m_engineMock;
35
+ RandomGeneratorMock m_rng;
28
36
};
29
37
30
38
TEST_F (ListBlocksTest, AddToList)
@@ -59,3 +67,69 @@ TEST_F(ListBlocksTest, AddToList)
59
67
ASSERT_EQ (list1->toString (), " test true" );
60
68
ASSERT_EQ (list2->toString (), " 123 Hello world" );
61
69
}
70
+
71
+ TEST_F (ListBlocksTest, DeleteOfList)
72
+ {
73
+ auto target = std::make_shared<Sprite>();
74
+
75
+ auto list1 = std::make_shared<List>(" " , " " );
76
+ list1->append (" Lorem" );
77
+ list1->append (" ipsum" );
78
+ list1->append (" dolor" );
79
+ list1->append (123 );
80
+ list1->append (true );
81
+ target->addList (list1);
82
+
83
+ auto list2 = std::make_shared<List>(" " , " " );
84
+ list2->append (" Hello" );
85
+ list2->append (" world" );
86
+ list2->append (false );
87
+ list2->append (-543.5 );
88
+ list2->append (" abc" );
89
+ list2->append (52.4 );
90
+ target->addList (list2);
91
+
92
+ auto list3 = std::make_shared<List>(" " , " " );
93
+ list3->append (1 );
94
+ list3->append (2 );
95
+ list3->append (3 );
96
+ target->addList (list3);
97
+
98
+ ScriptBuilder builder (m_extension.get (), m_engine, target);
99
+
100
+ auto addTest = [&builder](const Value &index, std::shared_ptr<List> list) {
101
+ builder.addBlock (" data_deleteoflist" );
102
+ builder.addValueInput (" INDEX" , index);
103
+ builder.addEntityField (" LIST" , list);
104
+ return builder.currentBlock ();
105
+ };
106
+
107
+ auto block = addTest (1 , list1);
108
+ addTest (3 , list1);
109
+ addTest (2 , list1);
110
+ addTest (0 , list1);
111
+ addTest (3 , list1);
112
+
113
+ addTest (" last" , list2);
114
+ addTest (" random" , list2);
115
+ addTest (" any" , list2);
116
+
117
+ addTest (" all" , list3);
118
+
119
+ builder.build ();
120
+
121
+ Compiler compiler (&m_engineMock, target.get ());
122
+ auto code = compiler.compile (block);
123
+ Script script (target.get (), block, &m_engineMock);
124
+ script.setCode (code);
125
+ Thread thread (target.get (), &m_engineMock, &script);
126
+ auto ctx = code->createExecutionContext (&thread);
127
+ ctx->setRng (&m_rng);
128
+
129
+ EXPECT_CALL (m_rng, randint (1 , 5 )).WillOnce (Return (2 ));
130
+ EXPECT_CALL (m_rng, randint (1 , 4 )).WillOnce (Return (3 ));
131
+ code->run (ctx.get ());
132
+ ASSERT_EQ (list1->toString (), " ipsum true" );
133
+ ASSERT_EQ (list2->toString (), " Hello false abc" );
134
+ ASSERT_TRUE (list3->empty ());
135
+ }
0 commit comments