Permalink
Browse files

added tests

  • Loading branch information...
1 parent 4c562a8 commit 3fba31a4ec82d73092c023fac828bad42401a5e2 @notro notro committed Mar 3, 2011
Showing with 142 additions and 17 deletions.
  1. +10 −0 spec/data/get_lines
  2. +3 −0 spec/data/procs.rb
  3. +129 −17 spec/lib_proc_source_spec.rb
View
@@ -0,0 +1,10 @@
+LINE1
+LINE2
+LINE3
+LINE4
+LINE5
+LINE6
+LINE7
+LINE8
+LINE9
+LINE10
View
@@ -0,0 +1,3 @@
+Proc.new { false }
+p = Proc.new { true }
+puts "hello"; p = Proc.new { true }
@@ -74,6 +74,51 @@ def lambda?
end
+describe ProcSource do
+
+ describe ".get_lines" do
+ [
+ [1, ["LINE1\n", "LINE2\n", "LINE3\n", "LINE4\n", "LINE5\n", "LINE6\n", "LINE7\n", "LINE8\n", "LINE9\n", "LINE10\n", ]],
+ [9, ["LINE9\n", "LINE10\n", ]],
+ [10, ["LINE10\n", ]],
+ [11, []],
+ [12, nil],
+ ].each do |test|
+ it "start_line=#{test[0]}" do
+ ProcSource.get_lines("spec/data/get_lines", test[0]).should == test[1]
+ end
+ end
+
+ it "default start_line" do
+ ProcSource.get_lines("spec/data/get_lines").should == ["LINE1\n", "LINE2\n", "LINE3\n", "LINE4\n", "LINE5\n", "LINE6\n", "LINE7\n", "LINE8\n", "LINE9\n", "LINE10\n", ]
+ end
+ end
+
+ describe ".find" do
+ it "should return nil if file doesn't exist" do
+ ProcSource.find("spec/data/file_dont_exist").should == nil
+ end
+
+ it "should return nil if no lines" do
+ ProcSource.find("spec/data/get_lines", 12).should == nil
+ end
+
+ it "should return only block as default" do
+ ProcSource.find("spec/data/procs.rb").should == "{ false }\n"
+ end
+
+ it "block_only=false should return the whole line" do
+ ProcSource.find("spec/data/procs.rb", 2, false).should == "p = Proc.new { true }\n"
+ end
+
+# it "block_only=false should return the whole expression" do
+# ProcSource.find("spec/data/procs.rb", 3, false).should == "p = Proc.new { true }\n"
+# end
+
+ end
+end
+
+
describe Proc do
describe "#source" do
@@ -84,26 +129,26 @@ def block_method(&block)
it "should handle a simple one line proc with brackets" do
p = Proc.new { false }
- p.source.split.join(' ').should == "{ false }"
+ p.source.gsub(/ +/, " ").strip.should == "{ false }"
end
it "should handle a simple multiline proc with brackets" do
p = Proc.new {
false
}
- p.source.split.join(' ').should == "{ false }"
+ p.source.gsub(/ +/, " ").strip.should == "{\n false\n }"
end
it "should handle a simple one line proc with do/end" do
p = Proc.new do false end
- p.source.split.join(' ').should == "do false end"
+ p.source.gsub(/ +/, " ").strip.should == "do false end"
end
it "should handle a simple multiline proc with do/end" do
p = Proc.new do
false
end
- p.source.split.join(' ').should == "do false end"
+ p.source.gsub(/ +/, " ").strip.should == "do\n false\n end"
end
it "should handle a more complicated proc with brackets" do
@@ -113,7 +158,7 @@ def block_method(&block)
print i
}
}
- p.source.split.join(' ').should == "{ print 'hello' (1..10).each { |i| print i } }"
+ p.source.gsub(/ +/, " ").strip.should == "{\n print 'hello'\n (1..10).each { |i|\n print i\n }\n }"
end
it "should handle a more complicated proc with do/end" do
@@ -123,21 +168,54 @@ def block_method(&block)
print i
}
end
- p.source.split.join(' ').should == "do print 'hello' (1..10).each { |i| print i } end"
+ p.source.gsub(/ +/, " ").strip.should == "do\n print 'hello'\n (1..10).each { |i|\n print i\n }\n end"
end
it "should handle a {} block passed to a method" do
block_method {
false
}
- @block.source.split.join(' ').should == "{ false }"
+ @block.source.gsub(/ +/, " ").strip.should == "{\n false\n }"
end
it "should handle a do/end block passed to a method" do
block_method do
false
end
- @block.source.split.join(' ').should == "do false end"
+ @block.source.gsub(/ +/, " ").strip.should == "do\n false\n end"
+ end
+
+ it "should handle hash assignment {:a=>1} in code with do/end singleline block" do
+ p = Proc.new do hash = {:a=>1} end
+ p.source.gsub(/ +/, " ").strip.should == "do hash = {:a=>1} end"
+ end
+
+ it "should handle hash assignment {:a=>1} in code with {} singleline block" do
+ p = Proc.new { hash = {:a=>1} }
+ p.source.gsub(/ +/, " ").strip.should == "{ hash = {:a=>1} }"
+ end
+
+ it "should handle hash assignment {:a=>1} in code with do/end multiline block" do
+ p = Proc.new do
+ hash = {:a=>1}
+ end
+ p.source.gsub(/ +/, " ").strip.should == "do\n hash = {:a=>1}\n end"
+ end
+
+ it "should handle hash assignment {:a=>1} in code with {} multiline block" do
+ p = Proc.new {
+ hash = {:a=>1}
+ }
+ p.source.gsub(/ +/, " ").strip.should == "{\n hash = {:a=>1}\n }"
+ end
+
+ it "should handle comments in proc" do
+ p = Proc.new {
+ # comment
+ true # comment
+ # comment
+ }
+ p.source.gsub(/ +/, " ").strip.should == "{ \n # comment\n true # comment\n # comment\n }"
end
describe "#lines" do
@@ -174,21 +252,16 @@ def block_method(&block)
end
describe "#line" do
- it "should be set after calling source" do
- before = __LINE__
+ it "should return correct line" do
+ line = __LINE__ + 1
p = Proc.new { false }
- after = __LINE__
- p.source
- line = p.line.to_i
- line.should > before
- line.should < after
+ p.line.should == line
end
end
describe "#file" do
- it "should be set after calling source" do
+ it "should return correct filename" do
p = Proc.new { false }
- p.source
p.file.should == __FILE__
end
end
@@ -277,4 +350,43 @@ def block_method(&block)
end
end
+
+ describe "JSON" do
+ it "should fail if #source is nil" do
+ p1 = eval "Proc.new { false }"
+ p1.source.should be_nil
+ expect{ p1.to_json }.to raise_error RuntimeError
+ end
+
+ it "should work with simple proc" do
+ l1 = __LINE__ + 1
+ p1 = Proc.new do false end
+ p1_j = JSON.generate p1
+ p1_j.should =~ /do false end.*#{__FILE__}.*#{l1},#{l1}/m
+ p2 = JSON.parse p1.to_json
+ p2.inspect.should =~ /#{__FILE__}:#{l1}/m
+ p1.call.should == false
+ p2.call.should == false
+ end
+
+ it "should work with more complex proc" do
+ l1 = __LINE__ + 1
+ p1 = Proc.new do |a,b,c,d|
+ sum = 0
+ (1..10).each do |i|
+ sum += a*i + b*i + c*i + d*i
+ end
+ sum
+ end
+ l2 = __LINE__ - 1
+ p1_j = JSON.generate p1
+ p1_j.should =~ /sum = 0.*#{__FILE__}.*#{l1},#{l2}/m
+ p2 = JSON.parse p1.to_json
+ p2.inspect.should =~ /#{__FILE__}:#{l1}/m
+ p2.source.should == p1.source
+ p1.call(2,3,5,7).should == 935
+ p2.call(2,3,5,7).should == 935
+ end
+
+ end
end

0 comments on commit 3fba31a

Please sign in to comment.