Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A simple test infrastructure for language tests.

Unlike the stuff under spec, this is written in Python and invokes magpie
for each test. This way, we can start writing and validating tests well
before the magpie implementation is complete.
  • Loading branch information...
commit 9742ba954284737e3eeb3c047c6932cf8d6b6a33 1 parent ca8a45b
Bob Nystrom authored
84 script/test.py
View
@@ -0,0 +1,84 @@
+#!/usr/bin/python
+
+# Runs the language tests.
+from os import listdir
+from os.path import abspath, dirname, isdir, join, realpath, relpath
+import re
+from subprocess import Popen, PIPE
+
+MAGPIE_DIR = dirname(dirname(realpath(__file__)))
+TEST_DIR = join(MAGPIE_DIR, 'test')
+# TODO(bob): Support other platforms and configurations.
+MAGPIE_APP = join(MAGPIE_DIR, 'build', 'Debug', 'magpie')
+
+EXPECT_PATTERN = re.compile(r'// expect: (.*)')
+
+class color:
+ GREEN = '\033[32m'
+ RED = '\033[31m'
+ DEFAULT = '\033[0m'
+
+def walk(dir, callback):
+ """ Walks [dir], and executes [callback] on each file. """
+ dir = abspath(dir)
+ for file in [file for file in listdir(dir) if not file in [".",".."]]:
+ nfile = join(dir, file)
+ callback(nfile)
+ if isdir(nfile):
+ walk(nfile, callback)
+
+
+def run_test(path):
+ # Make a nice short path relative to the working directory.
+ path = relpath(path)
+
+ # Read the test and parse out the expectations.
+ expect_output = []
+ with open(path, 'r') as file:
+ for line in file:
+ match = EXPECT_PATTERN.search(line)
+ if match:
+ expect_output.append(match.group(1))
+
+ # Invoke magpie and run the test.
+ proc = Popen([MAGPIE_APP, path], stdout=PIPE, stderr=PIPE)
+ (out, err) = proc.communicate()
+
+ fails = []
+
+ # Validate the output.
+ expect_index = 0
+ for line in out.split():
+ if expect_index >= len(expect_output):
+ fails.append('Got output "{0}" when none was expected.'.
+ format(line))
+ elif expect_output[expect_index] != line:
+ fails.append('Expected output "{0}" and got "{1}".'.
+ format(expect_output[expect_index], line))
+ expect_index += 1
+
+ while expect_index < len(expect_output):
+ fails.append('Missing expected output "{0}".'.
+ format(expect_output[expect_index]))
+ expect_index += 1
+
+ # Validate the error output.
+ # TODO(bob): Implement. Allow tests to specify which lines compile errors
+ # should occur on a parse those out of the stderr.
+
+ # Validate the return code.
+ # TODO(bob): Allow tests to specify expected return code.
+ expect_return = 0
+ if proc.returncode != expect_return:
+ fails.append('Expected return code {0} and got {1}.'.
+ format(expect_return, proc.returncode))
+
+ # Display the results.
+ if len(fails) == 0:
+ print color.GREEN + 'PASS' + color.DEFAULT + ': ' + path
+ else:
+ print color.RED + 'FAIL' + color.DEFAULT + ': ' + path
+ for fail in fails:
+ print ' ', fail
+
+walk(TEST_DIR, run_test)
2  src/Syntax/ErrorReporter.cpp
View
@@ -15,7 +15,7 @@ namespace magpie
vsprintf(message, format, args);
va_end(args);
- std::cout << "[" << pos.file() << " line "
+ std::cerr << "[" << pos.file() << " line "
<< pos.startLine() << " col "
<< pos.startCol() << "] Error: " << message << std::endl;
16 test/variable_scope.mag
View
@@ -0,0 +1,16 @@
+def main()
+ // Shadowing and unshadowing.
+ val a = "outer"
+ print(a) // expect: outer
+ do
+ val a = "inner"
+ print(a) // expect: inner
+
+ do
+ val a = "innerinner"
+ print(a) // expect: innerinner
+ end
+ print(a) // expect: inner
+ end
+ print(a) // expect: outer
+end
Please sign in to comment.
Something went wrong with that request. Please try again.