Mercurial > malikania
comparison tests/libclient/js-sprite/main.cpp @ 30:a1e80d991968
Misc: convert to spaces, #519
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 16 Jun 2016 13:35:31 +0200 |
parents | 0a1adf7dcca0 |
children | d4f5f7231b84 |
comparison
equal
deleted
inserted
replaced
29:99792c6c8b06 | 30:a1e80d991968 |
---|---|
31 | 31 |
32 using namespace std::chrono_literals; | 32 using namespace std::chrono_literals; |
33 | 33 |
34 class TestSprite : public testing::Test { | 34 class TestSprite : public testing::Test { |
35 protected: | 35 protected: |
36 ResourcesLocatorDirectory m_locator; | 36 ResourcesLocatorDirectory m_locator; |
37 ClientResourcesLoader m_loader; | 37 ClientResourcesLoader m_loader; |
38 | 38 |
39 duk::Context m_ctx; | 39 duk::Context m_ctx; |
40 | 40 |
41 public: | 41 public: |
42 TestSprite() | 42 TestSprite() |
43 : m_locator(SOURCE_DIRECTORY "/resources") | 43 : m_locator(SOURCE_DIRECTORY "/resources") |
44 , m_loader(m_locator) | 44 , m_loader(m_locator) |
45 { | 45 { |
46 duk::putGlobal(m_ctx, "Malikania", duk::Object()); | 46 duk::putGlobal(m_ctx, "Malikania", duk::Object()); |
47 | 47 |
48 loadMalikaniaImage(m_ctx); | 48 loadMalikaniaImage(m_ctx); |
49 loadMalikaniaSprite(m_ctx); | 49 loadMalikaniaSprite(m_ctx); |
50 loadMalikaniaWindow(m_ctx); | 50 loadMalikaniaWindow(m_ctx); |
51 | 51 |
52 /* Store the loader */ | 52 /* Store the loader */ |
53 duk::putGlobal(m_ctx, "\xff""\xff""loader", &m_loader); | 53 duk::putGlobal(m_ctx, "\xff""\xff""loader", &m_loader); |
54 } | 54 } |
55 }; | 55 }; |
56 | 56 |
57 TEST_F(TestSprite, cell) | 57 TEST_F(TestSprite, cell) |
58 { | 58 { |
59 try { | 59 try { |
60 auto ret = duk::pevalString(m_ctx, | 60 auto ret = duk::pevalString(m_ctx, |
61 "s = new Malikania.Sprite('sprites/margins.json');" | 61 "s = new Malikania.Sprite('sprites/margins.json');" |
62 "w = s.cell.width;" | 62 "w = s.cell.width;" |
63 "h = s.cell.height;" | 63 "h = s.cell.height;" |
64 ); | 64 ); |
65 | 65 |
66 if (ret != 0) { | 66 if (ret != 0) { |
67 throw duk::exception(m_ctx, -1); | 67 throw duk::exception(m_ctx, -1); |
68 } | 68 } |
69 | 69 |
70 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "w")); | 70 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "w")); |
71 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "h")); | 71 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "h")); |
72 } catch (const std::exception &ex) { | 72 } catch (const std::exception &ex) { |
73 FAIL() << ex.what(); | 73 FAIL() << ex.what(); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 TEST_F(TestSprite, columns) | 77 TEST_F(TestSprite, columns) |
78 { | 78 { |
79 try { | 79 try { |
80 auto ret = duk::pevalString(m_ctx, | 80 auto ret = duk::pevalString(m_ctx, |
81 "s = new Malikania.Sprite('sprites/margins.json');" | 81 "s = new Malikania.Sprite('sprites/margins.json');" |
82 "n = s.columns;" | 82 "n = s.columns;" |
83 ); | 83 ); |
84 | 84 |
85 if (ret != 0) { | 85 if (ret != 0) { |
86 throw duk::exception(m_ctx, -1); | 86 throw duk::exception(m_ctx, -1); |
87 } | 87 } |
88 | 88 |
89 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "n")); | 89 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "n")); |
90 } catch (const std::exception &ex) { | 90 } catch (const std::exception &ex) { |
91 FAIL() << ex.what(); | 91 FAIL() << ex.what(); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 TEST_F(TestSprite, margins) | 95 TEST_F(TestSprite, margins) |
96 { | 96 { |
97 try { | 97 try { |
98 auto ret = duk::pevalString(m_ctx, | 98 auto ret = duk::pevalString(m_ctx, |
99 "s = new Malikania.Sprite('sprites/margins.json');" | 99 "s = new Malikania.Sprite('sprites/margins.json');" |
100 "w = s.margins.width;" | 100 "w = s.margins.width;" |
101 "h = s.margins.height;" | 101 "h = s.margins.height;" |
102 ); | 102 ); |
103 | 103 |
104 if (ret != 0) { | 104 if (ret != 0) { |
105 throw duk::exception(m_ctx, -1); | 105 throw duk::exception(m_ctx, -1); |
106 } | 106 } |
107 | 107 |
108 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "w")); | 108 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "w")); |
109 ASSERT_EQ(6, duk::getGlobal<int>(m_ctx, "h")); | 109 ASSERT_EQ(6, duk::getGlobal<int>(m_ctx, "h")); |
110 } catch (const std::exception &ex) { | 110 } catch (const std::exception &ex) { |
111 FAIL() << ex.what(); | 111 FAIL() << ex.what(); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 TEST_F(TestSprite, rows) | 115 TEST_F(TestSprite, rows) |
116 { | 116 { |
117 try { | 117 try { |
118 auto ret = duk::pevalString(m_ctx, | 118 auto ret = duk::pevalString(m_ctx, |
119 "s = new Malikania.Sprite('sprites/margins.json');" | 119 "s = new Malikania.Sprite('sprites/margins.json');" |
120 "n = s.rows;" | 120 "n = s.rows;" |
121 ); | 121 ); |
122 | 122 |
123 if (ret != 0) { | 123 if (ret != 0) { |
124 throw duk::exception(m_ctx, -1); | 124 throw duk::exception(m_ctx, -1); |
125 } | 125 } |
126 | 126 |
127 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "n")); | 127 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "n")); |
128 } catch (const std::exception &ex) { | 128 } catch (const std::exception &ex) { |
129 FAIL() << ex.what(); | 129 FAIL() << ex.what(); |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 TEST_F(TestSprite, space) | 133 TEST_F(TestSprite, space) |
134 { | 134 { |
135 try { | 135 try { |
136 auto ret = duk::pevalString(m_ctx, | 136 auto ret = duk::pevalString(m_ctx, |
137 "s = new Malikania.Sprite('sprites/margins.json');" | 137 "s = new Malikania.Sprite('sprites/margins.json');" |
138 "w = s.space.width;" | 138 "w = s.space.width;" |
139 "h = s.space.height;" | 139 "h = s.space.height;" |
140 ); | 140 ); |
141 | 141 |
142 if (ret != 0) { | 142 if (ret != 0) { |
143 throw duk::exception(m_ctx, -1); | 143 throw duk::exception(m_ctx, -1); |
144 } | 144 } |
145 | 145 |
146 ASSERT_EQ(2, duk::getGlobal<int>(m_ctx, "w")); | 146 ASSERT_EQ(2, duk::getGlobal<int>(m_ctx, "w")); |
147 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "h")); | 147 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "h")); |
148 } catch (const std::exception &ex) { | 148 } catch (const std::exception &ex) { |
149 FAIL() << ex.what(); | 149 FAIL() << ex.what(); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 TEST_F(TestSprite, basic) | 153 TEST_F(TestSprite, basic) |
154 { | 154 { |
155 try { | 155 try { |
156 auto ret = duk::pevalString(m_ctx, | 156 auto ret = duk::pevalString(m_ctx, |
157 "w = new Malikania.Window();" | 157 "w = new Malikania.Window();" |
158 "s = new Malikania.Sprite('sprites/margins.json');" | 158 "s = new Malikania.Sprite('sprites/margins.json');" |
159 "c = 0;" | 159 "c = 0;" |
160 ); | 160 ); |
161 | 161 |
162 if (ret != 0) { | 162 if (ret != 0) { |
163 throw duk::exception(m_ctx, -1); | 163 throw duk::exception(m_ctx, -1); |
164 } | 164 } |
165 | 165 |
166 for (unsigned c = 0; c < 12; ++c) { | 166 for (unsigned c = 0; c < 12; ++c) { |
167 auto ret = duk::pevalString(m_ctx, | 167 auto ret = duk::pevalString(m_ctx, |
168 "w.setDrawingColor('lightskyblue');" | 168 "w.setDrawingColor('lightskyblue');" |
169 "w.clear();" | 169 "w.clear();" |
170 "s.draw(w, c++, { x: 320 - 16, y: 240 - 16 });" | 170 "s.draw(w, c++, { x: 320 - 16, y: 240 - 16 });" |
171 "w.present();" | 171 "w.present();" |
172 ); | 172 ); |
173 | 173 |
174 if (ret != 0) { | 174 if (ret != 0) { |
175 throw duk::exception(m_ctx, -1); | 175 throw duk::exception(m_ctx, -1); |
176 } | 176 } |
177 | 177 |
178 std::this_thread::sleep_for(1s); | 178 std::this_thread::sleep_for(1s); |
179 } | 179 } |
180 } catch (const std::exception &ex) { | 180 } catch (const std::exception &ex) { |
181 FAIL() << ex.what(); | 181 FAIL() << ex.what(); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 int main(int argc, char **argv) | 185 int main(int argc, char **argv) |
186 { | 186 { |
187 testing::InitGoogleTest(&argc, argv); | 187 testing::InitGoogleTest(&argc, argv); |
188 | 188 |
189 return RUN_ALL_TESTS(); | 189 return RUN_ALL_TESTS(); |
190 } | 190 } |