Home | History | Annotate | Download | only in json_schema_compiler
      1 #!/usr/bin/env python
      2 # Copyright (c) 2012 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 
      6 from json_schema import CachedLoad
      7 import model
      8 import unittest
      9 
     10 class ModelTest(unittest.TestCase):
     11   def setUp(self):
     12     self.model = model.Model()
     13     self.permissions_json = CachedLoad('test/permissions.json')
     14     self.model.AddNamespace(self.permissions_json[0],
     15         'path/to/permissions.json')
     16     self.permissions = self.model.namespaces.get('permissions')
     17     self.windows_json = CachedLoad('test/windows.json')
     18     self.model.AddNamespace(self.windows_json[0],
     19         'path/to/window.json')
     20     self.windows = self.model.namespaces.get('windows')
     21     self.tabs_json = CachedLoad('test/tabs.json')
     22     self.model.AddNamespace(self.tabs_json[0],
     23         'path/to/tabs.json')
     24     self.tabs = self.model.namespaces.get('tabs')
     25 
     26   def testNamespaces(self):
     27     self.assertEquals(3, len(self.model.namespaces))
     28     self.assertTrue(self.permissions)
     29 
     30   def testHasFunctions(self):
     31     self.assertEquals(["contains", "getAll", "remove", "request"],
     32         sorted(self.permissions.functions.keys()))
     33 
     34   def testHasTypes(self):
     35     self.assertEquals(['Tab'], self.tabs.types.keys())
     36     self.assertEquals(['Permissions'], self.permissions.types.keys())
     37     self.assertEquals(['Window'], self.windows.types.keys())
     38 
     39   def testHasProperties(self):
     40     self.assertEquals(["active", "favIconUrl", "highlighted", "id",
     41         "incognito", "index", "pinned", "selected", "status", "title", "url",
     42         "windowId"],
     43         sorted(self.tabs.types['Tab'].properties.keys()))
     44 
     45   def testProperties(self):
     46     string_prop = self.tabs.types['Tab'].properties['status']
     47     self.assertEquals(model.PropertyType.STRING,
     48                       string_prop.type_.property_type)
     49     integer_prop = self.tabs.types['Tab'].properties['id']
     50     self.assertEquals(model.PropertyType.INTEGER,
     51                       integer_prop.type_.property_type)
     52     array_prop = self.windows.types['Window'].properties['tabs']
     53     self.assertEquals(model.PropertyType.ARRAY,
     54                       array_prop.type_.property_type)
     55     self.assertEquals(model.PropertyType.REF,
     56                       array_prop.type_.item_type.property_type)
     57     self.assertEquals('tabs.Tab', array_prop.type_.item_type.ref_type)
     58     object_prop = self.tabs.functions['query'].params[0]
     59     self.assertEquals(model.PropertyType.OBJECT,
     60                       object_prop.type_.property_type)
     61     self.assertEquals(
     62         ["active", "highlighted", "pinned", "status", "title", "url",
     63          "windowId", "windowType"],
     64         sorted(object_prop.type_.properties.keys()))
     65 
     66   def testChoices(self):
     67     self.assertEquals(model.PropertyType.CHOICES,
     68                       self.tabs.functions['move'].params[0].type_.property_type)
     69 
     70   def testPropertyNotImplemented(self):
     71     (self.permissions_json[0]['types'][0]
     72         ['properties']['permissions']['type']) = 'something'
     73     self.assertRaises(model.ParseException, self.model.AddNamespace,
     74         self.permissions_json[0], 'path/to/something.json')
     75 
     76   def testDescription(self):
     77     self.assertFalse(
     78         self.permissions.functions['contains'].params[0].description)
     79     self.assertEquals('True if the extension has the specified permissions.',
     80         self.permissions.functions['contains'].callback.params[0].description)
     81 
     82   def testPropertyUnixName(self):
     83     param = self.tabs.functions['move'].params[0]
     84     self.assertEquals('tab_ids', param.unix_name)
     85 
     86   def testUnixName(self):
     87     expectations = {
     88       'foo': 'foo',
     89       'fooBar': 'foo_bar',
     90       'fooBarBaz': 'foo_bar_baz',
     91       'fooBARBaz': 'foo_bar_baz',
     92       'fooBAR': 'foo_bar',
     93       'FOO': 'foo',
     94       'FOOBar': 'foo_bar',
     95       'foo.bar': 'foo_bar',
     96       'foo.BAR': 'foo_bar',
     97       'foo.barBAZ': 'foo_bar_baz',
     98       'foo_Bar_Baz_box': 'foo_bar_baz_box',
     99       }
    100     for name in expectations:
    101       self.assertEquals(expectations[name], model.UnixName(name));
    102 
    103 if __name__ == '__main__':
    104   unittest.main()
    105