Skip to content

Commit 823be7e

Browse files
committed
space-age: added generator
1 parent 7045d99 commit 823be7e

File tree

7 files changed

+94
-16
lines changed

7 files changed

+94
-16
lines changed

exercises/space-age/.meta/.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class SpaceAgeCase < ExerciseCase
2+
using Generator::Underscore
3+
4+
SECONDS_PER_YEAR_ON_EARTH = 31557600
5+
6+
def workload
7+
[
8+
"age = SpaceAge.new(#{seconds.underscore})",
9+
assertion,
10+
].join("\n")
11+
end
12+
13+
private
14+
15+
def assertion
16+
[
17+
" #{assertion_for_location(expected, planet)}",
18+
]
19+
end
20+
21+
def assertion_for_location(expected, location)
22+
"assert_in_delta #{expected}, age.on_#{location.downcase}, DELTA"
23+
end
24+
25+
end
26+
27+
SpaceAgeCases = proc do |data|
28+
JSON.parse(data)['cases'].map.with_index do |row, i|
29+
SpaceAgeCase.new(row.merge(index: i))
30+
end
31+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env ruby
2+
gem 'minitest', '>= 5.0.0'
3+
require 'minitest/autorun'
4+
require_relative '<%= exercise_name %>'
5+
6+
# Common test data version: <%= abbreviated_commit_hash %>
7+
class <%= exercise_name_camel %>Test < Minitest::Test
8+
DELTA = 0.01
9+
10+
<% test_cases.each do |test_case| %>
11+
def <%= test_case.name %>
12+
<%= test_case.skipped %>
13+
<%= test_case.workload %>
14+
end
15+
16+
<% end %>
17+
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
18+
19+
def test_bookkeeping
20+
skip
21+
assert_equal <%= version %>, BookKeeping::VERSION
22+
end
23+
end

exercises/space-age/example.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
module BookKeeping
2+
VERSION = 1
3+
end
4+
15
class SpaceAge
26
attr_reader :seconds
37

exercises/space-age/space_age_test.rb

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,77 @@
33
require 'minitest/autorun'
44
require_relative 'space_age'
55

6+
# Common test data version: 7c63e40
67
class SpaceAgeTest < Minitest::Test
78
DELTA = 0.01
89

9-
def test_age_in_seconds
10-
age = SpaceAge.new(1_000_000)
11-
assert_in_delta 1_000_000, age.seconds, DELTA
12-
end
13-
14-
def test_age_in_earth_years
15-
skip
10+
def test_age_on_earth
11+
# skip
1612
age = SpaceAge.new(1_000_000_000)
1713
assert_in_delta 31.69, age.on_earth, DELTA
1814
end
1915

20-
def test_age_in_mercury_years
16+
def test_age_on_mercury
2117
skip
2218
age = SpaceAge.new(2_134_835_688)
23-
assert_in_delta 67.65, age.on_earth, DELTA
2419
assert_in_delta 280.88, age.on_mercury, DELTA
2520
end
2621

27-
def test_age_in_venus_years
22+
def test_age_on_venus
2823
skip
2924
age = SpaceAge.new(189_839_836)
30-
assert_in_delta 6.02, age.on_earth, DELTA
3125
assert_in_delta 9.78, age.on_venus, DELTA
3226
end
3327

3428
def test_age_on_mars
3529
skip
3630
age = SpaceAge.new(2_329_871_239)
37-
assert_in_delta 73.83, age.on_earth, DELTA
3831
assert_in_delta 39.25, age.on_mars, DELTA
3932
end
4033

4134
def test_age_on_jupiter
4235
skip
4336
age = SpaceAge.new(901_876_382)
44-
assert_in_delta 28.58, age.on_earth, DELTA
4537
assert_in_delta 2.41, age.on_jupiter, DELTA
4638
end
4739

4840
def test_age_on_saturn
4941
skip
5042
age = SpaceAge.new(3_000_000_000)
51-
assert_in_delta 95.06, age.on_earth, DELTA
5243
assert_in_delta 3.23, age.on_saturn, DELTA
5344
end
5445

5546
def test_age_on_uranus
5647
skip
5748
age = SpaceAge.new(3_210_123_456)
58-
assert_in_delta 101.72, age.on_earth, DELTA
5949
assert_in_delta 1.21, age.on_uranus, DELTA
6050
end
6151

6252
def test_age_on_neptune
6353
skip
6454
age = SpaceAge.new(8_210_123_456)
65-
assert_in_delta 260.16, age.on_earth, DELTA
6655
assert_in_delta 1.58, age.on_neptune, DELTA
6756
end
57+
58+
# Problems in exercism evolve over time, as we find better ways to ask
59+
# questions.
60+
# The version number refers to the version of the problem you solved,
61+
# not your solution.
62+
#
63+
# Define a constant named VERSION inside of the top level BookKeeping
64+
# module, which may be placed near the end of your file.
65+
#
66+
# In your file, it will look like this:
67+
#
68+
# module BookKeeping
69+
# VERSION = 1 # Where the version number matches the one in the test.
70+
# end
71+
#
72+
# If you are curious, read more about constants on RubyDoc:
73+
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
74+
75+
def test_bookkeeping
76+
skip
77+
assert_equal 1, BookKeeping::VERSION
78+
end
6879
end

lib/generator/exercise_cases.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def skipped
2222
# ], 4
2323
# )
2424
def indent_lines(code, depth, separator = "\n")
25+
require 'pry'
26+
binding.pry
2527
code.join(separator + ' ' * depth)
2628
end
2729

lib/generator/underscore.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ def underscore
55
downcase.gsub(/[- ]/, '_').gsub(/[^\w?]/, '')
66
end
77
end
8+
9+
refine Fixnum do
10+
def underscore
11+
self.to_s.reverse.gsub(/...(?=.)/, '\&_').reverse
12+
end
13+
end
814
end
915
end

0 commit comments

Comments
 (0)