Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby rspec and strings comparison

Tags:

ruby

rspec

I'm not a ruby expert and may be this will seem a silly question...but I'm too courious about an oddity (I think) I've found in RSpec matcher called match.

You know match takes in input a string or a regex. Example:

"test".should match "test" #=> will pass
"test".should match /test/ #=> will pass

The strange begins when you insert special regex characters in the input string:

"*test*".should match "*test*" #=> will fail throwing a regex exception

This means (I thought) that input strings are interpreted as regex, then I should escape special regex characters to make it works:

"*test*".should match "\*test\*" #=> will fail with same exception
"*test*".should match /\*test\*/ #=> will pass

From this basic test, I understand that match treats input strings as regular expressions but it does not allow you to escape special regex characters.

Am I true? Is not this a singular behavior? I mean, it's a string or a regex!


EDIT AFTER ANSWER:

Following DigitalRoss (right) answer the following tests passed:

"*test*".should match "\\*test\\*" #=> pass
"*test*".should match '\*test\*' #=> pass
"*test*".should match /\*test\*/ #=> pass
like image 269
Emiliano Poggi Avatar asked Apr 24 '11 17:04

Emiliano Poggi


People also ask

What is RSpec in Ruby?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What is describe in RSpec?

The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.

What is expect in RSpec?

RSpec::Expectations provides a simple, readable API to express the expected outcomes in a code example. To express an expected outcome, wrap an object or block in expect , call to or to_not (aliased as not_to ) and pass it a matcher object: expect(order. total).

What is before in RSpec?

before(:all) runs the block one time before all of the examples are run. before(:all) sets the instance variables @admission, @project, @creative, @contest_entry one time before all of the it blocks are run. However, :before(:each) resets the instance variables in the before block every time an it block is run.


1 Answers

What you are seeing is the different interpretation of backslash-escaped characters in String vs Regexp. In a soft (") quoted string, \* becomes a *, but /\*/ is really a backslash followed by a star.

If you use hard quotes (') for the String objects or double the backslash characters (only for the Strings, though) then your tests should produce the same results.

like image 144
DigitalRoss Avatar answered Oct 14 '22 03:10

DigitalRoss