Mercurial > malikania
comparison tests/libclient/js-sprite/main.cpp @ 36:9af360f34c7d
Misc: use raw duktape API
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 10 Aug 2016 14:30:51 +0200 |
parents | d4f5f7231b84 |
children | a47a4477f347 |
comparison
equal
deleted
inserted
replaced
35:8e1241156034 | 36:9af360f34c7d |
---|---|
19 #include <chrono> | 19 #include <chrono> |
20 #include <thread> | 20 #include <thread> |
21 | 21 |
22 #include <gtest/gtest.h> | 22 #include <gtest/gtest.h> |
23 | 23 |
24 #include <malikania/client-resources-loader.hpp> | 24 #include <malikania/js-client-resources-loader.hpp> |
25 #include <malikania/js-image.hpp> | 25 #include <malikania/js-image.hpp> |
26 #include <malikania/js-sprite.hpp> | 26 #include <malikania/js-sprite.hpp> |
27 #include <malikania/js-window.hpp> | 27 #include <malikania/js-window.hpp> |
28 #include <malikania/resources-locator.hpp> | 28 #include <malikania/resources-locator.hpp> |
29 | 29 |
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 UniqueContext m_ctx; |
39 duk::Context m_ctx; | |
40 | 39 |
41 public: | 40 public: |
42 TestSprite() | 41 TestSprite() |
43 : m_locator(SOURCE_DIRECTORY "/resources") | 42 : m_locator(SOURCE_DIRECTORY "/resources") |
44 , m_loader(m_locator) | 43 , m_loader(m_locator) |
45 { | 44 { |
46 duk::putGlobal(m_ctx, "Malikania", duk::Object()); | 45 duk_push_object(m_ctx); |
47 | 46 duk_put_global_string(m_ctx, "Malikania"); |
48 loadMalikaniaImage(m_ctx); | 47 dukx_load_image(m_ctx); |
49 loadMalikaniaSprite(m_ctx); | 48 dukx_load_sprite(m_ctx); |
50 loadMalikaniaWindow(m_ctx); | 49 dukx_load_window(m_ctx); |
51 | 50 dukx_put_client_loader(m_ctx, &m_loader); |
52 /* Store the loader */ | 51 } |
53 duk::putGlobal(m_ctx, "\xff""\xff""loader", &m_loader); | |
54 } | |
55 }; | 52 }; |
56 | 53 |
57 TEST_F(TestSprite, cell) | 54 TEST_F(TestSprite, cell) |
58 { | 55 { |
59 try { | 56 try { |
60 auto ret = duk::pevalString(m_ctx, | 57 auto ret = duk_peval_string(m_ctx, |
61 "s = new Malikania.Sprite('sprites/margins.json');" | 58 "s = new Malikania.Sprite('sprites/margins.json');" |
62 "w = s.cell.width;" | 59 "w = s.cell.width;" |
63 "h = s.cell.height;" | 60 "h = s.cell.height;" |
64 ); | 61 ); |
65 | 62 |
66 if (ret != 0) { | 63 if (ret != 0) |
67 throw duk::exception(m_ctx, -1); | 64 throw dukx_exception(m_ctx, -1); |
68 } | |
69 | 65 |
70 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "w")); | 66 duk_get_global_string(m_ctx, "w"); |
71 ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "h")); | 67 ASSERT_EQ(32U, duk_to_uint(m_ctx, -1)); |
72 } catch (const std::exception &ex) { | 68 duk_pop(m_ctx); |
73 FAIL() << ex.what(); | 69 duk_get_global_string(m_ctx, "h"); |
74 } | 70 ASSERT_EQ(32U, duk_to_uint(m_ctx, -1)); |
71 duk_pop(m_ctx); | |
72 } catch (const std::exception &ex) { | |
73 FAIL() << ex.what(); | |
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_peval_string(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 dukx_exception(m_ctx, -1); |
87 } | |
88 | 87 |
89 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "n")); | 88 duk_get_global_string(m_ctx, "n"); |
90 } catch (const std::exception &ex) { | 89 ASSERT_EQ(4U, duk_to_uint(m_ctx, -1)); |
91 FAIL() << ex.what(); | 90 duk_pop(m_ctx); |
92 } | 91 } catch (const std::exception &ex) { |
92 FAIL() << ex.what(); | |
93 } | |
93 } | 94 } |
94 | 95 |
95 TEST_F(TestSprite, margins) | 96 TEST_F(TestSprite, margins) |
96 { | 97 { |
97 try { | 98 try { |
98 auto ret = duk::pevalString(m_ctx, | 99 auto ret = duk_peval_string(m_ctx, |
99 "s = new Malikania.Sprite('sprites/margins.json');" | 100 "s = new Malikania.Sprite('sprites/margins.json');" |
100 "w = s.margins.width;" | 101 "w = s.margins.width;" |
101 "h = s.margins.height;" | 102 "h = s.margins.height;" |
102 ); | 103 ); |
103 | 104 |
104 if (ret != 0) { | 105 if (ret != 0) |
105 throw duk::exception(m_ctx, -1); | 106 throw dukx_exception(m_ctx, -1); |
106 } | |
107 | 107 |
108 ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "w")); | 108 duk_get_global_string(m_ctx, "w"); |
109 ASSERT_EQ(6, duk::getGlobal<int>(m_ctx, "h")); | 109 ASSERT_EQ(4U, duk_to_uint(m_ctx, -1)); |
110 } catch (const std::exception &ex) { | 110 duk_pop(m_ctx); |
111 FAIL() << ex.what(); | 111 duk_get_global_string(m_ctx, "h"); |
112 } | 112 ASSERT_EQ(6U, duk_to_uint(m_ctx, -1)); |
113 duk_pop(m_ctx); | |
114 } catch (const std::exception &ex) { | |
115 FAIL() << ex.what(); | |
116 } | |
113 } | 117 } |
114 | 118 |
115 TEST_F(TestSprite, rows) | 119 TEST_F(TestSprite, rows) |
116 { | 120 { |
117 try { | 121 try { |
118 auto ret = duk::pevalString(m_ctx, | 122 auto ret = duk_peval_string(m_ctx, |
119 "s = new Malikania.Sprite('sprites/margins.json');" | 123 "s = new Malikania.Sprite('sprites/margins.json');" |
120 "n = s.rows;" | 124 "n = s.rows;" |
121 ); | 125 ); |
122 | 126 |
123 if (ret != 0) { | 127 if (ret != 0) |
124 throw duk::exception(m_ctx, -1); | 128 throw dukx_exception(m_ctx, -1); |
125 } | |
126 | 129 |
127 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "n")); | 130 duk_get_global_string(m_ctx, "n"); |
128 } catch (const std::exception &ex) { | 131 ASSERT_EQ(3U, duk_to_uint(m_ctx, -1)); |
129 FAIL() << ex.what(); | 132 duk_pop(m_ctx); |
130 } | 133 } catch (const std::exception &ex) { |
134 FAIL() << ex.what(); | |
135 } | |
131 } | 136 } |
132 | 137 |
133 TEST_F(TestSprite, space) | 138 TEST_F(TestSprite, space) |
134 { | 139 { |
135 try { | 140 try { |
136 auto ret = duk::pevalString(m_ctx, | 141 auto ret = duk_peval_string(m_ctx, |
137 "s = new Malikania.Sprite('sprites/margins.json');" | 142 "s = new Malikania.Sprite('sprites/margins.json');" |
138 "w = s.space.width;" | 143 "w = s.space.width;" |
139 "h = s.space.height;" | 144 "h = s.space.height;" |
140 ); | 145 ); |
141 | 146 |
142 if (ret != 0) { | 147 if (ret != 0) |
143 throw duk::exception(m_ctx, -1); | 148 throw dukx_exception(m_ctx, -1); |
144 } | |
145 | 149 |
146 ASSERT_EQ(2, duk::getGlobal<int>(m_ctx, "w")); | 150 duk_get_global_string(m_ctx, "w"); |
147 ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "h")); | 151 ASSERT_EQ(2U, duk_to_uint(m_ctx, -1)); |
148 } catch (const std::exception &ex) { | 152 duk_pop(m_ctx); |
149 FAIL() << ex.what(); | 153 duk_get_global_string(m_ctx, "h"); |
150 } | 154 ASSERT_EQ(3U, duk_to_uint(m_ctx, -1)); |
155 duk_pop(m_ctx); | |
156 } catch (const std::exception &ex) { | |
157 FAIL() << ex.what(); | |
158 } | |
151 } | 159 } |
152 | 160 |
153 TEST_F(TestSprite, basic) | 161 TEST_F(TestSprite, basic) |
154 { | 162 { |
155 try { | 163 try { |
156 auto ret = duk::pevalString(m_ctx, | 164 auto ret = duk_peval_string(m_ctx, |
157 "w = new Malikania.Window();" | 165 "w = new Malikania.Window();" |
158 "s = new Malikania.Sprite('sprites/margins.json');" | 166 "s = new Malikania.Sprite('sprites/margins.json');" |
159 "c = 0;" | 167 "c = 0;" |
160 ); | 168 ); |
161 | 169 |
162 if (ret != 0) { | 170 if (ret != 0) |
163 throw duk::exception(m_ctx, -1); | 171 throw dukx_exception(m_ctx, -1); |
164 } | |
165 | 172 |
166 for (unsigned c = 0; c < 12; ++c) { | 173 for (unsigned c = 0; c < 12; ++c) { |
167 auto ret = duk::pevalString(m_ctx, | 174 auto ret = duk_peval_string(m_ctx, |
168 "w.setDrawingColor('lightskyblue');" | 175 "w.setDrawingColor('lightskyblue');" |
169 "w.clear();" | 176 "w.clear();" |
170 "s.draw(w, c++, { x: 320 - 16, y: 240 - 16 });" | 177 "s.draw(w, c++, { x: 320 - 16, y: 240 - 16 });" |
171 "w.present();" | 178 "w.present();" |
172 ); | 179 ); |
173 | 180 |
174 if (ret != 0) { | 181 if (ret != 0) { |
175 throw duk::exception(m_ctx, -1); | 182 throw dukx_exception(m_ctx, -1); |
176 } | 183 } |
177 | 184 |
178 std::this_thread::sleep_for(1s); | 185 std::this_thread::sleep_for(1s); |
179 } | 186 } |
180 } catch (const std::exception &ex) { | 187 } catch (const std::exception &ex) { |
181 FAIL() << ex.what(); | 188 FAIL() << ex.what(); |
182 } | 189 } |
183 } | 190 } |
184 | 191 |
185 int main(int argc, char **argv) | 192 int main(int argc, char **argv) |
186 { | 193 { |
187 testing::InitGoogleTest(&argc, argv); | 194 testing::InitGoogleTest(&argc, argv); |