Notes posted by emime
RSS feed
Input strings are treated as regexp
Input strings are treated as regexp, but you can escape special regexp characters as usual:
"*test*".should match "\\*test\\*" #=> pass "*test*".should match '\*test\*' #=> pass "*test*".should match /\*test\*/ #=> pass

Remove non empty directories
To remove a non empty directory use FileUtils:
Dir.mkdir("test_dir") Dir.mkdir("test_dir/sub_dir") FileUtils.remove_dir("test_dir",true)


Eql equals ==
Use eql to compare values as you would use ==:
"test".should eql "test" #=> pass "test".should == "test" #=> pass
Do not confuse with equal which compares objects.

Test strings with eql
Equal fails when comparing two different string objects:
"test".should equal "test" #=> fail "test".should match "test" #=> pass "test".should eql "test" #=> pass
In fact:
"test".object_id.should_not eql "test".object_id #=> pass
Match fails when the string contains regex special characters not escaped:
"*test*".should match "*test*" #=> fail for invalid regex "*test*".should eql "*test*" #=> pass
In fact, match treats input as regexp:
"*test*".should match /\*test\*/ #=> pass

Get all inner texts
Extend REXML::Element so that it can get the first text and following inner texts (child texts included) of the current element as array and as string:
class REXML::Element def inner_texts REXML::XPath.match(self,'.//text()') end def inner_text REXML::XPath.match(self,'.//text()').join end end

A stub with argument and return value
it “should use a dummy method with argument and return value” do
dummy = mock("dummy").stub!(:emulate) dummy.should_receive(:emulate).with(:something).and_return("Done! sir!") dummy.emulate(:something).should == "Done! sir!" end

Input for trigonometric functions must be radians
You must use radians to have the right result. For example to compute the sin of 125 degrees use:
Math.sin(125*Math::PI/180)