-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrefactoring_runner.rb
123 lines (108 loc) · 3.06 KB
/
refactoring_runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require_relative 'correct_answer_behavior'
require 'fileutils'
module FixtureHandler
def self.clear_fixtures
FileUtils.rm_rf(fixtures_dir)
end
def self.create_fixture_dir
FileUtils.mkdir(fixtures_dir)
end
def self.write_fixture index, text
File.open(fixture_path(index), "w") do |file|
file.write(text)
end
end
def self.fixture_exists? index
File.exists?(fixture_path(index))
end
def self.read_fixture index
File.read(fixture_path(index))
end
def self.fixture_path index
"#{fixtures_dir}/#{index}.txt"
end
def self.fixtures_dir
"#{File.expand_path(File.dirname(__FILE__))}/fixtures"
end
end
module StdOutToStringRedirector
require 'stringio'
def self.redirect_stdout_to_string
sio = StringIO.new
old_stdout, $stdout = $stdout, sio
yield
$stdout = old_stdout
sio.string
end
end
SIMULATIONS_COUNT = 5000
def run_simulation index = nil
result = CorrectAnswerBehavior.new(index).was_correctly_answered
puts "result was #{result}"
end
def capture_simulation_output index
StdOutToStringRedirector.redirect_stdout_to_string do
run_simulation(index)
end
end
def clean_fixtures
FixtureHandler.clear_fixtures
end
def record_fixtures
SIMULATIONS_COUNT.times do |index|
raise "You need to clean recorded simulation results first!" if FixtureHandler.fixture_exists?(index)
end
FixtureHandler.create_fixture_dir
SIMULATIONS_COUNT.times do |index|
FixtureHandler.write_fixture(index, capture_simulation_output(index))
end
rescue RuntimeError => e
puts "ERROR!!!"
puts e.message
end
minitest_class = nil
begin
require "minitest"
minitest_class = Minitest::Test
def run_minitest
Minitest.run
end
rescue LoadError
require "minitest/unit"
minitest_class = MiniTest::Unit::TestCase
def run_minitest
MiniTest::Unit.new.run []
end
end
class TestOutput < minitest_class
def test_output
SIMULATIONS_COUNT.times do |index|
raise "You need to record simulation results first!" unless FixtureHandler.fixture_exists?(index)
assert_equal(FixtureHandler.read_fixture(index), capture_simulation_output(index))
end
puts "OK."
rescue RuntimeError => e
puts "ERROR!!!"
puts e.message
end
end
case ARGV[0].to_s.downcase
when "-h", "--help", "help"
puts "Usage:"
puts " ruby #{__FILE__} [-h|--help|help] - shows help screen."
puts " ruby #{__FILE__} [-c|--clean|clean] - clean recorded results of simulation."
puts " ruby #{__FILE__} [-r|--record|record] - records #{SIMULATIONS_COUNT} results of simulation."
puts " ruby #{__FILE__} [-t|--test|test] - tests against #{SIMULATIONS_COUNT} recorded results of simulation."
puts " ruby #{__FILE__} <ANY_NUMBER> - shows result of simulation initialized with <ANY_NUMBER>."
puts " ruby #{__FILE__} - shows result of random simulation."
when "-c", "--clean", "clean"
clean_fixtures
when "-r", "--record", "record"
record_fixtures
when "-t", "--test", "test"
run_minitest
when /\d(.\d+)?/
run_simulation ARGV[0].to_f
else
run_simulation
end