def clean( self, *args, **kwargs ): super().clean( *args, **kwargs ) errors = {} if not name_regex.match( self.name ): errors[ 'name' ] = 'BluePrint name "{0}" is invalid'.format( self.name ) results = parser.lint( self.script ) if results is not None: errors[ 'script' ] = 'Script is invalid: {0}'.format( results ) if errors: raise ValidationError( errors )
def test_assignment(): with pytest.raises(ParseError) as e: node = parse('asdf=') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('asdf=') == 'Incomplete Parsing on line: 1 column: 1' with pytest.raises(ParseError) as e: node = parse('asdf =') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('asdf =') == 'Incomplete Parsing on line: 1 column: 1' node = parse('asdf=5') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': None, 'name': 'asdf' }), 'value': ('C', 5) }), 1)] }) node = parse('asdf = 5') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': None, 'name': 'asdf' }), 'value': ('C', 5) }), 1)] }) node = parse('asdf.fdsa = 5') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': 'asdf', 'name': 'fdsa' }), 'value': ('C', 5) }), 1)] }) node = parse('asdf = myfunc()') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': None, 'name': 'asdf' }), 'value': ('F', { 'module': None, 'name': 'myfunc', 'paramaters': {} }) }), 1)] }) node = parse('asdf = myval') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': None, 'name': 'asdf' }), 'value': ('V', { 'module': None, 'name': 'myval' }) }), 1)] }) node = parse('asdf = not myval') assert node == ('S', { '_children': [('L', ('A', { 'target': ('V', { 'module': None, 'name': 'asdf' }), 'value': ('X', { 'operator': 'not', 'left': ('V', { 'module': None, 'name': 'myval' }), 'right': ('C', None) }) }), 1)] }) node = parse('asdf[3] = myval') assert node == ('S', { '_children': [('L', ('A', { 'target': ('R', { 'module': None, 'name': 'asdf', 'index': ('C', 3) }), 'value': ('V', { 'module': None, 'name': 'myval' }) }), 1)] })
def test_function(): node = parse('hello()') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': {} }), 1)] }) node = parse('hello( )') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': {} }), 1)] }) node = parse('more.hello()') assert node == ('S', { '_children': [('L', ('F', { 'module': 'more', 'name': 'hello', 'paramaters': {} }), 1)] }) with pytest.raises(ParseError) as e: node = parse('hello( 10 )') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('hello( 10 )') == 'Incomplete Parsing on line: 1 column: 1' node = parse('hello( asdf = 10 )') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': { 'asdf': ('C', 10) } }), 1)] }) node = parse('hello( asdf = "" )') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': { 'asdf': ('C', '') } }), 1)] }) node = parse('hello(asdf=10)') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': { 'asdf': ('C', 10) } }), 1)] }) node = parse('hello( asdf = 10, zxcv="hi", qwerty=123 )') assert node == ('S', { '_children': [('L', ('F', { 'module': None, 'name': 'hello', 'paramaters': { 'asdf': ('C', 10), 'zxcv': ('C', 'hi'), 'qwerty': ('C', 123) } }), 1)] })
def test_begin(): node = parse('') assert node == ('S', {'_children': []}) node = parse(' ') assert node == ('S', {'_children': []}) node = parse('\n') assert node == ('S', {'_children': []}) node = parse(' \n') assert node == ('S', {'_children': []}) node = parse('\n\n') assert node == ('S', {'_children': []}) node = parse('10') assert node == ('S', {'_children': [('L', ('C', 10), 1)]}) node = parse('10\n42') assert node == ('S', { '_children': [('L', ('C', 10), 1), ('L', ('C', 42), 2)] }) node = parse('10\n 42 \n21') assert node == ('S', { '_children': [('L', ('C', 10), 1), ('L', ('C', 42), 2), ('L', ('C', 21), 3)] }) node = parse('begin()end') assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]}) node = parse('begin()\nend') assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]}) node = parse('begin( )\nend') assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]}) with pytest.raises(ParseError) as e: node = parse('begin()') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('begin()') == 'Incomplete Parsing on line: 1 column: 1' assert lint('begin()end') is None assert lint('') is None node = parse('begin()\n10\nend') assert node == ('S', { '_children': [('L', ('S', { '_children': [('L', ('C', 10), 2)] }), 1)] }) node = parse('begin()\n10\nbegin()end\nend') assert node == ('S', { '_children': [('L', ('S', { '_children': [('L', ('C', 10), 2), ('L', ('S', { '_children': [] }), 3)] }), 1)] }) node = parse('begin()\n10\nbegin()\n42\nend\nend') assert node == ('S', { '_children': [('L', ('S', { '_children': [('L', ('C', 10), 2), ('L', ('S', { '_children': [('L', ('C', 42), 4)] }), 3)] }), 1)] })
def test_ifelse(): with pytest.raises(ParseError) as e: node = parse('if True 10') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('if True 10') == 'Incomplete Parsing on line: 1 column: 1' node = parse('if True then 10') assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }]), 1)] }) node = parse('if True then 10 else 42') assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) node = parse('if True then\n10\nelse\n42') assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) with pytest.raises(ParseError) as e: node = parse('if True then 10 elif False 200 else 42') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('if True then 10 elif False 200 else 42' ) == 'Incomplete Parsing on line: 1 column: 1' node = parse('if True then 10 elif False then 200 else 42') assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': ('C', False), 'expression': ('C', 200) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) node = parse('if True then\n10\nelif False then\n200\nelse\n42') assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': ('C', False), 'expression': ('C', 200) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) node = parse( 'if True then 10 elif ( asdf == "a" ) then 100 elif False then 200 else 42' ) assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': ('X', { 'left': ('V', { 'module': None, 'name': 'asdf' }), 'operator': '==', 'right': ('C', 'a') }), 'expression': ('C', 100) }, { 'condition': ('C', False), 'expression': ('C', 200) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) node = parse( 'if True then\n10\nelif ( asdf == "a" ) then\n100\nelif False then\n200\nelse\n42' ) assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('C', True), 'expression': ('C', 10) }, { 'condition': ('X', { 'left': ('V', { 'module': None, 'name': 'asdf' }), 'operator': '==', 'right': ('C', 'a') }), 'expression': ('C', 100) }, { 'condition': ('C', False), 'expression': ('C', 200) }, { 'condition': None, 'expression': ('C', 42) }]), 1)] }) node = parse( 'if ( myobj.value >= "my string" ) then\nbegin( testing="this" )\n42\nend' ) assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('X', { 'left': ('V', { 'module': 'myobj', 'name': 'value' }), 'operator': '>=', 'right': ('C', "my string") }), 'expression': ('S', { 'testing': 'this', '_children': [('L', ('C', 42), 3)] }), }]), 1)] }) node = parse( 'if ( myobj.value >= "my string" ) then\nbegin( testing="this" )\n42\nend\nelse\nbegin( more="fun" )\n56\nend' ) assert node == ('S', { '_children': [('L', ('I', [{ 'condition': ('X', { 'left': ('V', { 'module': 'myobj', 'name': 'value' }), 'operator': '>=', 'right': ('C', "my string") }), 'expression': ('S', { 'testing': 'this', '_children': [('L', ('C', 42), 3)] }), }, { 'condition': None, 'expression': ('S', { 'more': 'fun', '_children': [('L', ('C', 56), 7)] }), }]), 1)] })
def test_while(): with pytest.raises(ParseError) as e: node = parse('while True 10') assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"' assert lint('while True 10') == 'Incomplete Parsing on line: 1 column: 1' node = parse('while true do 10') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('C', True), 'expression': ('C', 10) }), 1)] }) node = parse('while true do \n 10') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('C', True), 'expression': ('C', 10) }), 1)] }) node = parse('while true do \n\n 10') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('C', True), 'expression': ('C', 10) }), 1)] }) node = parse('while myval do 10') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('V', { 'module': None, 'name': 'myval' }), 'expression': ('C', 10) }), 1)] }) node = parse('while ( True == myval ) do 10') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('X', { 'left': ('C', True), 'operator': '==', 'right': ('V', { 'module': None, 'name': 'myval' }) }), 'expression': ('C', 10) }), 1)] }) node = parse('while myval do\nbegin()end') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('V', { 'module': None, 'name': 'myval' }), 'expression': ('S', { '_children': [] }) }), 1)] }) node = parse('while myval do\nbegin()\n10\nend') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('V', { 'module': None, 'name': 'myval' }), 'expression': ('S', { '_children': [('L', ('C', 10), 3)] }) }), 1)] }) node = parse('while myval do\nbegin(thep=5)\n10\nend') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('V', { 'module': None, 'name': 'myval' }), 'expression': ('S', { 'thep': 5, '_children': [('L', ('C', 10), 3)] }) }), 1)] }) node = parse('continue') assert node == ('S', {'_children': [('L', ('O', 'continue'), 1)]}) node = parse('break') assert node == ('S', {'_children': [('L', ('O', 'break'), 1)]}) node = parse('while myval do\nbegin()\n10\ncontinue\nend') assert node == ('S', { '_children': [('L', ('W', { 'condition': ('V', { 'module': None, 'name': 'myval' }), 'expression': ('S', { '_children': [('L', ('C', 10), 3), ('L', ('O', 'continue'), 4)] }) }), 1)] })