Skip to content

Commit

Permalink
exercism#37 - Upgrade to version 3 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunk13 committed Jun 27, 2023
1 parent 001e621 commit b869c27
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
26 changes: 21 additions & 5 deletions lib/extract_test_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def call
test: test_identifier,
name: test_name,
test_code: test_code,
index: index
index: index,
task_id: task_id
}
end

Expand Down Expand Up @@ -52,9 +53,6 @@ def test_name
# - Plus an bits we've chosen to add back as the test_code part
memoize
def test_code
# Get the lines excluding the first (def) and last (end)
body_line_numbers = ((test_node.first_line + 1)..(test_node.last_line - 1))

# Map through those lines, skipping any that were
# part of assertions
test_code = body_line_numbers.map do |idx|
Expand All @@ -63,13 +61,26 @@ def test_code
c = code_for_line(idx)

# Only return if it's not a skip comment
c.start_with?(/\s*#\s*skip/) ? nil : c
c.start_with?(/\s*#\s*skip/, /\s*###\s*task_id/) ? nil : c
end.compact.join("").rstrip

# Align everything to the left as the final step
clean_leading_whitespace(test_code)
end

memoize
def task_id
body_line_numbers.map do |idx|
next if ignore_line_numbers.include?(idx)

c = code_for_line(idx)
# Find a line started with `### task_id` and get the number
if c.start_with?(/\s*###\s*task_id/)
c.chomp.strip.delete('### task_id:').to_i
end
end.compact.first
end

# Remove the minimum amount of leading whitespace
# from all lines
def clean_leading_whitespace(multiline)
Expand All @@ -82,5 +93,10 @@ def clean_leading_whitespace(multiline)
def code_for_line(one_indexed_idx)
filelines[one_indexed_idx - 1]
end

def body_line_numbers
# Get the lines excluding the first (def) and last (end)
((test_node.first_line + 1)..(test_node.last_line - 1))
end
end
end
1 change: 1 addition & 0 deletions lib/minitest_ext/exercism_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def to_h
hash[:status] = status
hash[:output] = output if attach_output?
hash[:message] = message if attach_message?
hash[:task_id] = metadata[:task_id]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/write_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call

def json
{
version: 2,
version: 3,
status: status,
message: message,
tests: tests
Expand Down
5 changes: 3 additions & 2 deletions test/attacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AttacksTest < Minitest::Test
def test_large_output_is_truncated
assert_fixture(
:attack_large_output, {
version: 2,
version: 3,
status: :fail,
message: nil,
tests: [
Expand All @@ -13,7 +13,8 @@ def test_large_output_is_truncated
test_code: 'assert_equal "One for you, one for me.", TwoFer.two_fer',
status: :fail,
output: %(#{Array.new(500) { 'a' }.join}\n\n...Output was truncated. Please limit to 500 chars...),
message: "Expected: \"One for you, one for me.\"\n Actual: false"
message: "Expected: \"One for you, one for me.\"\n Actual: false",
task_id: nil
}
]
}
Expand Down
21 changes: 14 additions & 7 deletions test/extract_metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def test_assert_equal
test: "test_assert_equal_works_properly",
name: "Assert equal works properly",
test_code: %(some_result = TwoFer.two_fer\nassert_equal "One for you, one for me.", some_result),
index: 0
index: 0,
task_id: 123
}]

actual = TestRunner::ExtractMetadata.(File.expand_path("fixtures/metadata/assert_equal.rb", __dir__))
Expand All @@ -19,7 +20,8 @@ def test_no_skips
test: "test_skip_works_properly",
name: "Skip works properly",
test_code: "something = \"Something\"\nassert something.present?",
index: 0
index: 0,
task_id: 456
}]

actual = TestRunner::ExtractMetadata.(File.expand_path("fixtures/metadata/skip.rb", __dir__))
Expand All @@ -31,7 +33,8 @@ def test_no_skip_comments
test: "test_skip_works_properly",
name: "Skip works properly",
test_code: "something = \"Something\"\nassert something.present?",
index: 0
index: 0,
task_id: 789
}]

actual = TestRunner::ExtractMetadata.(File.expand_path("fixtures/metadata/skip_comment.rb", __dir__))
Expand All @@ -44,25 +47,29 @@ def test_extracting_indices
test: "test_zebra",
name: "Zebra",
test_code: %(some_result = TwoFer.two_fer("zebra")\nassert_equal "One for you, one for zebra.", some_result),
index: 0
index: 0,
task_id: 789
},
{
test: "test_anaconda",
name: "Anaconda",
test_code: %(some_result = TwoFer.two_fer("anaconda")\nassert_equal "One for you, one for anaconda.", some_result),
index: 1
index: 1,
task_id: nil
},
{
test: "test_gorilla",
name: "Gorilla",
test_code: %(some_result = TwoFer.two_fer("gorilla")\nassert_equal "One for you, one for gorilla.", some_result),
index: 2
index: 2,
task_id: nil
},
{
test: "test_boa",
name: "Boa",
test_code: %(some_result = TwoFer.two_fer("boa")\nassert_equal "One for you, one for boa.", some_result),
index: 3
index: 3,
task_id: nil
}
]

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/metadata/assert_equal.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SomeTest < Minitest::Test
def test_assert_equal_works_properly
### task_id: 123
some_result = TwoFer.two_fer
assert_equal "One for you, one for me.", some_result
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/metadata/indices.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SomeTest < Minitest::Test
def test_zebra
### task_id: 789
some_result = TwoFer.two_fer("zebra")
assert_equal "One for you, one for zebra.", some_result
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/metadata/skip.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SomeTest < Minitest::Test
def test_skip_works_properly
### task_id: 456
skip
something = "Something"
assert something.present?
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/metadata/skip_comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SomeTest < Minitest::Test
def test_skip_works_properly
### task_id: 789
#skip
# skip
something = "Something"
Expand Down
35 changes: 22 additions & 13 deletions test/test_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ def test_pass
assert_fixture(
:pass,
{
version: 2,
version: 3,
status: :pass,
message: nil,
tests: [
{
name: "No name given",
status: :pass,
test_code: %(assert_equal "One for you, one for me.", TwoFer.two_fer)
test_code: %(assert_equal "One for you, one for me.", TwoFer.two_fer),
task_id: nil
},
{
name: 'A name given',
test_code: 'assert_equal "One for Alice, one for me.", TwoFer.two_fer("Alice")',
status: :pass
status: :pass,
task_id: nil
},
{
name: "Another name given",
status: :pass,
test_code: 'assert_equal "One for Bob, one for me.", TwoFer.two_fer("Bob")'
test_code: 'assert_equal "One for Bob, one for me.", TwoFer.two_fer("Bob")',
task_id: nil
}
]
}
Expand All @@ -33,19 +36,21 @@ def test_pass_ruby_3
assert_fixture(
:pass_ruby_3_syntax,
{
version: 2,
version: 3,
status: :pass,
message: nil,
tests: [
{
name: "Rightward assign",
status: :pass,
test_code: %(assert_equal Ruby3Syntax.rightward_assign, 'is fun')
test_code: %(assert_equal Ruby3Syntax.rightward_assign, 'is fun'),
task_id: nil
},
{
name: "Endless method def",
status: :pass,
test_code: %(assert_equal Ruby3Syntax.endless_methods, 'are fun')
test_code: %(assert_equal Ruby3Syntax.endless_methods, 'are fun'),
task_id: nil
}
]
}
Expand All @@ -55,7 +60,7 @@ def test_pass_ruby_3
def test_fail
assert_fixture(
:fail, {
version: 2,
version: 3,
status: :fail,
message: nil,
tests: [
Expand All @@ -64,19 +69,22 @@ def test_fail
test_code: %(assert_equal "One for you, one for me.", TwoFer.two_fer),
status: :fail,
message: %(Expected: \"One for you, one for me.\"\n Actual: \"One for fred, one for me.\"),
output: "The name is fred.\nHere's another line.\n"
output: "The name is fred.\nHere's another line.\n",
task_id: nil
},
{
name: "A name given",
test_code: 'assert_equal "One for Alice, one for me.", TwoFer.two_fer("Alice")',
status: :pass,
output: "The name is Alice.\nHere's another line.\n"
output: "The name is Alice.\nHere's another line.\n",
task_id: nil
},
{
name: "Another name given",
test_code: 'assert_equal "One for Bob, one for me.", TwoFer.two_fer("Bob")',
status: :pass,
output: "The name is Bob.\nHere's another line.\n"
output: "The name is Bob.\nHere's another line.\n",
task_id: nil
}
]
}
Expand All @@ -95,15 +103,16 @@ def test_deep_exception
assert_fixture(
:deep_exception,
{
version: 2,
version: 3,
status: :fail,
message: nil,
tests: [
{
name: "No name given",
test_code: 'assert_equal "One for you, one for me.", TwoFer.two_fer',
status: :error,
message: message
message: message,
task_id: nil
}
]
}
Expand Down

0 comments on commit b869c27

Please sign in to comment.