Skip to content

Commit e3c507e

Browse files
author
Robb Kidd
committed
moar doc tests
And with a single doctest present in the code, it will also run any standard minitest tests present, too. They don't all have to switch to doctests.
1 parent d27bbfa commit e3c507e

File tree

6 files changed

+76
-82
lines changed

6 files changed

+76
-82
lines changed

2021/ruby/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ task :test, [:day] do |t, args|
2525
file.write "require 'day#{day}'\n"
2626
end
2727

28-
abort unless system("yard doctest ./day#{day}.rb")
28+
abort unless system("yard doctest -v ./day#{day}.rb")
2929
end
3030

3131
task :new do

2021/ruby/day01.rb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ def initialize(input=nil)
99
@sonar_sweep = input || sonar_sweep_report
1010
end
1111

12+
# @example part 1
13+
# d = Day01.new(PART1_EXAMPLE_INPUT)
14+
# d.part1 #=> 7
15+
#
1216
def part1
1317
num_increases(@sonar_sweep)
1418
end
1519

20+
# @example part 2
21+
# d = Day01.new(PART1_EXAMPLE_INPUT)
22+
# d.part2 #=> 5
23+
#
1624
def part2
1725
sliding_window_increases(@sonar_sweep)
1826
end
@@ -35,10 +43,7 @@ def sliding_window_increases(depths)
3543
def sonar_sweep_report
3644
File.read('../inputs/day01-input.txt').split("\n").map(&:to_i)
3745
end
38-
end
3946

40-
require 'minitest'
41-
class TestDay01 < Minitest::Test
4247
PART1_EXAMPLE_INPUT=[
4348
199,
4449
200,
@@ -51,22 +56,4 @@ class TestDay01 < Minitest::Test
5156
260,
5257
263,
5358
]
54-
55-
def setup
56-
@day01_example = Day01.new(PART1_EXAMPLE_INPUT)
57-
end
58-
59-
def test_part1_example
60-
assert_equal 7, @day01_example.part1
61-
end
62-
63-
def test_part2_example
64-
assert_equal 5, @day01_example.part2
65-
end
66-
end
67-
68-
if ENV.key? 'TEST'
69-
require 'minitest/autorun'
70-
else
71-
Day01.go
7259
end

2021/ruby/day02.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ def self.go
55
puts "Part 2: #{day.part2}"
66
end
77

8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
12+
# @example answer
13+
# new(EXAMPLE_INPUT).part1 #=> 150
14+
#
815
def part1
916
submarine = Sub.new
10-
submarine.follow_instructions(planned_course)
17+
submarine.follow_instructions(@input)
1118
submarine.where_you_at
1219
end
1320

21+
# @example answer
22+
# new(EXAMPLE_INPUT).part2 #=> 900
23+
#
1424
def part2
1525
submarine = SlightlyMoreComplicatedSub.new
16-
submarine.follow_instructions(planned_course)
26+
submarine.follow_instructions(@input)
1727
submarine.where_you_at
1828
end
1929

@@ -169,9 +179,3 @@ def test_sub_can_do_the_aim_thing
169179
assert_equal 40, @test_sub.depth
170180
end
171181
end
172-
173-
if ENV.key? 'TEST'
174-
require 'minitest/autorun'
175-
else
176-
Day02.go
177-
end

2021/ruby/day03.rb

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ def self.go
55
puts "Part 2: #{day.part2}"
66
end
77

8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
12+
# @example
13+
# new(EXAMPLE_INPUT).part1 #=> 198
14+
#
815
def part1
9-
diag = SubDiagnostics.new(report)
16+
diag = SubDiagnostics.new(@input)
1017
diag.power_consumption
1118
end
1219

20+
# @example
21+
# new(EXAMPLE_INPUT).part2 #=> 230
22+
#
1323
def part2
14-
diag = SubDiagnostics.new(report)
24+
diag = SubDiagnostics.new(@input)
1525
diag.life_support_rating
1626
end
1727

18-
def report
28+
def real_input
1929
File.read('../inputs/day03-input.txt')
2030
end
2131

@@ -48,15 +58,27 @@ def initialize(report='')
4858
end
4959
end
5060

61+
# @example
62+
# SubDiagnostics
63+
# .new(Day03::EXAMPLE_INPUT)
64+
# .power_consumption #=> 198
5165
def power_consumption
5266
gamma_rate * epsilon_rate
5367
end
5468

69+
# @example
70+
# SubDiagnostics
71+
# .new(Day03::EXAMPLE_INPUT)
72+
# .gamma_rate #=> 22
5573
def gamma_rate
5674
popular_bits
5775
.to_i(2)
5876
end
5977

78+
# @example
79+
# SubDiagnostics
80+
# .new(Day03::EXAMPLE_INPUT)
81+
# .epsilon_rate #=> 9
6082
def epsilon_rate
6183
popular_bits
6284
.tr('01', '10')
@@ -74,14 +96,26 @@ def popular_bits
7496
.join("")
7597
end
7698

99+
# @example
100+
# SubDiagnostics
101+
# .new(Day03::EXAMPLE_INPUT)
102+
# .life_support_rating #=> 230
77103
def life_support_rating
78104
oxygen_generator_rating * co2_scrubber_rating
79105
end
80106

107+
# @example
108+
# SubDiagnostics
109+
# .new(Day03::EXAMPLE_INPUT)
110+
# .oxygen_generator_rating #=> 23
81111
def oxygen_generator_rating
82112
rating_filter(method(:most_common_bit_at))
83113
end
84114

115+
# @example
116+
# SubDiagnostics
117+
# .new(Day03::EXAMPLE_INPUT)
118+
# .co2_scrubber_rating #=> 10
85119
def co2_scrubber_rating
86120
rating_filter(method(:least_common_bit_at))
87121
end
@@ -121,42 +155,3 @@ def bit_tally(input)
121155
.map{ |position| position.tally }
122156
end
123157
end
124-
125-
require 'minitest'
126-
127-
class TestDay03 < Minitest::Test
128-
129-
def setup
130-
@diag = SubDiagnostics.new(Day03::EXAMPLE_INPUT)
131-
end
132-
133-
def test_part1_gamma_rate
134-
assert_equal 22, @diag.gamma_rate
135-
end
136-
137-
def test_part1_epsilon_rate
138-
assert_equal 9, @diag.epsilon_rate
139-
end
140-
141-
def test_part1_power_consumption
142-
assert_equal 198, @diag.power_consumption
143-
end
144-
145-
def test_part2_oxygen_generator_rating
146-
assert_equal 23, @diag.oxygen_generator_rating
147-
end
148-
149-
def test_part2_co2_scrubber_rating
150-
assert_equal 10, @diag.co2_scrubber_rating
151-
end
152-
153-
def test_part2_life_support_rating
154-
assert_equal 230, @diag.life_support_rating
155-
end
156-
end
157-
158-
if ENV.key? 'TEST'
159-
require 'minitest/autorun'
160-
else
161-
Day03.go
162-
end

2021/ruby/day04.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@ def self.go
55
puts "Part 2: #{day.part2}"
66
end
77

8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
12+
# @example
13+
# new(EXAMPLE_INPUT).part1 #=> 4512
14+
#
815
def part1
9-
bingo = BingoSubSystem.new(input)
16+
bingo = BingoSubSystem.new(@input)
1017
chicken_dinner = bingo.first_winning_board
1118
chicken_dinner.score
1219
end
1320

21+
# @example
22+
# new(EXAMPLE_INPUT).part2 #=> 1924
23+
#
1424
def part2
15-
bingo = BingoSubSystem.new(input)
25+
bingo = BingoSubSystem.new(@input)
1626
tofurky_dinner = bingo.last_winning_board
1727
tofurky_dinner.score
1828
end
1929

20-
def input
30+
def real_input
2131
File.read('../inputs/day04-input.txt')
2232
end
2333

@@ -240,9 +250,3 @@ def test_tile_marking
240250
assert tile.marked?
241251
end
242252
end
243-
244-
if ENV.key? 'TEST'
245-
require 'minitest/autorun'
246-
else
247-
Day04.go
248-
end

2021/ruby/template.rb.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ class <%= day.capitalize %>
55
puts "Part 2: #{day.part2}"
66
end
77

8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
812
def part1
913
end
1014

1115
def part2
1216
end
1317

14-
def input
18+
def real_input
1519
File.read('../inputs/<%= day.downcase %>-input.txt')
1620
end
1721

0 commit comments

Comments
 (0)