Class: Shellfish::Application
- Inherits:
-
Object
- Object
- Shellfish::Application
- Defined in:
- lib/shellfish/application.rb
Instance Method Summary collapse
-
#initialize(argv) ⇒ Application
constructor
A new instance of Application.
- #on_ng(problem, input, output) ⇒ Object
- #on_ok(problem, input, output) ⇒ Object
- #on_quit ⇒ Object
- #on_show_problem(problem) ⇒ Object
- #on_skip ⇒ Object
- #play ⇒ Object
- #show_diff(output, expected) ⇒ Object
- #show_problem(problem) ⇒ Object
- #show_string(string) ⇒ Object
- #start ⇒ Object
- #try_problem(problem) ⇒ Object
Constructor Details
#initialize(argv) ⇒ Application
Returns a new instance of Application.
13 14 15 16 17 18 19 20 |
# File 'lib/shellfish/application.rb', line 13 def initialize(argv) @argv = argv if @argv.empty? @argv = [File.('../../examples/fizzbuzz.rb', File.dirname(__FILE__))] end @loader = ProblemLoader.new @differ = RSpec::Expectations::Differ.new end |
Instance Method Details
#on_ng(problem, input, output) ⇒ Object
74 75 76 77 78 |
# File 'lib/shellfish/application.rb', line 74 def on_ng(problem, input, output) puts "<red>NG</red>".termcolor puts show_diff output, problem.expected_result end |
#on_ok(problem, input, output) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/shellfish/application.rb', line 65 def on_ok(problem, input, output) puts "<green>OK</green>".termcolor puts show_string output puts "<green>Congratulations!</green>".termcolor puts raise NextProblemException end |
#on_quit ⇒ Object
85 86 87 |
# File 'lib/shellfish/application.rb', line 85 def on_quit raise QuitException end |
#on_show_problem(problem) ⇒ Object
89 90 91 92 |
# File 'lib/shellfish/application.rb', line 89 def on_show_problem(problem) show_problem problem raise SkipEvaluationException end |
#on_skip ⇒ Object
80 81 82 83 |
# File 'lib/shellfish/application.rb', line 80 def on_skip puts "<yellow>Skipped</yellow>".termcolor raise NextProblemException end |
#play ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/shellfish/application.rb', line 30 def play @problem_count = @argv.size @current_count = 1 @argv.each do |file| try_problem @loader.load(file) @current_count += 1 end rescue QuitException puts "Quitting shellfish..." ensure puts "Good bye." end |
#show_diff(output, expected) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/shellfish/application.rb', line 94 def show_diff(output, expected) diff = @differ.diff_as_string(output, expected).gsub(/^\n+/, '') colored_diff = '' diff.each_line do |line| if line[0] == '@' colored_diff += "<green><bold>#{line}</bold></green>".termcolor elsif line[0] == '+' colored_diff += "<blue><bold>#{line}</bold></blue>".termcolor elsif line[0] == '-' colored_diff += "<red><bold>#{line}</bold></red>".termcolor else colored_diff += line end end show_string colored_diff end |
#show_problem(problem) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/shellfish/application.rb', line 111 def show_problem(problem) puts ("<blue>Problem</blue> (#{@current_count}/#{@problem_count}): " + "<bold>#{problem.subject}</bold>").termcolor puts "Description: #{problem.description}" if problem.desc? puts puts "<bold>Expected Result:</bold>".termcolor show_string "<cyan>#{problem.expected_result}</cyan>".termcolor puts end |
#show_string(string) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/shellfish/application.rb', line 121 def show_string(string) puts "-" * 72 print string puts puts "-" * 72 end |
#start ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/shellfish/application.rb', line 22 def start opt = OptionParser.new opt.version = VERSION opt.parse!(@argv) play end |
#try_problem(problem) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/shellfish/application.rb', line 43 def try_problem(problem) show_problem(problem) while input = ::Readline.readline('shellfish $ ', true) begin on_skip if input =~ /^\s*:(?:skip|next)\s*$/ on_quit if input =~ /^\s*(?::?exit|:quit)\s*$/ on_show_problem(problem) if input =~ /^\s*:show\s*$/ output = `#{input}` if problem.expected_result != output on_ng problem, input, output else on_ok problem, input, output end puts rescue NextProblemException break rescue SkipEvaluationException end end end |