class TestExample(TestCase): def setUp(self): self.stdout, self.stderr = StringIO(), StringIO() self.cycy = CyCy(stdout=self.stdout, stderr=self.stderr) @skip("Integration test, will work eventually") def test_it_works(self): self.cycy.interpret( [FilePath(__file__).sibling("example.c").getContent()], ) self.assertEqual(self.stdout.getvalue(), "Hello, world!\n") def test_it_does_fibonacci(self): source = dedent("""\ int fib(int x) { while (x <= 2) { return 1; } return fib(x - 1) + fib(x - 2); } int main(void) { return fib(5); } """) w_returned = self.cycy.interpret([source]) self.assertEqual(w_returned, W_Int32(5))
def setUp(self): self.repl = REPL(interpreter=CyCy( parser=IncrementalParser(), stdin=StringIO(), stdout=StringIO(), stderr=StringIO(), ))
def test_run_repl(self): self.assertEqual( cli.parse_args([]), cli.CommandLine( action=cli.run_repl, cycy=CyCy(parser=IncrementalParser()), ), )
def __init__(self, interpreter=None): if interpreter is None: interpreter = CyCy(parser=IncrementalParser()) self.interpreter = interpreter self.stdin = interpreter.stdin self.stdout = interpreter.stdout self.stderr = interpreter.stderr self.compiler = interpreter.compiler self.parser = interpreter.parser
def test_run_source_files(self): self.assertEqual( cli.parse_args(["-I", "a/include", "-I", "b/include", "file.c"]), cli.CommandLine( action=cli.run_source, source_files=["file.c"], cycy=CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories( ["a/include", "b/include"], ), ), ), ), ), )
def test_run_source_string(self): self.assertEqual( cli.parse_args(["-I", "a/include", "-c", "int main (void) {}"]), cli.CommandLine( action=cli.run_source, source_string="int main (void) {}", cycy=CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories( ["a/include"], ), ), ), ), ), )
def parse_args(args): source_string = "" source_files = [] include_paths = [] arguments = iter(args) for argument in arguments: if argument in ("-h", "--help"): return CommandLine(action=print_help) if argument in ("-version", "--version"): return CommandLine(action=print_version) if argument in ("-I", "--include"): try: include_paths.append(next(arguments)) except StopIteration: return CommandLine( action=print_help, failure="-I expects an argument", ) elif argument == "-c": source = [] # this is the simplest valid RPython currently for argument in arguments: source.append(argument) source_string = " ".join(source) if not source_string: return CommandLine( action=print_help, failure="-c expects an argument", ) elif not argument.startswith("-"): source_files.append(argument) else: return CommandLine( action=print_help, failure="Unknown argument %s" % (argument, ), ) cycy = CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories(directories=include_paths, ), ), )) if source_files or source_string: return CommandLine( action=run_source, cycy=cycy, source_files=source_files, source_string=source_string, ) else: return CommandLine(action=run_repl, cycy=cycy)
class TestParser(TestCase): def setUp(self): self.preprocessor = Preprocessor(includers=[FakeIncluder()]) self.parser = Parser(preprocessor=self.preprocessor) self.cycy = CyCy(parser=self.parser) def test_include_statement(self): w_return = self.cycy.interpret([ """ #include "foo.h" int main(void) { return foo(); } """, ], ) self.assertEqual(w_return, W_Int32(12))
def setUp(self): self.preprocessor = Preprocessor(includers=[FakeIncluder()]) self.parser = Parser(preprocessor=self.preprocessor) self.cycy = CyCy(parser=self.parser)
def setUp(self): self.stdout, self.stderr = StringIO(), StringIO() self.cycy = CyCy(stdout=self.stdout, stderr=self.stderr)