1+ # Note: Chose pytest over unittest(2) because of cleaner and more lightweight syntax.
2+ # Run tests with "py.test"
3+
4+ # TODO:
5+ # - Performance tests to measure the effect of optimizations
6+ import os
7+ import pytest
8+
9+ from sc2reader import Replay
10+ from sc2reader .exceptions import ParseError
11+
12+ # Helper functions
13+ def sent_to_all (msg ):
14+ return msg .target == 0
15+
16+ def find (f , seq ):
17+ for item in seq :
18+ if f (item ):
19+ return item
20+
21+
22+ # Parsing should fail for an empty file.
23+ def test_empty ():
24+ # Todo: Are we happy with it raising a ValueError? Should it be rather ParseError or something else?
25+ # Maybe a "production" mode would be nice to have, so that it simply returns a status of the parse without
26+ # raising an exception.
27+ with pytest .raises (ValueError ):
28+ Replay ("test_replays/corrupted/empty.sc2replay" )
29+
30+ # Tests for build 17811 replays
31+
32+ # TODO: Realm/region functionality is missing!
33+ def test_1 ():
34+ replay = Replay ("test_replays/build17811/1.sc2replay" )
35+
36+ assert replay .date == "20 Feb 2011 22:44:48"
37+ assert replay .length == (32 , 47 )
38+ assert replay .map == "Lost Temple"
39+ assert replay .build == 17811
40+ assert replay .releaseString == "1.2.2.17811"
41+ assert replay .speed == "Faster"
42+ assert replay .type == "1v1"
43+
44+ assert len (replay .players ) == 2
45+ emperor = find (lambda player : player .name == "Emperor" , replay .players )
46+ assert emperor .team == 1
47+ assert emperor .race == "Protoss"
48+ assert emperor .recorder == False
49+
50+ boom = find (lambda player : player .name == "Boom" , replay .players )
51+ assert boom .team == 2
52+ assert boom .race == "Terran"
53+ assert boom .recorder == True
54+
55+ for player in replay .players :
56+ assert player .type == "Human"
57+
58+ # Because it is a 1v1 and the recording player quit, we should know the winner.
59+ assert emperor .result == "Win"
60+ assert boom .result == "Lost"
61+
62+ assert emperor .url == "http://eu.battle.net/sc2/en/profile/520049/1/Emperor/"
63+ assert boom .url == "http://eu.battle.net/sc2/en/profile/1694745/1/Boom/"
64+
65+ assert len (replay .messages ) == 12
66+ assert find (lambda player : player .pid == replay .messages [0 ].player , replay .players ).name == "Emperor"
67+ assert replay .messages [0 ].text == "hf"
68+ assert replay .messages [1 ].text == "HEYA"
69+ assert replay .messages [2 ].text == "gl hf"
70+ assert replay .messages [3 ].text == "sry for caps"
71+ assert replay .messages [4 ].text == "^^"
72+ assert replay .messages [5 ].text == "noppe"
73+ assert replay .messages [6 ].text == "you greedy bastard"
74+ assert replay .messages [7 ].text == "ggg"
75+ assert replay .messages [8 ].text == "WG"
76+ assert replay .messages [9 ].text == "wg? :)"
77+ assert replay .messages [10 ].text == "wipe"
78+ assert replay .messages [11 ].text == "huh?"
79+ assert find (lambda player : player .pid == replay .messages [11 ].player , replay .players ).name == "Boom"
80+
81+ for msg in replay .messages :
82+ assert sent_to_all (msg ) == true
0 commit comments