• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from unittest import TestCase
3from metadata_model import *
4from metadata_helpers import *
5
6class TestHelpers(TestCase):
7
8  def test_enum_calculate_value_string(self):
9    def compare_values_against_list(expected_list, enum):
10      for (idx, val) in enumerate(expected_list):
11        self.assertEquals(val,
12                          enum_calculate_value_string(list(enum.values)[idx]))
13
14    plain_enum = Enum(parent=None, values=['ON', 'OFF'])
15
16    compare_values_against_list(['0', '1'],
17                                plain_enum)
18
19    ###
20    labeled_enum = Enum(parent=None, values=['A', 'B', 'C'], ids={
21      'A': '12345',
22      'B': '0xC0FFEE',
23      'C': '0xDEADF00D'
24    })
25
26    compare_values_against_list(['12345', '0xC0FFEE', '0xDEADF00D'],
27                                labeled_enum)
28
29    ###
30    mixed_enum = Enum(parent=None,
31                      values=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
32                      ids={
33                        'C': '0xC0FFEE',
34                        'E': '123',
35                        'G': '0xDEADF00D'
36                      })
37
38    expected_values = ['0', '1', '0xC0FFEE', '0xC0FFEF', '123', '124',
39                       '0xDEADF00D',
40                       '0xDEADF00E']
41
42    compare_values_against_list(expected_values, mixed_enum)
43
44  def test_enumerate_with_last(self):
45    empty_list = []
46
47    for (x, y) in enumerate_with_last(empty_list):
48      self.fail("Should not return anything for empty list")
49
50    single_value = [1]
51    for (x, last) in enumerate_with_last(single_value):
52      self.assertEquals(1, x)
53      self.assertEquals(True, last)
54
55    multiple_values = [4, 5, 6]
56    lst = list(enumerate_with_last(multiple_values))
57    self.assertListEqual([(4, False), (5, False), (6, True)], lst)
58
59if __name__ == '__main__':
60    unittest.main()
61