Debian Codegraph @

tests/test-scores.vala

raw
1 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright © 2014 Nikhar Agrawal
4 * Copyright © 2016 Michael Catanzaro <[email protected]>
5 *
6 * This file is part of libgnome-games-support.
7 *
8 * libgnome-games-support is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * libgnome-games-support is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with libgnome-games-support. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Games {
23 namespace Scores {
24
25 private Category? category_request (string category_key)
26 {
27 // TODO: This isn't tested....
28 return new Category (category_key, category_key);
29 }
30
31 private void add_score_sync (Context context, int score, Category category) {
32 var main_loop = new MainLoop (MainContext.@default (), false);
33 context.add_score.begin (score, category, null, (object, result) => {
34 try
35 {
36 context.add_score.end (result);
37 }
38 catch (Error e)
39 {
40 assert_not_reached ();
41 }
42 main_loop.quit ();
43 });
44 main_loop.run ();
45 }
46
47 private void create_scores ()
48 {
49 Context context = new Context ("libgnome-games-support-test", "Games Type", null, category_request, Style.POINTS_GREATER_IS_BETTER);
50 Category cat = new Category ("cat1", "cat1");
51 add_score_sync (context, 101, cat);
52 add_score_sync (context, 102, cat);
53
54 cat.key = "cat2";
55 cat.name = "cat2";
56 add_score_sync (context, 21, cat);
57 add_score_sync (context, 24, cat);
58 }
59
60 private string get_test_directory_name ()
61 {
62 return Path.build_filename (Environment.get_user_data_dir (), "libgnome-games-support-test", null);
63 }
64
65 private string get_score_directory_name ()
66 {
67 return Path.build_filename (get_test_directory_name (), "scores", null);
68 }
69
70 private string get_score_filename_for_category (string category_name)
71 {
72 return Path.build_filename (get_score_directory_name (), category_name);
73 }
74
75 private void delete_scores ()
76 {
77 try
78 {
79 var directory = File.new_for_path (get_score_directory_name ());
80 if (directory.query_exists ())
81 {
82 var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
83 FileInfo file_info;
84 while ((file_info = enumerator.next_file ()) != null)
85 {
86 var file_name = file_info.get_name ();
87 var file = directory.get_child (file_name);
88 file.@delete ();
89 }
90 directory.@delete ();
91 }
92
93 var parent_directory = File.new_for_path (get_test_directory_name ());
94 if (parent_directory.query_exists ())
95 parent_directory.@delete ();
96 }
97 catch (Error e)
98 {
99 error (e.message);
100 }
101 }
102
103 private void test_scores_files_exist ()
104 {
105 create_scores ();
106
107 var filename = get_score_filename_for_category ("cat1");
108 var file = File.new_for_path (filename);
109 assert (file.query_exists ());
110
111 filename = get_score_filename_for_category ("cat2");
112 file = File.new_for_path (filename);
113 assert (file.query_exists ());
114 }
115
116 private void test_save_score_to_file ()
117 {
118 try
119 {
120 create_scores ();
121 var filename = get_score_filename_for_category ("cat1");
122 var file = File.new_for_path (filename);
123 var dis = new DataInputStream (file.read ());
124
125 string line;
126 assert ((line = dis.read_line (null)) != null);
127
128 var tokens = line.split (" ", 3);
129 assert (tokens.length == 3);
130 assert (tokens[0] == "101");
131 assert ((line = dis.read_line (null)) != null);
132
133 tokens = line.split (" ", 3);
134 assert (tokens.length == 3);
135 assert (tokens[0] == "102");
136 assert ((line = dis.read_line (null)) == null);
137
138 filename = get_score_filename_for_category ("cat2");
139 file = File.new_for_path (filename);
140 dis = new DataInputStream (file.read ());
141 assert ((line = dis.read_line (null)) != null);
142
143 tokens = line.split (" ", 3);
144 assert (tokens.length == 3);
145 assert (tokens[0] == "21");
146 assert ((line = dis.read_line (null)) != null);
147
148 tokens = line.split (" ", 3);
149 assert (tokens.length == 3);
150 assert (tokens[0] == "24");
151 assert ((line = dis.read_line (null)) == null);
152 }
153 catch (Error e)
154 {
155 error (e.message);
156 }
157 }
158
159 private void test_import_from_score_directory ()
160 {
161 try
162 {
163 var expected_score = new Score (42,
164 new DateTime.now_local ().to_unix (),
165 Environment.get_real_name ());
166 var category = new Category ("new-cat", "");
167
168 var test_directory = File.new_for_path (get_test_directory_name ());
169 test_directory.make_directory_with_parents ();
170
171 var old_scores_file = File.new_for_path (Path.build_filename (get_test_directory_name (),
172 "old-cat",
173 null));
174 var stream = old_scores_file.append_to (FileCreateFlags.NONE);
175 stream.write_all (@"$(expected_score.score) $(expected_score.time)\n".data, null);
176 stream.close ();
177 assert (old_scores_file.query_exists ());
178
179 var context = new Context.with_importer (
180 "libgnome-games-support-test",
181 "",
182 null,
183 (key) => {
184 assert (key == "new-cat");
185 return category;
186 },
187 Games.Scores.Style.POINTS_GREATER_IS_BETTER,
188 new Games.Scores.DirectoryImporter.with_convert_func ((old_key) => {
189 assert (old_key == "old-cat");
190 return "new-cat";
191 }));
192 assert (!old_scores_file.query_exists ());
193
194 var imported_scores = context.get_high_scores (category);
195 assert (imported_scores.size == 1);
196
197 var imported_score = imported_scores.first ();
198 assert (Score.equals (expected_score, imported_score));
199 }
200 catch (Error e)
201 {
202 error (e.message);
203 }
204 }
205
206 private void test_import_from_history_file ()
207 {
208 try
209 {
210 var expected_score = new Score (42,
211 1455426945,
212 Environment.get_real_name ());
213 var category = new Category ("new-cat", "");
214
215 var test_directory = File.new_for_path (get_test_directory_name ());
216 test_directory.make_directory_with_parents ();
217
218 var old_scores_file = File.new_for_path (Path.build_filename (get_test_directory_name (),
219 "history",
220 null));
221 var stream = old_scores_file.append_to (FileCreateFlags.NONE);
222 stream.write_all ("2016-02-13T23:15:45-0600 old-cat 42\n".data, null);
223 stream.close ();
224 assert (old_scores_file.query_exists ());
225
226 var context = new Context.with_importer (
227 "libgnome-games-support-test",
228 "",
229 null,
230 (key) => {
231 assert (key == "new-cat");
232 return category;
233 },
234 Games.Scores.Style.POINTS_GREATER_IS_BETTER,
235 new Games.Scores.HistoryFileImporter ((line, out score, out out_category) => {
236 assert (line == "2016-02-13T23:15:45-0600 old-cat 42");
237 score = new Score (42, HistoryFileImporter.parse_date ("2016-02-13T23:15:45-0600"));
238 assert (score.time == 1455426945);
239 out_category = category;
240 }));
241 assert (!old_scores_file.query_exists ());
242
243 var imported_scores = context.get_high_scores (category);
244 assert (imported_scores.size == 1);
245
246 var imported_score = imported_scores.first ();
247 assert (Score.equals (expected_score, imported_score));
248 }
249 catch (Error e)
250 {
251 error (e.message);
252 }
253 }
254
255 private void test_import_from_nonexistent_history_file ()
256 {
257 new Context.with_importer (
258 "libgnome-games-support-test",
259 "",
260 null,
261 (key) => {
262 return null;
263 },
264 Games.Scores.Style.POINTS_GREATER_IS_BETTER,
265 new Games.Scores.HistoryFileImporter ((line, out score, out category) => {
266 score = null;
267 category = null;
268 }));
269
270 /* No error */
271 }
272
273 public int main (string[] args)
274 {
275 /* Start fresh.... */
276 delete_scores ();
277
278 Test.init (ref args);
279 var test_suite = TestSuite.get_root ();
280 test_suite.add (new TestCase ("Scores Files Created", () => {}, test_scores_files_exist, delete_scores));
281 test_suite.add (new TestCase ("Save Score to File", () => {}, test_save_score_to_file, delete_scores));
282 test_suite.add (new TestCase ("Import from Score Directory", () => {}, test_import_from_score_directory, delete_scores));
283 test_suite.add (new TestCase ("Import from History File", () => {}, test_import_from_history_file, delete_scores));
284 test_suite.add (new TestCase ("Import from Nonexistent History File", () => {}, test_import_from_nonexistent_history_file, delete_scores));
285 return Test.run ();
286 }
287
288 }
289 }