sd_alias: use DEVPATH instead of path_id to extract SCSI ID
[nas-tools.git] / input-eventd / gen / tests.py
1 # -*- coding: utf-8 -*-
2 # This file is part of the lacie-nas project
3 #
4 # Copyright 2011 Benoît Canet
5 #
6 # Authors:
7 # - Benoît Canet <benoit.canet@gmail.com>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 3.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>
20
21 from gen import *
22 from parser import *
23 import unittest
24 import subprocess
25
26 class TestSuite(unittest.TestCase):
27
28         def test_strip_comments(self):
29                 result = strip_comments("HELLO/* SDFDSFSD sdfsd */WORLD")
30                 self.assertEqual(result, "HELLOWORLD")
31
32         def test_parse_line(self):
33                 test_array = { '0x55' :  "VALUE" }
34
35                 array = parse_line("#define VALUE 0x55")
36                 self.assertEqual(array, test_array)
37
38                 array = parse_line("#define VALUE      0x55")
39                 self.assertEqual(array, test_array)
40
41                 array = parse_line("#define VALUE_GOOD      0x55")
42                 test_array = { '0x55' :  "VALUE_GOOD" }
43                 self.assertEqual(array, test_array)
44
45                 array = parse_line("#define VALUE-BAD      0x55")
46                 self.assertEqual(array, {})
47
48                 array = parse_line("#define VALUE 0x55 3454")
49                 self.assertEqual(array, {})
50
51                 array = parse_line(" #define KEY_POWER 116 /* SC System Power Down */")
52                 test_array = { '116' :  "KEY_POWER" }
53                 self.assertEqual(array, test_array)
54
55                 array = parse_line("#define EV_VERSION              0x010001")
56                 self.assertEqual(array, {})
57
58         def test_events(self):
59                 array = parse_file_by_prefix("test_parse_event.h", "EV_")
60
61                 test_array = { '0x69' : "EV_TEST",
62                                '0x70' : "EV_TAISTE" }
63
64                 self.assertEqual(array, test_array)
65
66         def test_get_event_type_list(self):
67                 array = parse_file_by_prefix("test_parse_event.h", "EV_")
68
69                 test_array = { '0x69' : "EV_TEST",
70                                '0x70' : "EV_TAISTE" }
71
72                 result = get_event_type_list(test_array)
73
74                 self.assertEqual(result.sort(), ["TEST", "TAISTE"].sort())
75         
76         def test_generate_table(self):
77                 test_array = { '0x69' : "EV_TEST",
78                                '0x70' : "EV_TAISTE" }
79                 
80                 test_string = """struct input_entry event_type[] = {
81         {.value = 0x70, .string = "EV_TAISTE"},
82         {.value = 0x69, .string = "EV_TEST"}
83 };
84
85 """
86
87                 result = generate_table("event_type", test_array)
88                 self.assertEqual(result, test_string)
89
90         def test_generate_index(self):
91                 test_array = { '0x69' : "EV_TEST",
92                                '0x70' : "EV_TAISTE" }
93                 test_list  = [ "TEST" ]
94                 codes_count = { "codes_test" : 2}
95
96                 result = generate_index(test_array, test_list, codes_count)
97                 test_string = """struct index_entry table_index[] = {
98         {.type = 0x69, .codes = codes_test, .count = 2}
99 };
100
101 """
102                 self.assertEqual(result, test_string)
103
104         def test_compilation(self):
105                 result = generate("/usr/include/linux/input.h")
106                 fd = open("/tmp/test_header.h", "w")
107                 fd.write(result)
108                 fd.close()
109
110                 result = subprocess.call(["gcc", "test_main.c",  \
111                                           "-I",  "/tmp", "-o", \
112                                           "/tmp/test"])
113                 
114                 self.assertEqual(result , 0)
115
116                 result = subprocess.call(["/tmp/test"])
117                 self.assertEqual(result, 0)
118
119 def run_test():
120         suite = unittest.TestLoader().loadTestsFromTestCase(TestSuite)
121         unittest.TextTestRunner(verbosity=2).run(suite)
122
123 if __name__ == '__main__':
124     unittest.main()