From f11ad7b7fb91877b6a4d6ba89c8ce181b4547a62 Mon Sep 17 00:00:00 2001 From: ajwann Date: Mon, 20 Feb 2017 14:13:12 -0500 Subject: [PATCH] space-age: added generator --- exercises/space-age/.meta/.version | 1 + .../.meta/generator/space_age_case.rb | 11 +++++ .../.meta/generator/test_template.erb | 25 ++++++++++ .../space-age/.meta/solutions/space_age.rb | 4 ++ exercises/space-age/space_age_test.rb | 46 ++++++++++++------- lib/generator/underscore.rb | 6 +++ 6 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 exercises/space-age/.meta/.version create mode 100644 exercises/space-age/.meta/generator/space_age_case.rb create mode 100644 exercises/space-age/.meta/generator/test_template.erb diff --git a/exercises/space-age/.meta/.version b/exercises/space-age/.meta/.version new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/exercises/space-age/.meta/.version @@ -0,0 +1 @@ +1 diff --git a/exercises/space-age/.meta/generator/space_age_case.rb b/exercises/space-age/.meta/generator/space_age_case.rb new file mode 100644 index 0000000000..52b0fe7d4d --- /dev/null +++ b/exercises/space-age/.meta/generator/space_age_case.rb @@ -0,0 +1,11 @@ +require 'generator/exercise_case' + +class SpaceAgeCase < Generator::ExerciseCase + using Generator::Underscore + + def workload + indent_lines(["age = SpaceAge.new(#{seconds.underscore})", + "assert_in_delta #{expected}, age.on_#{planet.downcase}, DELTA" + ], 4) + end +end diff --git a/exercises/space-age/.meta/generator/test_template.erb b/exercises/space-age/.meta/generator/test_template.erb new file mode 100644 index 0000000000..0a17e4fe4a --- /dev/null +++ b/exercises/space-age/.meta/generator/test_template.erb @@ -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 diff --git a/exercises/space-age/.meta/solutions/space_age.rb b/exercises/space-age/.meta/solutions/space_age.rb index 9ef3931d24..0e2039dda5 100644 --- a/exercises/space-age/.meta/solutions/space_age.rb +++ b/exercises/space-age/.meta/solutions/space_age.rb @@ -1,3 +1,7 @@ +module BookKeeping + VERSION = 1 +end + class SpaceAge attr_reader :seconds diff --git a/exercises/space-age/space_age_test.rb b/exercises/space-age/space_age_test.rb index 360a10de8b..f0340c557b 100755 --- a/exercises/space-age/space_age_test.rb +++ b/exercises/space-age/space_age_test.rb @@ -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 - - 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 diff --git a/lib/generator/underscore.rb b/lib/generator/underscore.rb index 46d5a94559..79727ffff7 100644 --- a/lib/generator/underscore.rb +++ b/lib/generator/underscore.rb @@ -5,5 +5,11 @@ def underscore downcase.gsub(/[- ]/, '_').gsub(/[^\w?]/, '') end end + + refine Fixnum do + def underscore + self.to_s.reverse.gsub(/...(?=.)/, '\&_').reverse + end + end end end