def test_argument_type_mismatch():
    with pytest.raises(NoMatchingOverload) as e:
        pipeline.compile(
            SourceContext.wrap(BASE.format(code='self.two_args("hello", 3)')))

    assert e.value.methods[0].name == Identifier(
        'two_args') and e.value.arguments == [
            InlineString("hello"), NumericValue(3)
        ]

    pipeline.compile(
        SourceContext.wrap(BASE.format(
            code='self.two_args(3, 3)')))  # Can be implicitly casted
def test_argument_count_mismatch():
    with pytest.raises(NoMatchingOverload) as e:
        pipeline.compile(
            SourceContext.wrap(BASE.format(code="self.no_args(1)")))

    assert e.value.methods[0].name == Identifier(
        'no_args') and e.value.arguments == [NumericValue(1)]

    with pytest.raises(NoMatchingOverload) as e:
        pipeline.compile(
            SourceContext.wrap(BASE.format(code="self.two_args(1)")))

    assert e.value.methods[0].name == Identifier(
        'two_args') and e.value.arguments == [NumericValue(1)]
Esempio n. 3
0
def test_thing_program(test_file: ProgramTestCase):
    expected_output = test_file.metadata['expected_output']
    test_input = bytes(
        '\n'.join(test_file.metadata['input'])
        if 'input' in test_file.metadata else '', 'utf-8')
    bytecode = pipeline.compile(SourceContext.wrap(test_file.code))

    logging_utils.print_header('VM execution')

    with open(test_file.target_path, 'wb') as f:
        f.write(bytecode.bytes())

    vm = subprocess.Popen(["thinglang", test_file.target_path],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)

    stdout, stderr = (stream.decode('utf-8').strip()
                      for stream in vm.communicate(test_input))
    print(stderr)

    logging_utils.print_header('VM output')
    print(stdout)

    if not isinstance(expected_output, str):
        stdout = split_lines(stdout)

    assert vm.returncode == 0, 'VM process crashed'
    assert stdout == expected_output, 'VM output did not match expected output'
Esempio n. 4
0
def test_invalid_specificity_exception_handlers():
    with pytest.raises(ExceptionSpecificityError):
        pipeline.compile(SourceContext.wrap(SPECIFICITY_EXCEPTION))
Esempio n. 5
0
def test_duplicate_exception_handlers():
    with pytest.raises(DuplicateHandlerError):
        pipeline.compile(SourceContext.wrap(DUPLICATE_EXCEPTION))
Esempio n. 6
0
def compile(target_source_file_path):
    source_context = SourceContext.wrap(
        str(open(target_source_file_path, "rb").read(), encoding="utf-8"))
    compilation_result = pipeline.compile(source_context)
    return compilation_result.bytes()
Esempio n. 7
0
def compile_base(code='', thing_id=0, method_id=0, trim=False):
    context = pipeline.compile(SourceContext.wrap(code), mapper=SYMBOL_MAPPER)
    entry = context.classes[thing_id][0][method_id]
    instructions = entry[1].instructions
    return instructions[trim + 1:-1] if trim else instructions
def test_method_call_on_non_method_target():
    with pytest.raises(TargetNotCallable):
        pipeline.compile(SourceContext.wrap(
            BASE.format(code="self.member1()")))