1#!/usr/bin/env python
2import config
3import animation
4"""Tests for various pywi modules."""
5
6import os
7import StringIO
8import unittest
9
10source_dir = os.path.dirname(os.path.abspath(__file__))
11root_dir = os.path.normpath(source_dir + '/../..')
12
13
14class TestConfig(unittest.TestCase):
15
16    def sample_text(self):
17        return """
18key = this is global
19
20[section]
21foo=bar # comment
22# comment
23baz= quux
24"""
25
26    def test_read(self):
27        c = config.read(StringIO.StringIO(self.sample_text()))
28        s = c.get_section('section')
29        self.assertEqual(s['foo'], 'bar')
30        self.assertEqual(s['baz'], 'quux')
31        self.assertFalse('none' in s)
32
33    def test_itersections(self):
34        c = config.read(StringIO.StringIO(self.sample_text()))
35        self.assertEqual(
36            [name for name, section in c.itersections()], ['section'])
37
38    def test_modify(self):
39        c = config.read(StringIO.StringIO(self.sample_text()))
40        s = c.get_section('section')
41        s.set('foo', 'other')
42        s.set('tst', 'blah')
43        self.assertEqual(s['foo'], 'other')
44        self.assertEqual(s['tst'], 'blah')
45        stringio = StringIO.StringIO()
46        c.write(stringio)
47        c = config.read(StringIO.StringIO(stringio.getvalue()))
48        s = c.get_section('section')
49        self.assertEqual(s['foo'], 'other')
50        self.assertEqual(s['baz'], 'quux')
51        self.assertEqual(s['tst'], 'blah')
52
53    def test_remove_section(self):
54        c = config.read(StringIO.StringIO(self.sample_text()))
55        s = c.make_section('other')
56        s.set('x', 'y')
57        c.remove_section('other')
58        self.assertEqual(
59            [name for name, section in c.itersections()], ['section'])
60        s = c.get_section('section')
61        self.assertItemsEqual([(k, v) for k, v in s.iterentries()], [
62                              ('foo', 'bar'), ('baz', 'quux')])
63
64    def test_make_section(self):
65        c = config.read(StringIO.StringIO(self.sample_text()))
66        s = c.make_section('section')
67        self.assertEqual([(k, v) for k, v in s.iterentries()], [])
68        s.set('foo', 'written')
69        s2 = c.make_section('new')
70        s2.set('bar', 'baz')
71        stringio = StringIO.StringIO()
72        c.write(stringio)
73        c = config.read(StringIO.StringIO(stringio.getvalue()))
74        s = c.get_section('section')
75        self.assertItemsEqual([(k, v) for k, v in s.iterentries()], [
76                              ('foo', 'written')])
77        s2 = c.get_section('new')
78        self.assertItemsEqual([(k, v)
79                               for k, v in s2.iterentries()], [('bar', 'baz')])
80
81    def test_makers(self):
82        c = config.File()
83        s1 = c.make_section('sec1')
84        s1.set('0', '0')
85        s1.set('1', '1')
86        s2 = c.make_section('sec2')
87        s2.set('a', 'a')
88        s2.set('b', 'b')
89        self.assertItemsEqual([(k, v) for k, v in s1.iterentries()], [
90                              ('0', '0'), ('1', '1')])
91        self.assertItemsEqual([(k, v) for k, v in s2.iterentries()], [
92                              ('a', 'a'), ('b', 'b')])
93        stringio = StringIO.StringIO()
94        c.write(stringio)
95        c = config.read(StringIO.StringIO(stringio.getvalue()))
96        s1 = c.get_section('sec1')
97        s2 = c.get_section('sec2')
98        self.assertItemsEqual([(k, v) for k, v in s1.iterentries()], [
99                              ('0', '0'), ('1', '1')])
100        self.assertItemsEqual([(k, v) for k, v in s2.iterentries()], [
101                              ('a', 'a'), ('b', 'b')])
102
103
104class TestAnimation(unittest.TestCase):
105
106    def test_load_conf(self):
107        # This needs to be updated when the game data changes
108        tests = [
109            {
110                'directory': root_dir + '/data/tribes/barbarians/carrier',
111                'animation': 'idle',
112                'shape': (28, 21),
113                'hotspot': (21, 14),
114                'has_player_color': True,
115                'nrframes': 100,
116            },
117        ]
118
119        for test in tests:
120            anim = animation.load_conf(test['directory'], test['animation'])
121            self.assertEqual(anim.shape, test['shape'])
122            self.assertEqual(anim.has_player_color, test['has_player_color'])
123            self.assertEqual(anim.get_nrframes(), test['nrframes'])
124
125
126if __name__ == '__main__':
127    unittest.main()
128