Module: GreenDay::TestBuilder

Defined in:
lib/green_day/test_builder.rb

Class Method Summary collapse

Class Method Details

.build_example(submit_file_path, input, output) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/green_day/test_builder.rb', line 15

def build_example(submit_file_path, input, output)
  <<~SPEC
    #{tab}it 'test with #{unify_cr_lf(input).chomp}' do
    #{tab}#{tab}io = IO.popen('ruby #{submit_file_path}', 'w+')
    #{tab}#{tab}io.puts(#{unify_cr_lf(input)})
    #{tab}#{tab}io.close_write
    #{tab}#{tab}expect(io.readlines.join).to eq(#{unify_cr_lf(output)})
    #{tab}end
  SPEC
end

.build_test(submit_file_path, sample_answers) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/green_day/test_builder.rb', line 7

def build_test(submit_file_path, sample_answers)
  <<~SPEC
    RSpec.describe '#{submit_file_path}' do
    #{sample_answers.map { |sample_answer| build_example(submit_file_path, sample_answer.input, sample_answer.output) }.join("\n")}
    end
  SPEC
end

.tabObject



32
33
34
# File 'lib/green_day/test_builder.rb', line 32

def tab
  "\s\s"
end

.unify_cr_lf(string) ⇒ Object



26
27
28
29
30
# File 'lib/green_day/test_builder.rb', line 26

def unify_cr_lf(string)
  return unless string # たまに画像で例を出してくるとsampleの文字がなくなる

  string.gsub(/\R/, "\n").dump
end