|
| 1 | +#include <scratchcpp/target.h> |
1 | 2 | #include <scratchcpp/variable.h> |
2 | 3 | #include <scratchcpp/list.h> |
3 | | -#include <scratchcpp/stage.h> |
| 4 | +#include <scratchcpp/block.h> |
4 | 5 | #include <scratchcpp/costume.h> |
5 | 6 | #include <scratchcpp/sound.h> |
6 | 7 |
|
7 | 8 | #include "../common.h" |
8 | 9 |
|
9 | 10 | using namespace libscratchcpp; |
10 | 11 |
|
11 | | -TEST(TargetTest, FindCostume) |
| 12 | +TEST(TargetTest, IsStage) |
12 | 13 | { |
13 | | - Stage stage; |
14 | | - Costume c1("costume1", "", "svg"); |
15 | | - Costume c2("costume2", "", "png"); |
16 | | - Costume c3("costume3", "", "svg"); |
17 | | - stage.addCostume(c1); |
18 | | - stage.addCostume(c2); |
19 | | - stage.addCostume(c3); |
20 | | - |
21 | | - ASSERT_EQ(stage.findCostume("invalid"), -1); |
22 | | - ASSERT_EQ(stage.findCostume("costume1"), 0); |
23 | | - ASSERT_EQ(stage.findCostume("costume2"), 1); |
24 | | - ASSERT_EQ(stage.findCostume("costume3"), 2); |
| 14 | + Target target; |
| 15 | + ASSERT_FALSE(target.isStage()); |
25 | 16 | } |
26 | 17 |
|
27 | | -TEST(TargetTest, FindSound) |
| 18 | +TEST(TargetTest, Name) |
28 | 19 | { |
29 | | - Stage stage; |
30 | | - Sound s1("sound1", "", "svg"); |
31 | | - Sound s2("sound2", "", "png"); |
32 | | - Sound s3("sound3", "", "svg"); |
33 | | - stage.addSound(s1); |
34 | | - stage.addSound(s2); |
35 | | - stage.addSound(s3); |
36 | | - |
37 | | - ASSERT_EQ(stage.findSound("invalid"), -1); |
38 | | - ASSERT_EQ(stage.findSound("sound1"), 0); |
39 | | - ASSERT_EQ(stage.findSound("sound2"), 1); |
40 | | - ASSERT_EQ(stage.findSound("sound3"), 2); |
| 20 | + Target target; |
| 21 | + target.setName("Test"); |
| 22 | + ASSERT_EQ(target.name(), "Test"); |
41 | 23 | } |
42 | 24 |
|
43 | | -TEST(TargetTest, FindVariable) |
| 25 | +TEST(TargetTest, Variables) |
44 | 26 | { |
45 | | - Stage stage; |
46 | | - auto v1 = std::make_shared<Variable>("a", "variable1"); |
47 | | - auto v2 = std::make_shared<Variable>("b", "variable2"); |
48 | | - auto v3 = std::make_shared<Variable>("c", "variable3"); |
49 | | - stage.addVariable(v1); |
50 | | - stage.addVariable(v2); |
51 | | - stage.addVariable(v3); |
52 | | - |
53 | | - ASSERT_EQ(stage.findVariable("invalid"), -1); |
54 | | - ASSERT_EQ(stage.findVariable("variable1"), 0); |
55 | | - ASSERT_EQ(stage.findVariable("variable2"), 1); |
56 | | - ASSERT_EQ(stage.findVariable("variable3"), 2); |
57 | | - |
58 | | - ASSERT_EQ(stage.findVariableById("d"), -1); |
59 | | - ASSERT_EQ(stage.findVariableById("a"), 0); |
60 | | - ASSERT_EQ(stage.findVariableById("b"), 1); |
61 | | - ASSERT_EQ(stage.findVariableById("c"), 2); |
| 27 | + auto v1 = std::make_shared<Variable>("a", "var1"); |
| 28 | + auto v2 = std::make_shared<Variable>("b", "var2"); |
| 29 | + auto v3 = std::make_shared<Variable>("c", "var3"); |
| 30 | + |
| 31 | + Target target; |
| 32 | + ASSERT_EQ(target.addVariable(v1), 0); |
| 33 | + ASSERT_EQ(target.addVariable(v2), 1); |
| 34 | + ASSERT_EQ(target.addVariable(v3), 2); |
| 35 | + ASSERT_EQ(target.addVariable(v2), 1); // add existing variable |
| 36 | + |
| 37 | + ASSERT_EQ(target.variables(), std::vector<std::shared_ptr<Variable>>({ v1, v2, v3 })); |
| 38 | + ASSERT_EQ(target.variableAt(0), v1); |
| 39 | + ASSERT_EQ(target.variableAt(1), v2); |
| 40 | + ASSERT_EQ(target.variableAt(2), v3); |
| 41 | + ASSERT_EQ(target.variableAt(3), nullptr); |
| 42 | + ASSERT_EQ(target.variableAt(-1), nullptr); |
| 43 | + |
| 44 | + ASSERT_EQ(target.findVariable("invalid"), -1); |
| 45 | + ASSERT_EQ(target.findVariable("var1"), 0); |
| 46 | + ASSERT_EQ(target.findVariable("var2"), 1); |
| 47 | + ASSERT_EQ(target.findVariable("var3"), 2); |
| 48 | + |
| 49 | + ASSERT_EQ(target.findVariableById("d"), -1); |
| 50 | + ASSERT_EQ(target.findVariableById("a"), 0); |
| 51 | + ASSERT_EQ(target.findVariableById("b"), 1); |
| 52 | + ASSERT_EQ(target.findVariableById("c"), 2); |
62 | 53 | } |
63 | 54 |
|
64 | | -TEST(TargetTest, FindList) |
| 55 | +TEST(TargetTest, Lists) |
65 | 56 | { |
66 | | - Stage stage; |
67 | 57 | auto l1 = std::make_shared<List>("a", "list1"); |
68 | 58 | auto l2 = std::make_shared<List>("b", "list2"); |
69 | 59 | auto l3 = std::make_shared<List>("c", "list3"); |
70 | | - stage.addList(l1); |
71 | | - stage.addList(l2); |
72 | | - stage.addList(l3); |
73 | | - |
74 | | - ASSERT_EQ(stage.findList("invalid"), -1); |
75 | | - ASSERT_EQ(stage.findList("list1"), 0); |
76 | | - ASSERT_EQ(stage.findList("list2"), 1); |
77 | | - ASSERT_EQ(stage.findList("list3"), 2); |
78 | | - |
79 | | - ASSERT_EQ(stage.findListById("d"), -1); |
80 | | - ASSERT_EQ(stage.findListById("a"), 0); |
81 | | - ASSERT_EQ(stage.findListById("b"), 1); |
82 | | - ASSERT_EQ(stage.findListById("c"), 2); |
| 60 | + |
| 61 | + Target target; |
| 62 | + ASSERT_EQ(target.addList(l1), 0); |
| 63 | + ASSERT_EQ(target.addList(l2), 1); |
| 64 | + ASSERT_EQ(target.addList(l3), 2); |
| 65 | + ASSERT_EQ(target.addList(l2), 1); // add existing list |
| 66 | + |
| 67 | + ASSERT_EQ(target.lists(), std::vector<std::shared_ptr<List>>({ l1, l2, l3 })); |
| 68 | + ASSERT_EQ(target.listAt(0), l1); |
| 69 | + ASSERT_EQ(target.listAt(1), l2); |
| 70 | + ASSERT_EQ(target.listAt(2), l3); |
| 71 | + ASSERT_EQ(target.listAt(3), nullptr); |
| 72 | + ASSERT_EQ(target.listAt(-1), nullptr); |
| 73 | + |
| 74 | + ASSERT_EQ(target.findList("invalid"), -1); |
| 75 | + ASSERT_EQ(target.findList("list1"), 0); |
| 76 | + ASSERT_EQ(target.findList("list2"), 1); |
| 77 | + ASSERT_EQ(target.findList("list3"), 2); |
| 78 | + |
| 79 | + ASSERT_EQ(target.findListById("d"), -1); |
| 80 | + ASSERT_EQ(target.findListById("a"), 0); |
| 81 | + ASSERT_EQ(target.findListById("b"), 1); |
| 82 | + ASSERT_EQ(target.findListById("c"), 2); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(TargetTest, Blocks) |
| 86 | +{ |
| 87 | + auto b1 = std::make_shared<Block>("a", "event_whenflagclicked"); |
| 88 | + auto b2 = std::make_shared<Block>("b", "motion_gotoxy"); |
| 89 | + auto b3 = std::make_shared<Block>("c", "motion_ifonedgebounce"); |
| 90 | + auto b4 = std::make_shared<Block>("d", "event_whenflagclicked"); |
| 91 | + |
| 92 | + Target target; |
| 93 | + ASSERT_EQ(target.addBlock(b1), 0); |
| 94 | + ASSERT_EQ(target.addBlock(b2), 1); |
| 95 | + ASSERT_EQ(target.addBlock(b3), 2); |
| 96 | + ASSERT_EQ(target.addBlock(b4), 3); |
| 97 | + ASSERT_EQ(target.addBlock(b2), 1); // add existing block |
| 98 | + |
| 99 | + ASSERT_EQ(target.blocks(), std::vector<std::shared_ptr<Block>>({ b1, b2, b3, b4 })); |
| 100 | + ASSERT_EQ(target.blockAt(0), b1); |
| 101 | + ASSERT_EQ(target.blockAt(1), b2); |
| 102 | + ASSERT_EQ(target.blockAt(2), b3); |
| 103 | + ASSERT_EQ(target.blockAt(3), b4); |
| 104 | + ASSERT_EQ(target.blockAt(4), nullptr); |
| 105 | + ASSERT_EQ(target.blockAt(-1), nullptr); |
| 106 | + |
| 107 | + ASSERT_EQ(target.findBlock("e"), -1); |
| 108 | + ASSERT_EQ(target.findBlock("a"), 0); |
| 109 | + ASSERT_EQ(target.findBlock("b"), 1); |
| 110 | + ASSERT_EQ(target.findBlock("c"), 2); |
| 111 | + ASSERT_EQ(target.findBlock("d"), 3); |
| 112 | + |
| 113 | + ASSERT_EQ(target.greenFlagBlocks(), std::vector<std::shared_ptr<Block>>({ b1, b4 })); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(TargetTest, CurrentCostume) |
| 117 | +{ |
| 118 | + Target target; |
| 119 | + ASSERT_EQ(target.currentCostume(), 1); |
| 120 | + target.setCurrentCostume(5); |
| 121 | + ASSERT_EQ(target.currentCostume(), 5); |
| 122 | +} |
| 123 | + |
| 124 | +TEST(TargetTest, Costumes) |
| 125 | +{ |
| 126 | + Costume c1("costume1", "", "svg"); |
| 127 | + Costume c2("costume2", "", "png"); |
| 128 | + Costume c3("costume3", "", "svg"); |
| 129 | + |
| 130 | + Target target; |
| 131 | + ASSERT_EQ(target.addCostume(c1), 0); |
| 132 | + ASSERT_EQ(target.addCostume(c2), 1); |
| 133 | + ASSERT_EQ(target.addCostume(c3), 2); |
| 134 | + ASSERT_EQ(target.addCostume(c2), 1); // add existing costume |
| 135 | + |
| 136 | + // TODO: Use shared_ptr for assets |
| 137 | + // ASSERT_EQ(target.costumes(), std::vector<std::shared_ptr<Costume>>({ c1, c2, c3 })); |
| 138 | + ASSERT_EQ(target.costumes().size(), 3); |
| 139 | + ASSERT_EQ(target.costumes()[0].name(), c1.name()); |
| 140 | + ASSERT_EQ(target.costumes()[1].name(), c2.name()); |
| 141 | + ASSERT_EQ(target.costumes()[2].name(), c3.name()); |
| 142 | + |
| 143 | + ASSERT_EQ(target.costumeAt(0).name(), c1.name()); |
| 144 | + ASSERT_EQ(target.costumeAt(1).name(), c2.name()); |
| 145 | + ASSERT_EQ(target.costumeAt(2).name(), c3.name()); |
| 146 | + |
| 147 | + ASSERT_EQ(target.findCostume("invalid"), -1); |
| 148 | + ASSERT_EQ(target.findCostume("costume1"), 0); |
| 149 | + ASSERT_EQ(target.findCostume("costume2"), 1); |
| 150 | + ASSERT_EQ(target.findCostume("costume3"), 2); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(TargetTest, Sounds) |
| 154 | +{ |
| 155 | + Sound s1("sound1", "", "mp3"); |
| 156 | + Sound s2("sound2", "", "wav"); |
| 157 | + Sound s3("sound3", "", "mp3"); |
| 158 | + |
| 159 | + Target target; |
| 160 | + ASSERT_EQ(target.addSound(s1), 0); |
| 161 | + ASSERT_EQ(target.addSound(s2), 1); |
| 162 | + ASSERT_EQ(target.addSound(s3), 2); |
| 163 | + ASSERT_EQ(target.addSound(s2), 1); // add existing Sound |
| 164 | + |
| 165 | + // TODO: Use shared_ptr for assets |
| 166 | + // ASSERT_EQ(target.sounds(), std::vector<std::shared_ptr<Sound>>({ s1, s2, s3 })); |
| 167 | + ASSERT_EQ(target.sounds().size(), 3); |
| 168 | + ASSERT_EQ(target.sounds()[0].name(), s1.name()); |
| 169 | + ASSERT_EQ(target.sounds()[1].name(), s2.name()); |
| 170 | + ASSERT_EQ(target.sounds()[2].name(), s3.name()); |
| 171 | + |
| 172 | + ASSERT_EQ(target.soundAt(0).name(), s1.name()); |
| 173 | + ASSERT_EQ(target.soundAt(1).name(), s2.name()); |
| 174 | + ASSERT_EQ(target.soundAt(2).name(), s3.name()); |
| 175 | + |
| 176 | + ASSERT_EQ(target.findSound("invalid"), -1); |
| 177 | + ASSERT_EQ(target.findSound("sound1"), 0); |
| 178 | + ASSERT_EQ(target.findSound("sound2"), 1); |
| 179 | + ASSERT_EQ(target.findSound("sound3"), 2); |
| 180 | +} |
| 181 | + |
| 182 | +TEST(TargetTest, LayerOrder) |
| 183 | +{ |
| 184 | + Target target; |
| 185 | + ASSERT_EQ(target.layerOrder(), 0); |
| 186 | + target.setLayerOrder(2); |
| 187 | + ASSERT_EQ(target.layerOrder(), 2); |
| 188 | +} |
| 189 | + |
| 190 | +TEST(TargetTest, Volume) |
| 191 | +{ |
| 192 | + Target target; |
| 193 | + ASSERT_EQ(target.volume(), 100); |
| 194 | + target.setVolume(50); |
| 195 | + ASSERT_EQ(target.volume(), 50); |
83 | 196 | } |
0 commit comments