def test_iteration_loop_parsing(): loop = parse_local('for number n in numbers') assert isinstance(loop, IterationLoop) assert loop.target == Identifier('n') assert loop.target_type == Identifier('number') assert loop.collection == Identifier('numbers')
def test_chained_call(): call = parse_local('counter.increment().add(10)') validate_types(call.arguments, [NumericValue]) assert call.target == NamedAccess([ MethodCall(NamedAccess([ Identifier('counter'), Identifier('increment') ])), Identifier('add') ])
def test_generic_parsing(): node = parse_local('list<number> l = [1, 2, 3]') assert isinstance(node, AssignmentOperation) assert isinstance(node.name, Identifier) assert isinstance(node.name.type, GenericIdentifier) assert node.name == Identifier('l') assert node.name.type.value == Identifier('list') assert node.name.type.generics == (Identifier('number'), )
def test_source_reference_parser_output(): ast = parse_local( 'number a = distribution.normal(number.random(10), number.random(25))') assignment_name, outer_function_target, inner_function_argument = \ ast.name.source_ref, ast.value.target.source_ref, ast.value.arguments[1].arguments[0].source_ref assert assignment_name.column_start == 7 and assignment_name.column_end == 8 assert outer_function_target.column_start == 11 and outer_function_target.column_end == 30 assert inner_function_argument.column_start == 64 and inner_function_argument.column_end == 66
def test_inline_list_assignment(): validate_list( parse_local('number b = [[1, 2], a.b(), "hi"]').value, [[NumericValue] * 2, MethodCall, InlineString])
def test_conditional_else(): validate_assignment(parse_local('else if "dog" eq "dog"'), [InlineString, InlineString], ConditionalElse)
def test_simple_equality(): validate_assignment(parse_local('if "dog" eq "dog"'), [InlineString, InlineString])
def test_valid_syntax_examples(source): parse_local(source)
def test_thing_definition_inheritance_and_generic(): node = parse_local('thing SmartList extends BaseList with type T, type V') validate_thing_definition(node, 'SmartList', 'BaseList', ['T', 'V'])
def test_method_definition_argument_invalid_syntax(source): with pytest.raises(VectorReductionError): parse_local(source)
def test_combined_method_definition(): method = parse_local( "does say_hello with text message, number count returns text") validate_method_definition(method, 'say_hello', [('message', 'text'), ('count', 'number')], Identifier('text'))
def test_multiple_argument_method_definition(): method = parse_local("does say_hello with text message, number count") validate_method_definition(method, 'say_hello', [('message', 'text'), ('count', 'number')])
def test_single_argument_method_definition(): method = parse_local("does say_hello with text message") validate_method_definition(method, 'say_hello', [('message', 'text')])
def test_method_definition_return_type(): method = parse_local("does compute returns number") validate_method_definition(method, 'compute', (), Identifier('number'))
def test_constructor_arguments(): method = parse_local("setup with text name, number age") parse_local("thing container").attach(method) validate_method_definition(method, Identifier.constructor(), [('name', 'text'), ('age', 'number')], Identifier('container'))
def test_thing_definition_inheritance(): method = parse_local('thing Child extends Person') validate_thing_definition(method, 'Child', 'Person')
def test_thing_definition_generic(): node = parse_local('thing SmartList with type T') validate_thing_definition(node, 'SmartList', generics=['T']) node = parse_local('thing SmartList with type T, type V') validate_thing_definition(node, 'SmartList', generics=['T', 'V'])
def test_immediate_declarations(): validate_assignment(parse_local('number a = 5'), 'number', 'a', NumericValue(5)) validate_assignment(parse_local('text a = "hello world!"'), 'text', 'a', InlineString('hello world!'))
def test_invalid_syntax_examples(source): with pytest.raises(VectorReductionError): parse_local(source)
def test_immediate_assignments(): validate_assignment(parse_local('a = 5'), None, 'a', NumericValue(5)) validate_assignment(parse_local('a = "hello world!"'), None, 'a', InlineString('hello world!'))
def test_boolean_primitive_conditional(): validate_assignment(parse_local('if true'), LexicalBooleanTrue)
def test_simple_method_call_value_type(): assignment = parse_local('number a = number.random()') validate_assignment(assignment, 'number', 'a', MethodCall) validate_method_call(assignment.value, ['number', 'random'], [])
def test_equality_method_call(): validate_assignment(parse_local('if "dog" eq DogFactory.build_dog("german_shepherd")'), [InlineString, [InlineString]])
def test_complex_method_call_value_type(): assignment = parse_local( 'number a = distribution.normal(number.random(10), number.random(25))') validate_assignment(assignment, 'number', 'a', MethodCall) validate_method_call(assignment.value, ['distribution', 'normal'], [[NumericValue], [NumericValue]])
def test_unconditional_else(): validate_assignment(parse_local('else'), [], UnconditionalElse)
def test_arithmetic_operation_value_type(): assignment = parse_local('number a = 2 * (4 + 2) * (3 + 2)') validate_assignment(assignment, 'number', 'a', BinaryOperation) assert assignment.value.evaluate() == 60
def test_simple_inline_list(): validate_list(parse_local('[1, 2, 3]'), [NumericValue] * 3) validate_list(parse_local('[1, "text", 3]'), [NumericValue, InlineString, NumericValue])
def test_in_place_modifier(): reassignment = parse_local('a += 2 * 8') validate_assignment(reassignment, None, 'a', BinaryOperation) assert reassignment.value.lhs == Identifier('a') assert isinstance(reassignment.value.rhs, BinaryOperation) assert reassignment.value.rhs.evaluate() == 16
def test_empty_inline_list(): validate_list(parse_local('[]'), [])
def test_simple_thing_definition(): method = parse_local('thing Person') validate_thing_definition(method, 'Person')