Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
example default
f = "asdf"
struct TestResult2
f String
"test doc"
example default
f = "asdf"
route TestRoute:2 (TestArg2, TestResult2, Void)
"test doc"
""")
route_whitelist_filter = {
"route_whitelist": {"test": ["*"]},
"datatype_whitelist": {}
}
api = specs_to_ir([('test.stone', text)],
route_whitelist_filter=route_whitelist_filter)
self._compare_namespace_names(api, ['test'])
self._compare_datatype_names(api.namespaces['test'],
['TestArg', 'TestArg2', 'TestResult', 'TestResult2'])
alias M = Map(String, Int32)
""")
api = specs_to_ir([('test.stone', text)])
m_alias = api.namespaces['test'].alias_by_name['M']
self.assertIsInstance(m_alias, Alias)
self.assertIsInstance(m_alias.data_type, Map)
# maps of maps
text = textwrap.dedent("""\
namespace test
alias M = Map(String, Map(String, Int32))
""")
api = specs_to_ir([('test.stone', text)])
m_alias = api.namespaces['test'].alias_by_name['M']
self.assertIsInstance(m_alias.data_type.value_data_type, Map)
# Map type errors with 0 args
text = textwrap.dedent("""\
namespace test
alias M = Map()
""")
# map type errors with only 1 args
with self.assertRaises(InvalidSpec):
specs_to_ir([('test.stone', text)])
text = textwrap.dedent("""\
namespace test
""")
api = specs_to_ir([('test.stone', text)])
test_ns = api.namespaces['test']
self.assertIsInstance(test_ns.alias_by_name['T'], Alias)
self.assertIsInstance(test_ns.alias_by_name['R'], Alias)
self.assertIsInstance(test_ns.alias_by_name['R'].data_type, Alias)
self.assertEqual(test_ns.alias_by_name['R'].data_type.name, 'T')
# Test order invariance
text = textwrap.dedent("""\
namespace test
alias R = T
alias T = String
""")
api = specs_to_ir([('test.stone', text)])
# Try re-definition
text = textwrap.dedent("""\
namespace test
alias A = String
alias A = UInt64
""")
with self.assertRaises(InvalidSpec) as cm:
specs_to_ir([('test.stone', text)])
self.assertIn("Symbol 'A' already defined (test.stone:3).",
cm.exception.msg)
# Try cyclical reference
text = textwrap.dedent("""\
namespace test
u = default
union U
a
b S2?
example default
b = default
struct S2
f String
example default
f = "F"
""")
api = specs_to_ir([('test.stone', text)])
s_dt = api.namespaces['test'].data_type_by_name['S']
self.assertEqual(s_dt.get_examples()['default'].value,
{'u': {'.tag': 'b', 'f': 'F'}})
# Test TagRef
text = textwrap.dedent("""\
namespace test
union U
a
b
struct S
u U = a
example default
text = textwrap.dedent("""\
namespace test
struct T
g Int64
struct S
f T
"Dimensions for a photo or video."
struct
a String
b Int64
""")
with self.assertRaises(InvalidSpec) as cm:
specs_to_ir([('ns1.stone', text)])
self.assertEqual(
"Symbol 'T' already defined (ns1.stone:3).",
cm.exception.msg)
self.assertEqual(cm.exception.lineno, 9)
self.assertEqual(cm.exception.path, 'ns1.stone')
f String
"Only field doc"
union U
"Only type doc"
f String
union V
f String
"Only field doc"
# Check for inherited doc
struct W extends T
g String
""")
api = specs_to_ir([('test.stone', text)])
E_dt = api.namespaces['test'].data_type_by_name['E']
self.assertFalse(E_dt.has_documented_type_or_fields())
self.assertFalse(E_dt.has_documented_fields())
S_dt = api.namespaces['test'].data_type_by_name['S']
self.assertTrue(S_dt.has_documented_type_or_fields())
self.assertFalse(S_dt.has_documented_fields())
T_dt = api.namespaces['test'].data_type_by_name['T']
self.assertTrue(T_dt.has_documented_type_or_fields())
self.assertTrue(T_dt.has_documented_fields())
U_dt = api.namespaces['test'].data_type_by_name['U']
self.assertTrue(U_dt.has_documented_type_or_fields())
self.assertFalse(U_dt.has_documented_fields())
cm.exception.msg)
self.assertEqual(cm.exception.lineno, 2)
self.assertEqual(cm.exception.path, 'test.stone')
# Test missing attribute for route attribute with default
stone_cfg_text = textwrap.dedent("""\
namespace stone_cfg
struct Route
f1 String = "yay"
""")
test_text = textwrap.dedent("""\
namespace test
route r1(Void, Void, Void)
""")
api = specs_to_ir([
('stone_cfg.stone', stone_cfg_text), ('test.stone', test_text)])
ns1 = api.namespaces['test']
self.assertEquals(ns1.route_by_name['r1'].attrs['f1'], 'yay')
# Test missing attribute for route attribute with optional
stone_cfg_text = textwrap.dedent("""\
namespace stone_cfg
struct Route
f1 String?
""")
test_text = textwrap.dedent("""\
namespace test
route r1(Void, Void, Void)
""")
api = specs_to_ir([
# Test duplicate field name -- earlier being in a parent type
text = textwrap.dedent("""\
namespace test
struct A
a UInt64
struct B extends A
b String
struct C extends B
a String
""")
with self.assertRaises(InvalidSpec) as cm:
specs_to_ir([('test.stone', text)])
self.assertIn('already defined in parent', cm.exception.msg)
# Test extending from wrong type
text = textwrap.dedent("""\
namespace test
union A
a
struct B extends A
b UInt64
""")
with self.assertRaises(InvalidSpec) as cm:
specs_to_ir([('test.stone', text)])
self.assertIn('struct can only extend another struct', cm.exception.msg)
s = api.namespaces['test'].data_type_by_name['S']
self.assertIsInstance(s.get_examples()['default'].value, dict)
# error when example doesn't match definition
text = textwrap.dedent("""\
namespace test
struct S
m Map(String, String)
example default
m = {"one": 1}
""")
with self.assertRaises(InvalidSpec):
specs_to_ir([('test.stone', text)])
for err in route_filter_errors:
print(err, file=sys.stderr)
sys.exit(1)
else:
route_filter = None
if args.route_whitelist_filter:
with open(args.route_whitelist_filter) as f:
route_whitelist_filter = json.loads(f.read())
else:
route_whitelist_filter = None
try:
# TODO: Needs version
api = specs_to_ir(specs, debug=debug,
route_whitelist_filter=route_whitelist_filter)
except InvalidSpec as e:
print('%s:%s: error: %s' % (e.path, e.lineno, e.msg), file=sys.stderr)
if debug:
print('A traceback is included below in case this is a bug in '
'Stone.\n', traceback.format_exc(), file=sys.stderr)
sys.exit(1)
if api is None:
print('You must fix the above parsing errors for generation to '
'continue.', file=sys.stderr)
sys.exit(1)
if args.whitelist_namespace_routes:
for namespace_name in args.whitelist_namespace_routes:
if namespace_name not in api.namespaces:
print('error: Whitelisted namespace missing from spec: %s' %