Permalink
Browse files

add Webconsole::Shell tests, make prompts configurable

  • Loading branch information...
1 parent 4a8eeae commit 3ab19c7b1bdc57a31dc73ed419da5f8309946703 @cldwalker cldwalker committed Aug 18, 2011
Showing with 85 additions and 6 deletions.
  1. +2 −5 lib/rack/webconsole/repl.rb
  2. +10 −1 lib/rack/webconsole/shell.rb
  3. +73 −0 spec/rack/webconsole/shell_spec.rb
@@ -68,11 +68,8 @@ def call(env)
return [status, headers, response] unless check_legitimate(req)
$sandbox ||= Sandbox.new
- result = Shell.eval_query params['query']
- response_body = MultiJson.encode(:result => result,
- :multi_line => Ripl.shell.multi_line?,
- :previous_multi_line => Ripl.shell.previous_multi_line?,
- :prompt => Ripl.shell.previous_multi_line? ? "|| " : ">> ")
+ hash = Shell.eval_query params['query']
+ response_body = MultiJson.encode(hash)
headers = {}
headers['Content-Type'] = 'application/json'
headers['Content-Length'] = response_body.bytesize.to_s
@@ -5,7 +5,13 @@ module Shell
def self.eval_query(query)
Ripl.shell.input = query
Ripl.shell.loop_once
- Ripl.shell.return_result
+ {}.tap do |hash|
+ hash[:result] = Ripl.shell.return_result
+ hash[:multi_line] = Ripl.shell.multi_line?
+ hash[:previous_multi_line] = Ripl.shell.previous_multi_line?
+ hash[:prompt] = Ripl.shell.previous_multi_line? ?
+ Ripl.config[:multi_line_prompt] : Ripl.config[:prompt]
+ end
end
# TODO: move to plugin
@@ -54,8 +60,11 @@ def before_loop() end
# Disable readline's #get_input
Ripl.config[:readline] = false
+Ripl.config[:multi_line_prompt] = '|| '
+Ripl.config[:prompt] = '>> '
# Let ripl users detect web shells
Ripl.config[:web] = true
+
Ripl::Shell.include Rack::Webconsole::Shell
# must come after Webconsole plugin
require 'ripl/multi_line'
@@ -0,0 +1,73 @@
+require 'spec_helper'
+
+module Rack
+ describe Webconsole::Shell do
+ before do
+ Ripl.instance_variable_set(:@shell, nil)
+ end
+
+ describe ".eval_query" do
+ describe "sets Ripl::Shell" do
+ before { Webconsole::Shell.eval_query('2 + 2') }
+
+ it "#input" do
+ Ripl.shell.input.must_equal '2 + 2'
+ end
+
+ it "#result" do
+ Ripl.shell.result.must_equal 4
+ end
+
+ it "#history" do
+ Ripl.shell.history.must_equal ['2 + 2']
+ end
+ end
+
+ describe "for one line input" do
+ def response
+ Webconsole::Shell.eval_query("'input'")
+ end
+
+ it ":result returns inspected result" do
+ response[:result].must_equal '"input"'
+ end
+
+ it ":prompt returns normal prompt" do
+ response[:prompt] = '>> '
+ end
+
+ it "returns false for multi_line keys" do
+ response[:multi_line].must_equal false
+ response[:previous_multi_line].must_equal false
+ end
+
+ it "with an error :result returns error raised" do
+ Webconsole::Shell.eval_query("blah")[:result].must_match /^Error:/
+ end
+ end
+
+ describe "for multi line input" do
+ def response
+ [
+ Webconsole::Shell.eval_query("[1,2,3].map do |num|"),
+ Webconsole::Shell.eval_query("num ** 2"),
+ Webconsole::Shell.eval_query("end")
+ ]
+ end
+
+ it "returns correct results" do
+ response.map {|e| e[:result] }.must_equal [nil, nil, "[1, 4, 9]"]
+ end
+
+ it "returns correct prompts" do
+ response.map {|e| e[:prompt] }.must_equal ['>> ', '|| ', '|| ']
+ end
+
+ it "returns correct multi_line keys" do
+ response.map {|e| e[:multi_line] }.must_equal [true, true, false]
+ response.map {|e| e[:previous_multi_line] }.must_equal [false, true, true]
+ end
+ end
+ end
+ end
+end

0 comments on commit 3ab19c7

Please sign in to comment.