Merge: Perfize nitcc_runtime
[nit.git] / contrib / nitrpg / src / test_statistics.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Test module for `stats.nit`
18 module test_statistics is test_suite
19
20 import test_helper
21 import statistics
22
23 class TestGame
24 super NitrpgTestHelper
25
26 fun test_game_stats do
27 var db = load_db("test_game_stats")
28 var game = load_game("privat/nit", db)
29 var stats = game.stats
30 assert stats.overall["test"] == 0
31 stats.overall.inc("test")
32 assert stats.overall["test"] == 1
33 stats.save
34 var ogame = load_game("privat/nit", db)
35 var ostats = ogame.stats
36 ostats.overall.inc("test")
37 assert ostats.overall["test"] == 2
38 db.drop
39 end
40 end
41
42 class TestPlayer
43 super NitrpgTestHelper
44
45 fun test_player_stats do
46 var db = load_db("test_player_stats")
47 var game = load_game("privat/nit", db)
48 var player = new Player(game, "Morriar")
49 var stats = player.stats
50 assert stats.overall["test"] == 0
51 stats.overall.inc("test")
52 assert stats.overall["test"] == 1
53 stats.save
54 var oplayer = new Player(game, "Morriar")
55 var ostats = oplayer.stats
56 ostats.overall.inc("test")
57 assert ostats.overall["test"] == 2
58 db.drop
59 end
60 end
61
62 class TestGameStats
63 super NitrpgTestHelper
64
65 fun test_init_from_json do
66 var db = load_db("test_init_from_json")
67 var game = load_game("privat/nit", db)
68 var owner = new Player(game, "Morriar")
69 var json = """{
70 "period": "2015",
71 "owner": "Morriar",
72 "values": {
73 "test1": 10,
74 "test2": 20
75 }
76 }""".parse_json.as(JsonObject)
77 var stats = new GameStats.from_json(game, "2015", owner, json)
78 assert stats["test0"] == 0
79 assert stats["test1"] == 10
80 assert stats["test2"] == 20
81 db.drop
82 end
83 end