Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/python
      2 
      3 # Copyright 2014 Google Inc.
      4 #
      5 # Use of this source code is governed by a BSD-style license that can be
      6 # found in the LICENSE file.
      7 
      8 """
      9 Test the VarsDict.
     10 """
     11 
     12 import sys
     13 import test_variables
     14 import unittest
     15 
     16 sys.path.append(test_variables.GYP_GEN_DIR)
     17 
     18 import vars_dict_lib
     19 from vars_dict_lib import OrderedSet
     20 from vars_dict_lib import VarsDict
     21 from vars_dict_lib import VAR_NAMES
     22 
     23 class VarsDictTest(unittest.TestCase):
     24   """
     25   Tests for the VarsDict class.
     26   """
     27 
     28   # May not be needed.
     29   def setUp(self):
     30     self.__vars_dict = VarsDict()
     31 
     32   def assert_consistency(self, v_dict):
     33     self.assertIs(v_dict.LOCAL_CFLAGS, v_dict['LOCAL_CFLAGS'])
     34     self.assertIs(v_dict.LOCAL_CPPFLAGS, v_dict['LOCAL_CPPFLAGS'])
     35     self.assertIs(v_dict.LOCAL_SRC_FILES, v_dict['LOCAL_SRC_FILES'])
     36     self.assertIs(v_dict.LOCAL_SHARED_LIBRARIES,
     37                   v_dict['LOCAL_SHARED_LIBRARIES'])
     38     self.assertIs(v_dict.LOCAL_STATIC_LIBRARIES,
     39                   v_dict['LOCAL_STATIC_LIBRARIES'])
     40     self.assertIs(v_dict.LOCAL_C_INCLUDES, v_dict['LOCAL_C_INCLUDES'])
     41     self.assertIs(v_dict.LOCAL_EXPORT_C_INCLUDE_DIRS,
     42                   v_dict['LOCAL_EXPORT_C_INCLUDE_DIRS'])
     43     self.assertIs(v_dict.KNOWN_TARGETS, v_dict['KNOWN_TARGETS'])
     44 
     45   def test_creation(self):
     46     v_dict = VarsDict()
     47     # VarsDict has one entry for each label in VAR_NAMES
     48     self.assertEqual(len(v_dict.keys()), len(VAR_NAMES))
     49     for key in v_dict.keys():
     50       self.assertIn(key, VAR_NAMES)
     51       # Each entry is an empty OrderedSet
     52       self.assertIsNotNone(v_dict[key])
     53       self.assertIsInstance(v_dict[key], OrderedSet)
     54       self.assertEqual(len(v_dict[key]), 0)
     55       self.assert_consistency(v_dict)
     56 
     57   def test_intersection(self):
     58     v_dict_list = []
     59     RANGE = 10
     60     for i in range(RANGE):
     61       v_dict = VarsDict()
     62       # Add something common to each field, as well as a unique entry
     63       for key in v_dict.keys():
     64         v_dict[key].add(key.lower())
     65         v_dict[key].add(str(i))
     66 
     67       self.assert_consistency(v_dict)
     68 
     69       v_dict_list.append(v_dict)
     70 
     71     intersection = vars_dict_lib.intersect(v_dict_list)
     72 
     73     self.assert_consistency(intersection)
     74 
     75     for key in intersection.keys():
     76       # Each field had one common item
     77       self.assertEqual(len(intersection[key]), 1)
     78       for item in intersection[key]:
     79         for other_v_dict in v_dict_list:
     80           self.assertNotIn(item, other_v_dict[key])
     81 
     82 
     83 def main():
     84   loader = unittest.TestLoader()
     85   suite = loader.loadTestsFromTestCase(VarsDictTest)
     86   unittest.TextTestRunner(verbosity=2).run(suite)
     87 
     88 if __name__ == "__main__":
     89   main()
     90 
     91