-
-
Notifications
You must be signed in to change notification settings - Fork 525
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
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 | ||
|
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 | ||
<% end %> | ||
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> | ||
def test_bookkeeping | ||
skip | ||
assert_equal <%= version.next %>, BookKeeping::VERSION | ||
end | ||
end |
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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this code generated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I wasn't sure whether to leave it There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
There was a problem hiding this comment.
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.