Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "Test runner for ruby code"

This reverts commit cb1df63.

Conflicts:

	lib/homework/json_formatter.rb
	lib/test_runner.rb
	spec/lib/test_runner_spec.rb
  • Loading branch information...
commit 0cb57ce4fd3d02e473831189205c11fad0135575 1 parent 08725f5
@AndrewRadev AndrewRadev authored
View
33 lib/homework/json_formatter.rb
@@ -1,33 +0,0 @@
-require 'rspec/core/formatters/base_text_formatter'
-require 'json'
-
-class JsonFormatter < RSpec::Core::Formatters::BaseTextFormatter
- def initialize(output)
- super
-
- @store = {}
- end
-
- def start(count)
- @store['count'] = count
- @store['passed'] = []
- @store['failed'] = []
- end
-
- def example_passed(example)
- @store['passed'] << example.full_description
- end
-
- def example_failed(example)
- @store['failed'] << example.full_description
- end
-
- def start_dump
- @output.puts @store.to_json
- end
-
- def dump_failures(*args); end
- def dump_failure(*args); end
- def dump_pending(*args); end
- def dump_summary(*args); end
-end
View
40 lib/test_runner.rb
@@ -1,8 +1,10 @@
+require 'open3'
+
class TestRunner
- attr_reader :passed_count, :failures_count, :results, :log
+ attr_reader :passed_count, :failures_count, :log
def initialize(test, solution)
- @test = test
+ @test = test
@solution = solution
end
@@ -15,20 +17,32 @@ def run(test, solution)
end
def run
- Dir.mktmpdir do |dir|
- spec_path = Pathname(dir).join('spec.rb')
- solution_path = Pathname(dir).join('solution.rb')
+ ENV['PYTHONPATH'] = Rails.root.join("python").to_s
+
+ solution_path = make_temp_file(@solution, 'solution')
+ test_path = make_temp_file(@test, 'test')
+
+ Open3.popen3("python3.2", test_path, solution_path) do |stdin, stdout, stderr|
+ load_log stdout.read + stderr.read
+ end
+ end
- open(spec_path, 'w') { |file| file.write @test.encode('utf-8') }
- open(solution_path, 'w') { |file| file.write @solution }
+ private
- output = `ruby lib/homework/runner.rb #{spec_path} #{solution_path}`
- json, log = output.split("\nLOG:\n", 2)
+ def make_temp_file(code, name)
+ file = Tempfile.new(name)
+ file.write code
+ file.close
+ file.path
+ end
- @log = log
- @results = JSON.parse json
- @passed_count = results['passed'].try(:count) || 0
- @failures_count = results['failed'].try(:count) || 0
+ def load_log(output)
+ if /\A(\d+) (\d+)\n/ =~ output
+ @passed_count, @failures_count = [$1, $2].map(&:to_i)
+ @log = $'
+ else
+ @passed_count, @failures_count = 0, 0, 0
+ @log = output
end
end
end
View
31 spec/lib/test_runner_spec.rb
@@ -3,19 +3,23 @@
describe TestRunner do
let(:test_case_code) do
<<END.strip
-describe "Homework" do
- it("succeeds once") { true.should be_true }
- it("succeeds thrice") { puts "Just to mess with you" }
- it("fails once") { true.should be_false }
- it("succeeds twice") { Homework.answer.should eq 42 }
- it("fails twice") { 1.should eq 2 }
- it("errors once") { raise RuntimeError }
-end
+import homework
+
+class HomeworkTest(homework.Test):
+ def test_success_1(self): self.assert_(True)
+ def test_success_3(self): print("Just to mess up with you")
+ def test_failure_1(self): self.assert_(False)
+ def test_success_2(self): self.assertEquals(self.solution.answer, 42)
+ def test_failure_2(self): self.assertEquals(1, 2)
+ def test_errored_1(self): raise RuntimeError
+
+if __name__ == '__main__':
+ HomeworkTest.main()
END
end
it "handles solutions that raise a runtime error" do
- solution = 'require "intertools"'
+ solution = 'import intertools'
runner = TestRunner.new(test_case_code, solution)
runner.run
@@ -26,12 +30,7 @@
describe "on successful run" do
before(:all) do
- solution = <<-EOF
- module Homework
- def self.answer; 42; end
- puts "Another puts"
- end
- EOF
+ solution = 'answer = 42; print("Another print")'
@runner = TestRunner.new(test_case_code, solution)
@runner.run
@@ -46,7 +45,7 @@ def self.answer; 42; end
end
it "collects the execution log" do
- @runner.log.should include("6 examples, 3 failures")
+ @runner.log.should include("FAILED (failures=2, errors=1)")
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.