-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_solution.rb
More file actions
executable file
·103 lines (90 loc) · 3.1 KB
/
run_solution.rb
File metadata and controls
executable file
·103 lines (90 loc) · 3.1 KB
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
#!/usr/bin/env -S ruby
# frozen_string_literal: true
arguments_missing = "Pass these arguments:\n" \
" - The exercise subfolder\n" \
" - The argument to pass to the solution method\n" \
" - The method to use, if there is more than one\n" \
" - The Ruby solution file\n"
if ARGV.size > 0
exercise_folder = ARGV[0]
argument = ARGV[1] # String
method_name = ARGV[2]
solution = ARGV[3] || 'solution'
else
puts "\nError:\n" +
arguments_missing +
"Example\n" \
" .\/run_solution.rb #{exercise_folder || '1_1_number_format'} 2000 pretty_number solution\n\n"
exit(1)
end
exercise_path = "exercises/#{exercise_folder}/"
begin
require_relative "#{exercise_path}test_data"
rescue Exception => e
puts "\nError: #{e}\n\n"
puts "Unable to find '#{exercise_path}test_data.rb'"
puts "Are you sure that the exercise folder '#{exercise_folder}' is correct?"
exit(1)
end
require_relative 'utils/quizzes_utils'
method_name = METHODS_MULTIPLE_ARITY.keys.first if METHODS_MULTIPLE_ARITY.keys.size == 1
if method_name.nil? || method_name.empty?
puts "\nError: there are more than one possible methods to invoke\n" \
"please include it as a last parameter\n" \
"Examples:\n"
METHODS_MULTIPLE_ARITY.keys.each do |m|
puts " .\/run_solution.rb #{exercise_folder || '1_1_number_format'} 'params' #{m} solution"
end
exit(1)
elsif !METHODS_MULTIPLE_ARITY.keys.include?(method_name.to_sym)
puts "\nError:\n\n" \
"The method '#{method_name}' is not present. Are you sure it is correct?"
exit(1)
end
splat_arguments = METHODS_MULTIPLE_ARITY[method_name.to_sym]
# method-specific details, if there is more than one method:
details = QuizzesUtils.method_details(method_name)
unless details
puts 'Unable to read the test data'
exit(1)
end
param_transformation, test_data = details
if argument && param_transformation.is_a?(Proc)
argument = param_transformation.call(argument)
end
unless argument
puts "\n" \
"Please pass at least one argument with the data to use " \
"(and if there is more than one solution, the solution file).\n" +
QuizzesUtils.examples_message_run(TEST_DATA.keys, exercise_folder:, splat_arguments:)
exit(1)
end
solution_file = "#{exercise_path}#{solution}.rb"
unless File.exist?(solution_file)
puts "\n" \
"Error:\n" \
" No solutions found as '#{solution_file}'\n\n"
possibles = Dir["#{exercise_path}solution*.rb"].collect{|fn| File.basename(fn, '.rb')}
unless possibles.empty?
puts 'Could it be one of these?'
possibles.each do |fn|
puts "- #{fn}"
end
end
exit(1)
end
begin
require_relative "#{exercise_path}#{solution}"
rescue Exception => e
puts "\n" \
"Error: #{e}\n\n" +
arguments_missing +
QuizzesUtils.examples_message_run(test_data.keys, argument, exercise_folder:, splat_arguments:)
exit(1)
end
result = if splat_arguments
send method_name, *argument
else
send method_name, argument
end
puts "For '#{argument}', the solution is '#{result}'"