Skip to content

space-age: Add generator #583

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
May 19, 2017
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 exercises/space-age/.meta/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
11 changes: 11 additions & 0 deletions exercises/space-age/.meta/generator/space_age_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'generator/exercise_case'

class SpaceAgeCase < Generator::ExerciseCase
using Generator::Underscore
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'm not too familiar with refinements, but it looks like this using statement is necessary.
It doesn't look like the using statement in ExerciseCase gets inherited by SpaceAgeCase.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I also discovered this and commented here: #583 (comment)

It turns out you can not, due to lexical scope issues. Sorry for misleading you.
http://blog.honeybadger.io/understanding-ruby-refinements-and-lexical-scope/

Looks like github managed to hide the comment.

I also made an issue about addressing this here in xruby: #645

But including it for now is good. 👍


Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Insti; I definitely didn't need that constant, it was left over from when each assertion was unnecessarily testing the age on Earth.

def workload
indent_lines(["age = SpaceAge.new(#{seconds.underscore})",
"assert_in_delta #{expected}, age.on_#{planet.downcase}, DELTA"
], 4)
end
end
25 changes: 25 additions & 0 deletions exercises/space-age/.meta/generator/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby
require 'minitest/autorun'
require_relative '<%= exercise_name %>'

# Common test data version: <%= canonical_data_version %> <%= abbreviated_commit_hash %>
class <%= exercise_name_camel %>Test < Minitest::Test
# assert_in_delta will pass if the difference
# between the values being compared is less
# than the allowed delta
DELTA = 0.01

<% test_cases.each_with_index do |test_case, idx| %>
def <%= test_case.name %>
<%= test_case.skipped(idx) %>
<%= test_case.workload %>
end

<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>

def test_bookkeeping
skip
assert_equal <%= version %>, BookKeeping::VERSION
end
end
4 changes: 4 additions & 0 deletions exercises/space-age/.meta/solutions/space_age.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module BookKeeping
VERSION = 1
end

class SpaceAge
attr_reader :seconds

Expand Down
46 changes: 30 additions & 16 deletions exercises/space-age/space_age_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,80 @@
require 'minitest/autorun'
require_relative 'space_age'

# Common test data version: 1.0.0 7c63e40
class SpaceAgeTest < Minitest::Test
# assert_in_delta will pass if the difference
# between the values being compared is less
# than the allowed delta
DELTA = 0.01

def test_age_in_seconds
age = SpaceAge.new(1_000_000)
assert_in_delta 1_000_000, age.seconds, DELTA
end
Copy link
Contributor Author

@ajwann ajwann Apr 30, 2017

Choose a reason for hiding this comment

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

This method was missing from x-common, so I omitted it. If it's needed, I can add it to x-common or manually hack it in before the test_age_on_earth method in space_age_cases.rb

Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving it out is fine. If it's not specified in the readme or the canonical data it probably shouldn't be there.


def test_age_in_earth_years
skip
def test_age_on_earth
# skip
age = SpaceAge.new(1_000_000_000)
assert_in_delta 31.69, age.on_earth, DELTA
end

def test_age_in_mercury_years
def test_age_on_mercury
skip
age = SpaceAge.new(2_134_835_688)
assert_in_delta 67.65, age.on_earth, DELTA
assert_in_delta 280.88, age.on_mercury, DELTA
end

def test_age_in_venus_years
def test_age_on_venus
skip
age = SpaceAge.new(189_839_836)
assert_in_delta 6.02, age.on_earth, DELTA
assert_in_delta 9.78, age.on_venus, DELTA
end

def test_age_on_mars
skip
age = SpaceAge.new(2_329_871_239)
assert_in_delta 73.83, age.on_earth, DELTA
assert_in_delta 39.25, age.on_mars, DELTA
end

def test_age_on_jupiter
skip
age = SpaceAge.new(901_876_382)
assert_in_delta 28.58, age.on_earth, DELTA
assert_in_delta 2.41, age.on_jupiter, DELTA
end

def test_age_on_saturn
skip
age = SpaceAge.new(3_000_000_000)
assert_in_delta 95.06, age.on_earth, DELTA
assert_in_delta 3.23, age.on_saturn, DELTA
end

def test_age_on_uranus
skip
age = SpaceAge.new(3_210_123_456)
assert_in_delta 101.72, age.on_earth, DELTA
assert_in_delta 1.21, age.on_uranus, DELTA
end

def test_age_on_neptune
skip
age = SpaceAge.new(8_210_123_456)
assert_in_delta 260.16, age.on_earth, DELTA
assert_in_delta 1.58, age.on_neptune, DELTA
end

# 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_equal 1, BookKeeping::VERSION
end
end
6 changes: 6 additions & 0 deletions lib/generator/underscore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ def underscore
downcase.gsub(/[- ]/, '_').gsub(/[^\w?]/, '')
end
end

refine Fixnum do
def underscore
self.to_s.reverse.gsub(/...(?=.)/, '\&_').reverse
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good 👍

end
end
end
end