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 import json_schema
      7 import unittest
      8 
      9 class JsonSchemaUnittest(unittest.TestCase):
     10   def testNocompile(self):
     11     compiled = [
     12       {
     13         "namespace": "compile",
     14         "description": "The compile API.",
     15         "functions": [],
     16         "types":     {}
     17       },
     18 
     19       {
     20         "namespace": "functions",
     21         "description": "The functions API.",
     22         "functions": [
     23           {
     24             "id": "two"
     25           },
     26           {
     27             "id": "four"
     28           }
     29         ],
     30 
     31         "types": {
     32           "one": { "key": "value" }
     33         }
     34       },
     35 
     36       {
     37         "namespace": "types",
     38         "description": "The types API.",
     39         "functions": [
     40           { "id": "one" }
     41         ],
     42         "types": {
     43           "two": {
     44             "key": "value"
     45           },
     46           "four": {
     47             "key": "value"
     48           }
     49         }
     50       },
     51 
     52       {
     53         "namespace": "nested",
     54         "description": "The nested API.",
     55         "properties": {
     56           "sync": {
     57             "functions": [
     58               {
     59                 "id": "two"
     60               },
     61               {
     62                 "id": "four"
     63               }
     64             ],
     65             "types": {
     66               "two": {
     67                 "key": "value"
     68               },
     69               "four": {
     70                 "key": "value"
     71               }
     72             }
     73           }
     74         }
     75       }
     76     ]
     77 
     78     schema = json_schema.CachedLoad('test/json_schema_test.json')
     79     self.assertEquals(compiled, json_schema.DeleteNodes(schema, 'nocompile'))
     80 
     81     def should_delete(value):
     82       return isinstance(value, dict) and not value.get('valid', True)
     83     expected = [
     84       {'one': {'test': 'test'}},
     85       {'valid': True},
     86       {}
     87     ]
     88     given = [
     89       {'one': {'test': 'test'}, 'two': {'valid': False}},
     90       {'valid': True},
     91       {},
     92       {'valid': False}
     93     ]
     94     self.assertEquals(
     95         expected, json_schema.DeleteNodes(given, matcher=should_delete))
     96 
     97 
     98 if __name__ == '__main__':
     99   unittest.main()
    100