Skip to content

[WIP] nth-prime: Add test file generator. #411

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

Merged
merged 1 commit into from
Oct 5, 2016
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
7 changes: 7 additions & 0 deletions bin/generate-nth-prime
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require_relative '../lib/helper'
require 'generator'
require 'nth_prime_cases'

Generator.new('nth-prime', NthPrimeCases).generate
1 change: 1 addition & 0 deletions exercises/nth-prime/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
4 changes: 4 additions & 0 deletions exercises/nth-prime/example.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module BookKeeping
VERSION = 1
end

class Prime
def self.nth(n)
if n < 1
Expand Down
23 changes: 23 additions & 0 deletions exercises/nth-prime/example.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'nth_prime'

# Test data version:
# <%= sha1 %>
# Rubocop directives
# rubocop:disable Style/NumericLiterals
#
class NthPrimeTest < Minitest::Test<% test_cases.each do |test_case| %>
def <%= test_case.name %><% if test_case.skipped? %>
skip<% end %><% if test_case.raises_error? %>
assert_raises(ArgumentError) { <%= test_case.actual %> }<% else %>
assert_equal <%= test_case.expected %>, <%= test_case.actual %><% end %>
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to remove the conditional from the template.
See how https://github.com/exercism/xruby/blob/master/lib/rna_transcription_cases.rb handles this.

<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
def test_bookkeeping
skip
assert_equal <%= version.next %>, BookKeeping::VERSION
end
end
43 changes: 33 additions & 10 deletions exercises/nth-prime/nth_prime_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/usr/bin/env ruby

gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'

require_relative 'nth_prime'

class TestPrimes < Minitest::Test
def test_first
# Test data version:
# bb79e10
# Rubocop directives
# rubocop:disable Style/NumericLiterals
#
class NthPrimeTest < Minitest::Test
def test_first_prime
assert_equal 2, Prime.nth(1)
end

def test_second
def test_second_prime
skip
assert_equal 3, Prime.nth(2)
end
Expand All @@ -22,13 +25,33 @@ def test_sixth_prime

def test_big_prime
skip
assert_equal 104_743, Prime.nth(10_001)
assert_equal 104743, Prime.nth(10001)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to be nice, you could make a helper method that formatted the numbers from the json into underscored versions in the test file.

(This is not at all a requirement, just an optional extra if you feel like it.)

end

def test_there_is_no_zeroth_prime
skip
assert_raises(ArgumentError) { Prime.nth(0) }
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this code generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json file generates the false case. After it generated that I changed it to raise the exception.

I wasn't sure whether to leave it false or change it to this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think raising an ArgumentError is ok.

The important thing is that the generator generates a test file that doesn't need further modification, so if/when the .json file changes in future, we can just re-generate the test file and not worry about any special cases lurking in there that need manual tweaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated example.tt to generate the exception test.


def test_weird_case
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_raises ArgumentError do
Prime.nth(0)
end
assert_equal 1, BookKeeping::VERSION
end
end
23 changes: 23 additions & 0 deletions lib/nth_prime_cases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class NthPrimeCase < OpenStruct
def name
'test_%s' % description.downcase.gsub(/[ -]/, '_')
end

def actual
"Prime.nth(#{input})"
end

def raises_error?
expected == false
end

def skipped?
index > 0
end
end

NthPrimeCases = proc do |data|
JSON.parse(data)['cases'].map.with_index do |row, i|
NthPrimeCase.new(row.merge('index' => i))
end
end