We can't get pass green in the test-first's
red-green-refactor rhythm for this simple case.
We have to resort to a hacked, non-general double
gsub() invocation to get all the tests to pass:
class String
def transform
gsub(/(^|\W)(abs|sign)(\s*\()/,'\1\2_c\3').
gsub(/(^|\W)(abs|sign)(\s*\()/,'\1\2_c\3')
end
end
require 'test/unit'
class TestRegexExpression < Test::Unit::TestCase
def test_nested_overlapping_replacement_of_functions
assert_equal('=abs_c(sign_c(sign))',
'=abs(sign(sign))'.transform)
end
def test_only_translates_functions_of_interest
assert_equal('stabs(sign_c(var))',
'stabs(sign(var))'.transform)
end
def test_multiple_replacements_on_a_line
assert_equal('abs_c(varA)+abs_c(varB)',
'abs(varA)+abs(varB)'.transform)
end
end
Oh regex masters, please shine a light if you can,
···
--
Bil, http://fun3d.larc.nasa.gov