diff --git a/frameworks/elixir/cw_formatter.ex b/frameworks/elixir/cw_formatter.ex index 82276c69..a055dee8 100644 --- a/frameworks/elixir/cw_formatter.ex +++ b/frameworks/elixir/cw_formatter.ex @@ -28,18 +28,18 @@ defmodule CWFormatter do end def handle_event({:suite_finished, run_us, load_us}, _config) do - IO.puts "" <> format_time(run_us, load_us) + IO.puts "\n" <> format_time(run_us, load_us) :remove_handler end def handle_event({:test_started, %ExUnit.Test{} = test}, config) do test_name = trace_test_name(test) |> nl_to_lf() - IO.puts "#{test_name}" + IO.puts "\n#{test_name}" {:ok, config} end def handle_event({:test_finished, %ExUnit.Test{state: nil}}, config) do - IO.puts "Test Passed" + IO.puts "\nTest Passed" {:ok, %{config | tests_counter: config.tests_counter + 1}} end @@ -49,14 +49,14 @@ defmodule CWFormatter do end def handle_event({:test_finished, %ExUnit.Test{state: {:invalid, _}} = test}, config) do - IO.puts "" <> trace_test_result(test) + IO.puts "\n" <> trace_test_result(test) {:ok, %{config | tests_counter: config.tests_counter + 1, invalids_counter: config.invalids_counter + 1}} end def handle_event({:test_finished, %ExUnit.Test{state: {:failed, failed}} = test}, config) do - IO.write "" <> trace_test_result(test) <> @lf + IO.write "\n" <> trace_test_result(test) <> @lf formatted = format_test_failure(test, failed, config.failures_counter + 1, config.width, fn _type, msg -> msg end) @@ -67,7 +67,7 @@ defmodule CWFormatter do end def handle_event({:case_started, %ExUnit.TestCase{name: name}}, config) do - IO.puts "" <> inspect(name) + IO.puts "\n" <> inspect(name) {:ok, config} end diff --git a/frameworks/elixir/cw_runner.ex b/frameworks/elixir/cw_runner.ex index 7d1d4406..5c6520e2 100644 --- a/frameworks/elixir/cw_runner.ex +++ b/frameworks/elixir/cw_runner.ex @@ -12,7 +12,7 @@ defmodule CWRunner do |> case do :ok -> :ok {:error, message} -> - IO.puts "" <> message + IO.puts "\n" <> message end end diff --git a/test/runners/elixir_spec.js b/test/runners/elixir_spec.js index fd1e39ee..467257c0 100644 --- a/test/runners/elixir_spec.js +++ b/test/runners/elixir_spec.js @@ -6,7 +6,10 @@ describe('elixir runner', function() { runner.assertCodeExamples('elixir'); it('handles basic code evaluation', function(done) { - runner.run({language: 'elixir', code: 'IO.puts "This was real. And it was me. I had lived that life, and I had died that death. I was staring at the very end of me. ― Hilary Duff, Elixir (2010)"'}, function(buffer) { + runner.run({ + language: 'elixir', + code: 'IO.puts "This was real. And it was me. I had lived that life, and I had died that death. I was staring at the very end of me. ― Hilary Duff, Elixir (2010)"' + }, function(buffer) { expect(buffer.stdout).to.contain('This was real'); done(); }); @@ -14,35 +17,105 @@ describe('elixir runner', function() { describe('ex_unit', function() { it('handles a simple test unit defininition', function(done) { - runner.run({language: 'elixir', code: 'defmodule AEqual1 do\n def a, do: 1\nend', fixture: 'defmodule TestAEqual1 do\nuse ExUnit.Case\nimport AEqual1, only: [a: 0]\ntest "a is equal 1" do\nassert a() == 1\nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestAEqual1'); - expect(buffer.stdout).to.contain('a is equal 1\nTest Passed\n'); + runner.run({ + language: 'elixir', + code: 'defmodule AEqual1 do\n def a, do: 1\nend', + fixture: [ + 'defmodule TestAEqual1 do', + ' use ExUnit.Case', + ' import AEqual1, only: [a: 0]', + ' test "a is equal 1" do', + ' assert a() == 1', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestAEqual1'); + expect(buffer.stdout).to.contain('\na is equal 1\n\nTest Passed\n'); expect(buffer.stdout).to.match(/Finished in [0-9.]+ seconds \([0-9.]+s on load, [0-9.]+s on tests\)/); done(); }); }); + it('handles test unit with a couple of defininitions', function(done) { - runner.run({language: 'elixir', code: 'defmodule YesNo do\n def yes, do: "yes"\n def no, do: "no"\nend', fixture: 'defmodule TestYesNo do\nuse ExUnit.Case\ntest ".yes is yes" do\nassert YesNo.yes == "yes"\nend\ntest ".no is no" do\nassert YesNo.no == "no"\nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestYesNo'); - expect(buffer.stdout).to.contain('.yes is yes\nTest Passed\n'); - expect(buffer.stdout).to.contain('.no is no\nTest Passed\n'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule YesNo do', + ' def yes, do: "yes"', + ' def no, do: "no"', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestYesNo do', + ' use ExUnit.Case', + ' test ".yes is yes" do', + ' assert YesNo.yes == "yes"', + ' end', + ' test ".no is no" do', + ' assert YesNo.no == "no"', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestYesNo'); + expect(buffer.stdout).to.contain('\n.yes is yes\n\nTest Passed\n'); + expect(buffer.stdout).to.contain('\n.no is no\n\nTest Passed\n'); expect(buffer.stdout).to.match(/Finished in [0-9.]+ seconds \([0-9.]+s on load, [0-9.]+s on tests\)/); done(); }); }); it('handles test which does not pass', function(done) { - runner.run({language: 'elixir', code: 'defmodule NotPass do\n def can_i_enter?, do: :no\nend', fixture: 'defmodule TestNotPass do\nuse ExUnit.Case\ntest "passes if works" do\nassert NotPass.can_i_enter? == :yes\nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestNotPass'); - expect(buffer.stdout).to.contain('passes if works\npasses if works'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule NotPass do', + ' def can_i_enter?, do: :no', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestNotPass do', + ' use ExUnit.Case', + ' test "passes if works" do', + ' assert NotPass.can_i_enter? == :yes', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestNotPass'); + expect(buffer.stdout).to.contain('\npasses if works\n\npasses if works'); expect(buffer.stdout).to.match(/Finished in [0-9.]+ seconds \([0-9.]+s on load, [0-9.]+s on tests\)/); done(); }); }); it('handles test unit with pass and not-pass tests', function(done) { - runner.run({language: 'elixir', code: 'defmodule YesBrokenNo do\n def yes, do: "yes"\n def no, do: "yes"\nend', fixture: 'defmodule TestYesBrokenNo do\nuse ExUnit.Case\ntest ".yes is yes" do\nassert YesBrokenNo.yes == "yes"\nend\ntest ".no is no" do\nassert YesBrokenNo.no == "no"\nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestYesBrokenNo'); - expect(buffer.stdout).to.contain('.yes is yes\nTest Passed\n'); - expect(buffer.stdout).to.contain('.no is no\n.no is no'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule YesBrokenNo do', + ' def yes, do: "yes"', + ' def no, do: "yes"', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestYesBrokenNo do', + ' use ExUnit.Case', + ' test ".yes is yes" do', + ' assert YesBrokenNo.yes == "yes"', + ' end', + ' test ".no is no" do', + ' assert YesBrokenNo.no == "no"', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestYesBrokenNo'); + expect(buffer.stdout).to.contain('\n.yes is yes\n\nTest Passed\n'); + expect(buffer.stdout).to.contain('\n.no is no\n\n.no is no'); expect(buffer.stdout).to.match(/Finished in [0-9.]+ seconds \([0-9.]+s on load, [0-9.]+s on tests\)/); done(); }); @@ -50,15 +123,40 @@ describe('elixir runner', function() { describe('compilation errors', function() { it('catches compilation error of the code module', function(done) { - runner.run({language: 'elixir', code: 'defodule BrokenModule do\n def broken, do: true\nend', fixture: 'defmodule TestBrokenModule do\nend', testFramework: 'ex_unit'}, function(buffer) { + runner.run({ + language: 'elixir', + code: [ + 'defodule BrokenModule do', + ' def broken, do: true', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestBrokenModule do', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { console.log(buffer); - expect(buffer.stdout).to.contain('solution:1: undefined function defodule/2\n'); + expect(buffer.stdout).to.contain('\nsolution:1: undefined function defodule/2\n'); done(); }); }); it('catches compilation error of the fixture module', function(done) { - runner.run({language: 'elixir', code: 'defmodule BrokenFixture do\n def broken, do: true\nend', fixture: 'defmodule TestBrokenFixture do\n -> hanging_arrow \n end', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('fixture:2: unhandled operator ->'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule BrokenFixture do', + ' def broken, do: true', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestBrokenFixture do', + ' -> hanging_arrow ', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nfixture:2: unhandled operator ->'); done(); }); }); @@ -66,16 +164,53 @@ describe('elixir runner', function() { describe('runtime errors', function() { it('catches runtime error of the code module', function(done) { - runner.run({language: 'elixir', code: 'defmodule RuntimeIssue do\n def make_issue do\n 5 = 8\nend\nend', fixture: 'defmodule TestRuntimeIssue do\nuse ExUnit.Case\ntest "have runtime issue" do\n RuntimeIssue.make_issue \nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestRuntimeIssue'); - expect(buffer.stdout).to.contain('have runtime issue\nhave runtime issue'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule RuntimeIssue do', + ' def make_issue do', + ' 5 = 8', + ' end', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestRuntimeIssue do', + ' use ExUnit.Case', + ' test "have runtime issue" do', + ' RuntimeIssue.make_issue', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestRuntimeIssue'); + expect(buffer.stdout).to.contain('\nhave runtime issue\n\nhave runtime issue'); done(); }); }); + it('catches runtime error of the fixture module', function(done) { - runner.run({language: 'elixir', code: 'defmodule FixtureIssue do\n def no_issue do\n :no_issue\nend\nend', fixture: 'defmodule TestFixtureIssue do\nuse ExUnit.Case\ntest "have fixture issue" do\n ModuleNotExist.call_it() \nend\nend', testFramework: 'ex_unit'}, function(buffer) { - expect(buffer.stdout).to.contain('TestFixtureIssue'); - expect(buffer.stdout).to.contain('have fixture issue\nhave fixture issue'); + runner.run({ + language: 'elixir', + code: [ + 'defmodule FixtureIssue do', + ' def no_issue do', + ' :no_issue', + ' end', + 'end', + ].join('\n'), + fixture: [ + 'defmodule TestFixtureIssue do', + ' use ExUnit.Case', + ' test "have fixture issue" do', + ' ModuleNotExist.call_it()', + ' end', + 'end', + ].join('\n'), + testFramework: 'ex_unit' + }, function(buffer) { + expect(buffer.stdout).to.contain('\nTestFixtureIssue'); + expect(buffer.stdout).to.contain('\nhave fixture issue\n\nhave fixture issue'); done(); }); }); @@ -85,18 +220,48 @@ describe('elixir runner', function() { // Due to ExUnit.start, it can add COMPLETEDIN:: at the end. // That's why we match with .contain and not .equal it('handles correctly empty fixture', function(done) { - runner.run({language: 'elixir', code: 'IO.puts "Hello"', fixture: '', testFramework: 'ex_unit'}, function(buffer) { + runner.run({ + language: 'elixir', + code: 'IO.puts "Hello"', + fixture: '', + testFramework: 'ex_unit' + }, function(buffer) { expect(buffer.stdout).to.contain('Hello\n'); done(); }); }); it('handles correctly fixture with just comments', function(done) { - runner.run({language: 'elixir', code: 'IO.puts "Hello comment"', fixture: '# this is comment', testFramework: 'ex_unit'}, function(buffer) { + runner.run({ + language: 'elixir', + code: 'IO.puts "Hello comment"', + fixture: '# this is comment', + testFramework: 'ex_unit' + }, function(buffer) { expect(buffer.stdout).to.contain('Hello comment\n'); done(); }); }); }); + + it('should have output format command on independent line', function(done) { + runner.run({ + language: 'elixir', + testFramework: 'ex_unit', + code: '#', + fixture: [ + 'defmodule Tests do', + ' use ExUnit.Case', + ' test "test" do', + ' IO.write "foo"', + ' assert 1 == 2', + ' end', + 'end', + ].join('\n'), + }, function(buffer) { + expect(buffer.stdout).to.contain('\n'); + done(); + }); + }); }); }); });