Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 import sys
      6 import os
      7 import unittest
      8 
      9 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
     10 BUILD_TOOLS_DIR = os.path.dirname(SCRIPT_DIR)
     11 
     12 sys.path.append(BUILD_TOOLS_DIR)
     13 
     14 import sdk_tools.config as config
     15 
     16 class TestSdkToolsConfig(unittest.TestCase):
     17   def testInvalidSyntax(self):
     18     invalid_json = "# oops\n"
     19     cfg = config.Config()
     20     self.assertRaises(config.Error, lambda: cfg.LoadJson(invalid_json))
     21 
     22   def testEmptyConfig(self):
     23     """Test that empty config contains just empty sources list."""
     24     expected = '{\n  "sources": []\n}'
     25     cfg = config.Config()
     26     json_output = cfg.ToJson()
     27     self.assertEqual(json_output, expected)
     28 
     29   def testIntegerSetting(self):
     30     json_input = '{ "setting": 3 }'
     31     cfg = config.Config()
     32     cfg.LoadJson(json_input)
     33     self.assertEqual(cfg.setting, 3)
     34 
     35   def testReadWrite(self):
     36     json_input1 = '{\n  "sources": [], \n  "setting": 3\n}'
     37     json_input2 = '{\n  "setting": 3\n}'
     38     for json_input in (json_input1, json_input2):
     39       cfg = config.Config()
     40       cfg.LoadJson(json_input)
     41       json_output = cfg.ToJson()
     42       self.assertEqual(json_output, json_input1)
     43 
     44   def testAddSource(self):
     45     cfg = config.Config()
     46     cfg.AddSource('http://localhost/foo')
     47     json_output = cfg.ToJson()
     48     expected = '{\n  "sources": [\n    "http://localhost/foo"\n  ]\n}'
     49     self.assertEqual(json_output, expected)
     50 
     51 
     52 if __name__ == '__main__':
     53   unittest.main()
     54