root/trunk/tests/line.py

Revision 112, 4.2 kB (checked in by lgs, 4 years ago)

Remove whitespace

Line 
1# Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#
3# This file is part of PyCha.
4#
5# PyCha is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# PyCha is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19
20import cairo
21
22import pycha.line
23
24class PointTests(unittest.TestCase):
25
26    def test_point(self):
27        point = pycha.line.Point(2, 3, 1.0, 2.0, "test")
28        self.assertEqual(point.x, 2)
29        self.assertEqual(point.y, 3)
30        self.assertEqual(point.xval, 1.0)
31        self.assertEqual(point.yval, 2.0)
32        self.assertEqual(point.name, "test")
33
34class LineTests(unittest.TestCase):
35
36    def test_init(self):
37        ch = pycha.line.LineChart(None)
38        self.assertEqual(ch.points, [])
39
40    def test_updateChart(self):
41        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
42        dataset = (
43            ('dataset1', ([0, 1], [1, 1], [2, 3])),
44            ('dataset2', ([0, 2], [1, 0], [3, 4])),
45            )
46        ch = pycha.line.LineChart(surface)
47        ch.addDataset(dataset)
48        ch._updateXY()
49        ch._updateChart()
50
51        self.assertEqual(ch.minxval, 0)
52        self.assertEqual(ch.maxxval, 3)
53        self.assertEqual(ch.xrange, 3)
54        self.assertAlmostEqual(ch.xscale, 1/3.0, 4)
55        self.assertEqual(ch.minyval, 0)
56        self.assertEqual(ch.maxyval, 4)
57        self.assertEqual(ch.yrange, 4)
58        self.assertAlmostEqual(ch.yscale, 0.25, 4)
59
60        points = (
61            pycha.line.Point(0, 0.75, 0, 1, 'dataset1'),
62            pycha.line.Point(1/3.0, 0.75, 1, 1, 'dataset1'),
63            pycha.line.Point(2/3.0, 0.25, 2, 3, 'dataset1'),
64            pycha.line.Point(0, 0.5, 0, 2, 'dataset2'),
65            pycha.line.Point(1/3.0, 1, 1, 0, 'dataset2'),
66            pycha.line.Point(1, 0, 3, 4, 'dataset2'),
67        )
68        for i, point in enumerate(points):
69            p1, p2 = ch.points[i], point
70            self.assertEqual(p1.x, p2.x)
71            self.assertEqual(p1.y, p2.y)
72            self.assertAlmostEqual(p1.xval, p2.xval, 4)
73            self.assertAlmostEqual(p1.yval, p2.yval, 4)
74            self.assertEqual(p1.name, p2.name)
75
76    def test_updateChartWithNegatives(self):
77        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
78        dataset = (
79            ('dataset1', ([0, 1], [1, -2], [2, 3])),
80            ('dataset2', ([0, 2], [1, 0], [3, -4])),
81            )
82        ch = pycha.line.LineChart(surface)
83        ch.addDataset(dataset)
84        ch._updateXY()
85        ch._updateChart()
86        self.assertEqual(ch.minxval, 0)
87        self.assertEqual(ch.maxxval, 3)
88        self.assertEqual(ch.xrange, 3)
89        self.assertAlmostEqual(ch.xscale, 1/3.0, 4)
90        self.assertEqual(ch.minyval, -4)
91        self.assertEqual(ch.maxyval, 3)
92        self.assertEqual(ch.yrange, 7)
93        self.assertAlmostEqual(ch.yscale, 1/7.0, 4)
94
95        points = (
96            pycha.line.Point(0, 0.2857, 0, 1, 'dataset1'),
97            pycha.line.Point(1/3.0, 0.7143, 1, -2, 'dataset1'),
98            pycha.line.Point(2/3.0, 0.0, 2, 3, 'dataset1'),
99            pycha.line.Point(0, 0.1429, 0, 2, 'dataset2'),
100            pycha.line.Point(1/3.0, 0.4286, 1, 0, 'dataset2'),
101            pycha.line.Point(1, 1.0, 3, -4, 'dataset2'),
102        )
103        for i, point in enumerate(points):
104            p1, p2 = ch.points[i], point
105            self.assertAlmostEqual(p1.x, p2.x, 4)
106            self.assertAlmostEqual(p1.y, p2.y, 4)
107            self.assertAlmostEqual(p1.xval, p2.xval, 4)
108            self.assertAlmostEqual(p1.yval, p2.yval, 4)
109            self.assertEqual(p1.name, p2.name)
110
111def test_suite():
112    return unittest.TestSuite((
113        unittest.makeSuite(PointTests),
114        unittest.makeSuite(LineTests),
115    ))
116
117if __name__ == '__main__':
118    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.