From 1c0ef5dfbed6450b365fd181ae701b1f373de2d0 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 13 Jun 2015 22:16:12 +0900 Subject: [PATCH] add first spec for mal test cases (step2) --- spec/crisp/mal_specs/mal_spec_runner.cr | 40 +++++++++++++++++++++++++ spec/crisp/mal_specs/mal_tests_spec.cr | 22 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 spec/crisp/mal_specs/mal_spec_runner.cr create mode 100644 spec/crisp/mal_specs/mal_tests_spec.cr diff --git a/spec/crisp/mal_specs/mal_spec_runner.cr b/spec/crisp/mal_specs/mal_spec_runner.cr new file mode 100644 index 0000000..d465c8a --- /dev/null +++ b/spec/crisp/mal_specs/mal_spec_runner.cr @@ -0,0 +1,40 @@ +class MalTestRunner + def initialize(@test_file) + end + + private macro check_EOF + return if l.is_a? Iterator::Stop + end + + def each_test + lines = File.open(@test_file).each_line + + until (l = lines.next).is_a? Iterator::Stop + while l =~ /^\s*(;|$)/ + l = lines.next + check_EOF + end + + check_EOF + input = l + + output = [] of String + loop do + l = lines.next + check_EOF + + if l =~ /^; / + output << l[2..-1] + else + break + end + end + + if l =~ /^;=>/ + yield input, output, l[3..-1] + else + yield input, output, nil + end + end + end +end diff --git a/spec/crisp/mal_specs/mal_tests_spec.cr b/spec/crisp/mal_specs/mal_tests_spec.cr new file mode 100644 index 0000000..b9b1881 --- /dev/null +++ b/spec/crisp/mal_specs/mal_tests_spec.cr @@ -0,0 +1,22 @@ +require "../../helper" +require "./mal_spec_runner.cr" + +describe "'Make a Lisp' tests" do + it "tests step2 of mal" do + runner = MalTestRunner.new(__DIR__ + "/tests/step2_eval.mal") + i = Crisp::Interpreter.new + + runner.each_test do |input, output, result| + if result + r = i.print i.eval_string(input) + else + output.empty?.should be_false + begin + i.eval_string(input) + rescue e + e.message.should match(/#{output.last.chomp}/) + end + end + end + end +end