diff options
-rw-r--r-- | environment.py | 7 | ||||
-rw-r--r-- | interpreter.py | 34 | ||||
-rw-r--r-- | test cases/common/39 tryrun/meson.build | 59 |
3 files changed, 100 insertions, 0 deletions
diff --git a/environment.py b/environment.py index f7f42e1..309a1e3 100644 --- a/environment.py +++ b/environment.py @@ -23,6 +23,13 @@ class EnvironmentException(Exception): def __init(self, *args, **kwargs): Exception.__init__(self, *args, **kwargs) +class RunResult(): + def __init__(self, compiled, returncode=999, stdout='UNDEFINED', stderr='UNDEFINED'): + self.compiled = compiled + self.returncode = returncode + self.stdout = stdout + self.stderr = stderr + class CCompiler(): def __init__(self, exelist): if type(exelist) == type(''): diff --git a/interpreter.py b/interpreter.py index b7d0055..834f7f8 100644 --- a/interpreter.py +++ b/interpreter.py @@ -38,6 +38,28 @@ class InterpreterObject(): return self.methods[method_name](args, kwargs) raise InvalidCode('Unknown method "%s" in object.' % method_name) +class TryRunResultHolder(InterpreterObject): + def __init__(self, res): + super().__init__() + self.res = res + self.methods.update({'returncode' : self.returncode_method, + 'compiled' : self.compiled_method, + 'stdout' : self.stdout_method, + 'stderr' : self.stderr_method, + }) + + def returncode_method(self, args, kwargs): + return self.res.returncode + + def compiled_method(self, args, kwargs): + return self.res.compiled + + def stdout_method(self, args, kwargs): + return self.res.stdout + + def stderr_method(self, args, kwargs): + return self.res.stderr + class RunProcess(InterpreterObject): def __init__(self, command_array, curdir): @@ -627,8 +649,20 @@ class CompilerHolder(InterpreterObject): 'get_id': self.get_id_method, 'sizeof': self.sizeof_method, 'has_header': self.has_header_method, + 'run' : self.run_method, }) + def run_method(self, args, kwargs): + if len(args) != 1: + raise InterpreterException('Run method takes exactly one positional argument.') + code = args[0] + if isinstance(code, nodes.StringStatement): + code = code.get_value() + if not isinstance(code, str): + raise InterpreterException('First argument is not a string.') + result = environment.RunResult(True, 0, 'stdout', 'stderr') + return TryRunResultHolder(result) + def get_id_method(self, args, kwargs): return self.compiler.get_id() diff --git a/test cases/common/39 tryrun/meson.build b/test cases/common/39 tryrun/meson.build new file mode 100644 index 0000000..7f4cdad --- /dev/null +++ b/test cases/common/39 tryrun/meson.build @@ -0,0 +1,59 @@ +project('tryrun', 'c') + +cc = meson.get_compiler('c') + +ok_code = '''#include<stdio.h> +int main(int argc, char **argv) { + printf("%s\n", "stdout"); + fprintf(stderr, "%s\n", "stderr"); + return 0; +} +''' + +error_code = '''int main(int argc, char **argv) { + return 1; +} +''' + +no_compile_code = '''int main(int argc, char **argv) { +''' + +ok = cc.run(ok_code) +err = cc.run(error_code) +noc = cc.run(no_compile_code) + +if noc.compiled() + message('Compilation fail test failed.') +else + error('Fail detected.') +endif + +if ok.compiled() + message('Compilation worked.') +else + error('Compilation did not work.') +endif + +if ok.returncode() == 0 + message('Return code ok.') +else + error('Return code fail') +endif + +if err.returncode() == 1 + message('Bad return code ok.') +else + error('Bad return code fail.') +endif + +if ok.stdout().strip() == 'stdout' + message('Stdout ok.') +else + message('Bad stdout.') +endif + +if ok.stderr().strip() == 'stderr' + message('Stderr ok.') +else + message('Bad stderr.') +endif |