Skip to content
Browse files

fix inspecting sexps when manual testing

  • Loading branch information...
1 parent cf12fb8 commit d51ba1ae57d67612e08fc4bca378533eef6b8870 @mislav committed Aug 22, 2010
Showing with 33 additions and 9 deletions.
  1. +4 −4 app.rb
  2. +29 −5 code.rb
View
8 app.rb
@@ -84,14 +84,14 @@ def sass_with_caching(name)
end
get '/f/:name' do
- file = File.open("./fixtures/#{params[:name]}.rb")
- rocco(file.path) { ExplainRuby::Code.new(file.read, file.path).to_s }
+ code = ExplainRuby::Code.from_test_fixture(params[:name])
+ rocco(code.url) { code.to_s }
end
get '/f/:name/sexp' do
content_type 'text/plain'
- file = File.open("./fixtures/#{params[:name]}.rb")
- ruby2sexp(file).pretty_inspect
+ code = ExplainRuby::Code.from_test_fixture(params[:name])
+ code.pretty_inspect
end
get '/chunky.css' do
View
34 code.rb
@@ -81,6 +81,7 @@ def initialize(code, url = nil)
@url = url.nil?? nil : url.to_s
@reconstructed_code = nil
@explained_code = nil
+ @sexp = nil
end
def to_s
@@ -91,11 +92,21 @@ def process
insert_explanations reconstruct_code
end
+ def parse
+ @sexp ||= self.class.ruby2sexp(@code, @url)
+ end
+
+ # delegate pretty printing to sexp
+ def pretty_print(io)
+ parse.pretty_print(io)
+ end
+
def reconstruct_code
- @reconstructed_code ||= ruby2ruby(@code, @url)
+ @reconstructed_code ||= self.class.ruby2ruby(parse, @url)
end
EXPLANATIONS_PATH = File.expand_path('../explanations', __FILE__)
+ FIXTURES_PATH = File.expand_path('../fixtures', __FILE__)
def self.get_explanation(name)
begin
@@ -112,22 +123,30 @@ def insert_explanations(code)
}
end
- def ruby2ruby(io, filename = nil)
- filename = io.path if filename.nil? and io.respond_to? :path
+ def self.ruby2ruby(input, filename = nil)
+ filename = input.path if filename.nil? and input.respond_to? :path
+ input = ruby2sexp(input, filename) unless ::Sexp === input
ruby2ruby = Processor.new
- ruby2ruby.process ruby2sexp(io, filename)
+ ruby2ruby.process input
end
- def ruby2sexp(io, filename = io.path)
+ def self.ruby2sexp(io, filename = io.path)
code = io.respond_to?(:read) ? io.read : io.to_s
parser = ::RubyParser.new
parser.process(code, filename)
end
+
+ def self.from_test_fixture(name)
+ File.open(FIXTURES_PATH + "/#{name}.rb") do |file|
+ new(file.read, file.path)
+ end
+ end
end
end
if $0 == __FILE__
require 'spec/autorun'
+ require 'pp'
body_code, body_no_code, body_gist, body_gist_no_ruby = DATA.read.split('===')
@@ -190,6 +209,11 @@ def ruby2sexp(io, filename = io.path)
code.reconstruct_code.should == ">> method\ndef foo\n 1\nend\n\ndef bar\n 2\nend\n"
end
end
+
+ it "delegates pretty printing to sexp" do
+ code = described_class.new("class Klass; end")
+ code.pretty_inspect.should == "s(:class, :Klass, nil, s(:scope))\n"
+ end
end
end

0 comments on commit d51ba1a

Please sign in to comment.
Something went wrong with that request. Please try again.