def test_indexable_different_types(self): idata = """ interface NumberOrStringDictionary { [index: string]: number | string; length: number; name: string; } """ target = """{ "NumberOrStringDictionary": { "index": { "indexed": { "type": [ "string" ] }, "type": [ "number", "string" ] }, "length": { "type": [ "number" ] }, "name": { "type": [ "string" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_named_function_types(self): idata = """ interface ClockInterface { currentTime: Date; setTime(d: Date): void; } """ target = """{ "ClockInterface": { "currentTime": { "type": [ "Date" ] }, "setTime": { "function": true, "parameters": { "d": { "type": [ "Date" ] } }, "type": [ "void" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_function_types(self): idata = """ interface SearchFunc{ (source: string, subString: string): boolean; } """ target = """{ "SearchFunc": { "anonymous_function": { "function": true, "parameters": { "source": { "type": [ "string" ] }, "subString": { "type": [ "string" ] } }, "type": [ "boolean" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_indexable_types(self): idata = """ interface NotOkay { [y: number]: Animal; [x: string]: Dog; } """ target = """{ "NotOkay": { "x": { "indexed": { "type": [ "string" ] }, "type": [ "Dog" ] }, "y": { "indexed": { "type": [ "number" ] }, "type": [ "Animal" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_string_index_signature(self): idata = """ interface SquareConfig { color?: string; width?: number; [propName: string]: any; } """ target = """{ "SquareConfig": { "color": { "optional": true, "type": [ "string" ] }, "propName": { "indexed": { "type": [ "string" ] }, "type": [ "any" ] }, "width": { "optional": true, "type": [ "number" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_simple_interface(self): idata = """ interface LabeledValue { label: string; } """ target = """{ "LabeledValue": { "label": { "type": [ "string" ] } } }""" self.assertEqual(transform(idata), target)
def test_extensions(self): idata = """ interface Square extends Shape { sideLength: number; } """ target = """{ "Square": { "extends": [ "Shape" ], "sideLength": { "type": [ "number" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_nested_types(self): idata = """ interface NumberOrStringDictionary { name : { value : string, value2 : number}; } """ target = """{ "NumberOrStringDictionary": { "name" : { "type" : { "value" : { "type" : [ "string"] }, "value2" : { "type" : ["number"] } } } } }""" self.assertEqualJSON(transform(idata), target)
def test_indexable_readonly_types(self): idata = """ interface ReadonlyStringArray { readonly [index: number]: string; } """ target = """{ "ReadonlyStringArray": { "index": { "indexed": { "type": [ "number" ] }, "readonly": true, "type": [ "string" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_const_properties(self): idata = """ interface Point { const x: number; const y: number; } """ target = """{ "Point": { "x": { "constant": true, "type": [ "number" ] }, "y": { "constant": true, "type": [ "number" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_readonly_properties(self): idata = """ interface Point { readonly x: number; readonly y: number; } """ target = """{ "Point": { "x": { "readonly": true, "type": [ "number" ] }, "y": { "readonly": true, "type": [ "number" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_inline_comment(self): idata = """ interface NumberOrStringDictionary { [index: string]: number | string; length: number; // ok, length is a number name: string; // ok, name is a string } """ target = """{ "NumberOrStringDictionary": { "index": { "indexed": { "type": [ "string" ] }, "type": [ "number", "string" ] }, "length": { "description": "// ok, length is a number\\n", "type": [ "number" ] }, "name": { "description": "// ok, name is a string\\n", "type": [ "string" ] } } }""" self.assertEqualJSON(transform(idata), target)
def test_optional_properties(self): idata = """ interface SquareConfig { color?: string; width?: number; } """ target = """{ "SquareConfig": { "color": { "optional": true, "type": [ "string" ] }, "width": { "optional": true, "type": [ "number" ] } } }""" self.assertEqual(transform(idata), target)