Including double quotes in the string parameters

2,479 views
Skip to first unread message

Venugopal Shenoy

unread,
Aug 24, 2010, 8:06:15 AM8/24/10
to Cukes
Hi

I have the following @And method in the java file.
@And ("the strings \"([^\"]*)\" and \"([^\"]*)\" are EQUAL")
public void stringAssertion(String s1, String s2) {
Assert.assertEquals("The string is " + s1 + "\nand " + s2, s1, s2);
}

The .feature file contains the following:

Feature: Ftr1
Scenario: Scn1
And the strings "abcd" and "abcd" are EQUAL


The above feature file runs perfectly fine with passing the Scenario:
Scn1

However if the strings contains the double quote character ("), not
sure how to provide the regular expression in the java file. Also
how the double quote (") is to be escaped in the feature file.

Any help on this ASAP, is needed.

Richard Lawrence

unread,
Aug 24, 2010, 5:38:14 PM8/24/10
to cu...@googlegroups.com
A couple possibilities:
1. Don't use double quotes in a double quoted string; it doesn't
really make sense. Use single quotes or some other delimiter instead.
2. Change [^\"]* to .*. The * is greedy, so it will skip over the
inner quotes (I think—I haven't tested this).

Number 1 really is the better option, but number 2 should work.

Richard

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
>
>

Jonas Nicklas

unread,
Aug 24, 2010, 7:25:45 PM8/24/10
to cu...@googlegroups.com
If you're on Ruby 1.9 you could use lookbehind to construct a regexp
that will allow you to escape your quotes with a backslash. Something
like this:

/^foo "((?:[^\"]|(?<=\\)\")*)" bar$/

will match

foo "something" bar
foo "I said \"hello\" to him" bar

If that looks ridiculous, it's because it is. I said you *can* do
this, I didn't say you should ;) Not only are the regexen really hard
to read, but the features don't read good either.

/Jonas

Venugopal Shenoy

unread,
Aug 25, 2010, 2:34:23 AM8/25/10
to Cukes
Hi Jonas

I made the following change in the java file
@And ("the strings \"((?:[^\"]|(?<=\\)\")*)\" and \"((?:[^\"]|(?<=\\)
\")*)\" are EQUAL")
public void str(String s1, String s2) {
Assert.assertEquals("The string is " + s1 + "\nand " + s2, s1, s2);
}

In the .feature file:
Feature: Ftr1
Scenario: Scn1
Given the strings "abcd" and "abcd" are EQUAL

the result was
[java] java.util.regex.PatternSyntaxException: Unclosed group
near index 69
[java] the strings "((?:[^"]|(?<=\)")*)" and "((?:[^"]|(?<=
\)")*)" are EQUAL

[java]
^ (NativeException)


I modified the java file again, as given below:
@And ("the strings \"((?:[^\"]|(?<=\\\\)\")*)\" and \"((?:[^\"]|(?<=\\
\\)\")*)\" are EQUAL")
public void str(String s1, String s2) {
Assert.assertEquals("The string is " + s1 + "\nand " + s2, s1, s2);
}

For the feature file
Feature: Ftr1
Scenario: Scn1
Given the strings "a\"bc\"d" and "a\"bc\"d" are EQUAL

The result was:
The test was passing:
Given the strings "a\"bc\"d" and "a\"bc\"d" are EQUAL
Account.str(String,String)

{Though I was expecting the strings to be passed as a"bc"d, without
the escape {backslash} character, in the string arguments}



For the feature file
Feature: Ftr1
Scenario: Scn1
Given the strings "a\"bc\"d" and "a\"boc\"d" are EQUAL.

The result was:
The test was failing:
Given the strings "a\"bc\"d" and "a\"boc\"d" are
EQUAL Account.str(String,String)
org.junit.ComparisonFailure: The string is a\"bc\"d
and a\"boc\"d expected:<a\"b[]c\"d> but was:<a\"b[o]c\"d>
{Here again I was expecting the strings to passed without the escape
(backslash) character in the string arguments}



So in essence, now the double quotes {which are escaped using
backslash charcter are parsed correctly} and this resolves the first
part of the problem.
The solution will be complete if the second part is also addressed;
i.e., the escape (backslash) character is not part of the string
arguments / cucumber results}

Appreciate, if you could provide this resolution also.

I am using jruby version
jruby 1.5.2 (ruby 1.8.7 patchlevel 249)

and Java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

Regards
VENU
> >> For more options, visit this group athttp://groups.google.com/group/cukes?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Cukes" group.
> > To post to this group, send email to cu...@googlegroups.com.
> > To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/cukes?hl=en.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages