Home | History | Annotate | Download | only in wardmodem
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import global_state
      6 
      7 import logging
      8 import unittest
      9 
     10 import wardmodem_exceptions as wme
     11 
     12 class GlobalStateSkeletonBadTestCase(unittest.TestCase):
     13     """
     14     Test failing derivations of GlobalStateSkeleton.
     15 
     16     """
     17 
     18     def test_duplicate_component_name(self):
     19         """
     20         Try (unsuccessfully) to add two components with the same name.
     21 
     22         """
     23         state = global_state.GlobalStateSkeleton()
     24         state._add_state_component('common_name', ['THIS_IS_FINE'])
     25         self.assertRaises(wme.WardModemSetupException,
     26                           state._add_state_component,
     27                           'common_name',
     28                           ['THIS_IS_NOT_FINE'])
     29 
     30 
     31     def test_ill_formed_names(self):
     32         """
     33         Try (unsuccessfully) to add components with ill formed names or ill
     34         formed values.
     35 
     36         """
     37         state = global_state.GlobalStateSkeleton()
     38         self.assertRaises(TypeError,
     39                           state._add_state_component,
     40                           'this_is_fine',
     41                           'must_have_been_list')
     42         self.assertRaises(wme.WardModemSetupException,
     43                           state._add_state_component,
     44                           'ill formed',
     45                           ['NO_SPACES'])
     46         self.assertRaises(wme.WardModemSetupException,
     47                           state._add_state_component,
     48                           '',
     49                           ['CANT_BE_EMPTY'])
     50         self.assertRaises(wme.WardModemSetupException,
     51                           state._add_state_component,
     52                           'ILL_FORMED',
     53                           ['MUST_BE_LOWERCASE'])
     54         self.assertRaises(wme.WardModemSetupException,
     55                           state._add_state_component,
     56                           'no_spaces',
     57                           ['ILL FORMED'])
     58         self.assertRaises(wme.WardModemSetupException,
     59                           state._add_state_component,
     60                           'cant_be_empty',
     61                           [''])
     62         self.assertRaises(wme.WardModemSetupException,
     63                           state._add_state_component,
     64                           'use_int_when_you_want_numbers',
     65                           ['2'])
     66         self.assertRaises(wme.WardModemSetupException,
     67                           state._add_state_component,
     68                           'must_be_uppercase',
     69                           ['ill_formed'])
     70 
     71 
     72     def test_valid_names(self):
     73         """
     74         Some examples of correct component additions.
     75 
     76         """
     77         state = global_state.GlobalStateSkeleton()
     78 
     79         state._add_state_component('this_is_fine', ['A', 'B', 'C'])
     80         state._add_state_component('so_is_this', [1, 1, 2, 3, 5, 8, 13])
     81         state._add_state_component('and_even_this_guy', ['A', 'B3B_CC', 34])
     82 
     83 
     84 class GlobalStateSkeletonTestCase(unittest.TestCase):
     85     """
     86     Test the basic functionality of GlobalStateSkeleton, assuming that it is
     87     derived without errors.
     88 
     89     """
     90 
     91     class TestGlobalState(global_state.GlobalStateSkeleton):
     92         """
     93         This class will correctly derive from GlobalStateSkeleton.
     94 
     95         """
     96 
     97         def __init__(self):
     98             super(GlobalStateSkeletonTestCase.TestGlobalState, self).__init__()
     99             # Now, add all state components.
    100             self._add_state_component('comp1', ['ALLOWED_VALUE_1_1'])
    101             self._add_state_component('comp2', ['ALLOWED_VALUE_2_1',
    102                                                 'ALLOWED_VALUE_2_2'])
    103             # No value can ever be assigned to this. Ah, what the heck!
    104             self._add_state_component('comp3', [])
    105 
    106 
    107     def setUp(self):
    108         self.state = GlobalStateSkeletonTestCase.TestGlobalState()
    109 
    110 
    111     def test_successful_read_write(self):
    112         """
    113         Test that all values are initialized correctly.
    114 
    115         """
    116         self.assertEqual(self.state.INVALID_VALUE, self.state['comp1'])
    117         self.assertEqual(self.state.INVALID_VALUE, self.state['comp2'])
    118         self.assertEqual(self.state.INVALID_VALUE, self.state['comp3'])
    119 
    120         self.state['comp2'] = 'ALLOWED_VALUE_2_1'
    121         self.assertEqual('ALLOWED_VALUE_2_1', self.state['comp2'])
    122         self.state['comp2'] = 'ALLOWED_VALUE_2_2'
    123         self.assertEqual('ALLOWED_VALUE_2_2', self.state['comp2'])
    124         self.state['comp1'] = 'ALLOWED_VALUE_1_1'
    125         self.assertEqual('ALLOWED_VALUE_1_1', self.state['comp1'])
    126 
    127 
    128     def _read(self, key):
    129         """Wrap the read from state to check exceptions raised."""
    130         return self.state[key]
    131 
    132     def _write(self, key, value):
    133         """Wrap the assignment to state to check exceptions raised."""
    134         self.state[key] = value
    135 
    136 
    137     def test_failed_read_write(self):
    138         """
    139         Attempt to read/write invalid values.
    140 
    141         """
    142         self.assertRaises(wme.StateMachineException,
    143                           self._read, 'some_invalid_var')
    144         self.assertRaises(wme.StateMachineException,
    145                           self._write, 'some_invalide_var', '')
    146         self.assertRaises(wme.StateMachineException,
    147                           self._write, 'comp1', 'DOES_NOT_EXIST')
    148 
    149 
    150 if __name__ == '__main__':
    151     logging.basicConfig(level=logging.DEBUG)
    152     unittest.main()
    153