Merge: concurrent_collections: Add implementation of has method
[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
19
20 import test_helper
21 import statistics
22
23 class TestGame
24 super NitrpgTestHelper
25 test
26
27 fun test_game_stats is test do
28 var db = gen_test_db
29 var game = load_game("privat/nit", db)
30 var stats = game.stats
31 assert stats.overall["test"] == 0
32 stats.overall.inc("test")
33 assert stats.overall["test"] == 1
34 stats.save
35 var ogame = load_game("privat/nit", db)
36 var ostats = ogame.stats
37 ostats.overall.inc("test")
38 assert ostats.overall["test"] == 2
39 end
40 end
41
42 class TestPlayer
43 super NitrpgTestHelper
44 test
45
46 fun test_player_stats is test do
47 var db = gen_test_db
48 var game = load_game("privat/nit", db)
49 var player = new Player(game, "Morriar")
50 var stats = player.stats
51 assert stats.overall["test"] == 0
52 stats.overall.inc("test")
53 assert stats.overall["test"] == 1
54 stats.save
55 var oplayer = new Player(game, "Morriar")
56 var ostats = oplayer.stats
57 ostats.overall.inc("test")
58 assert ostats.overall["test"] == 2
59 end
60 end
61
62 class TestGameStats
63 super NitrpgTestHelper
64 test
65
66 fun test_init_from_json is test do
67 var db = gen_test_db
68 var game = load_game("privat/nit", db)
69 var owner = new Player(game, "Morriar")
70 var json = """{
71 "period": "2015",
72 "owner": "Morriar",
73 "values": {
74 "test1": 10,
75 "test2": 20
76 }
77 }""".parse_json.as(JsonObject)
78 var stats = new GameStats.from_json(game, "2015", owner, json)
79 assert stats["test0"] == 0
80 assert stats["test1"] == 10
81 assert stats["test2"] == 20
82 end
83 end