Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第22章 失敗の扱い #27

Merged
merged 3 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
* [第19章 前準備](https://github.com/at-grandpa/study-tdd/pull/24)
* [第20章 後片付け](https://github.com/at-grandpa/study-tdd/pull/25)
* [第21章 数え上げ](https://github.com/at-grandpa/study-tdd/pull/26)
* [第22章 失敗の扱い](https://github.com/at-grandpa/study-tdd/pull/27)
14 changes: 11 additions & 3 deletions part02/src/xunit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ class TestCaseTest < TestCase
result = @test.run
result.summary.should eq "1 run, 1 failed"
end

def test_failed_result_formatting
result = TestResult.new
result.test_started
result.test_failed
result.summary.should eq "1 run, 1 failed"
end
end

TestCaseTest.new("test_template_method").run
TestCaseTest.new("test_result").run
# TestCaseTest.new("test_failed_result").run
puts TestCaseTest.new("test_template_method").run.summary
puts TestCaseTest.new("test_result").run.summary
puts TestCaseTest.new("test_failed_result").run.summary
puts TestCaseTest.new("test_failed_result_formatting").run.summary
22 changes: 13 additions & 9 deletions part02/src/xunit/test_case.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class TestCase
result = TestResult.new
result.test_started
setup
{% begin %}
case @name
{% for method, index in @type.methods.map(&.name) %}
when {{method.stringify}} then {{method.id}}
{% end %}
else
raise "Undefined method. You specified: #{@name}"
end
{% end %}
begin
{% begin %}
case @name
{% for method, index in @type.methods.map(&.name) %}
when {{method.stringify}} then {{method.id}}
{% end %}
else
raise "Undefined method. You specified: #{@name}"
end
{% end %}
rescue e
result.test_failed
end
teardown
result
end
Expand Down
7 changes: 6 additions & 1 deletion part02/src/xunit/test_result.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class TestResult
property run_count : Int32 = 0
property error_count : Int32 = 0

def test_started
@run_count += 1
end

def test_failed
@error_count += 1
end

def summary
"%d run, 0 failed" % [@run_count]
"%d run, %d failed" % [@run_count, @error_count]
end
end