require File.dirname(__FILE__) + '/spec_helper.rb' require 'fault_injection.rb' $LOAD_PATH << File.dirname(__FILE__) describe "Argument Checking" do it "raises if a line number is less than 0" do Proc.new{ FaultInjection.inject({:file => 'target_00.rb',:line => -5}).should be_true }.should raise_error(ArgumentError) end it "raises if a err_class is not a subclass of Exception" do Proc.new { FaultInjection.inject({:file => 'target_00.rb',:line => 1}, 3).should be_true }.should raise_error(ArgumentError) end end describe "'File & line' fault-injection." do require 'target_00.rb' after(:each) do clear end it "works on Hash format for file & line specification" do Proc.new{ target_func_00 }.should_not raise_error FaultInjection.inject({:file => 'target_00.rb', :line => 9}).should be_true begin target_func_00 violate "target_func_00 must raise RuntimeError" rescue $!.instance_of?(RuntimeError).should be_true $!.backtrace[0].should match(/target_00\.rb/) $target_00['one'].should be_eql(1) $target_00['two'].should be_nil $target_00['three'].should be_nil end end it "works on 'file:line' format injection command." do Proc.new{ target_func_00 }.should_not raise_error FaultInjection.inject("target_00.rb:9", SecurityError, "This is a error").should be_true Proc.new{ target_func_00 }.should raise_error(SecurityError,"This is a error") end end describe "Method tracing fault-injection" do require 'target_02.rb' after(:each) do clear end it "should check arguments in Array format command." do Proc.new{ Bar.new.bar }.should_not raise_error Proc.new{ FaultInjection.inject([]) }.should raise_error(ArgumentError) Proc.new{ FaultInjection.inject([:Array]) }.should raise_error(ArgumentError) end it "should inject faults properly with Array format command" do Proc.new{ Bar.new.bar }.should_not raise_error # raise on method calling chane, "Bar#bar --> Foo#foo" FaultInjection.inject([:Bar,:bar,:Foo,:foo],RangeError).should be_true Proc.new{ Bar.new.bar }.should raise_error(RangeError) end end describe "'Line number in method' style fault injection" do require 'target_03.rb' it "should inject faults properly with Class#method:line format." do Proc.new { T3.new.foo }.should_not raise_error pending "not yet implemented" do FaultInjection.inject("T3#foo:2",IOError) Proc.new { T3.new.foo }.should raise_error(IOError) end end end describe "Specifying Exception class" do require 'target_01.rb' it "can inject fault with specified Exception class" do include FaultInjection inject({:file => 'target_01.rb', :line => 3},ArgumentError).should be_true Proc.new{ target_func_01 }.should raise_error(ArgumentError) clear end end