One of the tasks in my current project is writing acceptance tests for an iOS app using Cucumber and Calabash. It’s quite cool to be able to mix Ruby and Objective-C. Nope, on Swift yet, enterprise client… Who knows when they’ll start using it.

Cumubers steps can be written using Regular Expressions, which is quite handy, and also gives me a way to hone my RegEx skills, which are quite limited at the moment.

Here’s a closer look at some of the matchers used in the predefined steps from calabash_steps.rb

([^\"\*)

Then /^I toggle the "([^\"]*)" switch$/ do |name|
  ...
end

^\" will match any character that’s not ", [ ]* will match any number of characters that are not ", and finally ( ) is simply a capture group. So this RegEx will match everything untill it finds a ", and because of the capture group the result will go into the name variable.

(\d+)

Then /^I press button number (\d+)$/ do |index|
  ...
end

\d stands for any digit, and + is the 1 or more quantifier. This expression will match any uninterrupted sequence of digits.

(foo|bar)

Then /^I pinch to zoom (in|out)$/ do |in_out|
  ...
end

The | symbol is the alteration, it acts like a logical OR, matching the expression at its left or at right.

(?:foo|bar)

Then /^I (?:press|touch) button number (\d+)$/ do |index|
  ...
end

The ?: at the start makes this group non capturing, which means that the match will not be retained. In fact you can see that the only variable for the step in the example is index.

(?:foo)?

Then /^I (?:should)? see text containing "([^\"]*)"$/ do |text|
  ...
end

The ? after the capture group is the optional quantifier, it matches 0 or 1 instances of it’s token. This provides flexibility and code reuse in the syntax of the steps, in fact both “I see…” and “I should see…” can be used when writing the feature.