Exception in -Is.StringMatching(string)- method

Bug #697919 reported by Roberto Carrillo
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
New
Undecided
Unassigned

Bug Description

This is my code:

        [Test]
        public void Test_NUnitBug()
        {
            const string ExpectedString = "NUnit is :) happy!";
            Encoding encoding = Encoding.Unicode;
            byte[] bytes = encoding.GetBytes(ExpectedString);
            string actualString = encoding.GetString(bytes);

            Console.WriteLine(actualString);
            Console.WriteLine(ExpectedString);
            Console.WriteLine(actualString.Length);
            Console.WriteLine(ExpectedString.Length);
            Assert.That(actualString, Is.StringMatching(ExpectedString));
        }

This is the exception:

NUnit is :) happy!
NUnit is :) happy!
18
18

System.ArgumentException : parsing "NUnit is :) happy!" - Too many )'s.
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, Boolean useCache)
at System.Text.RegularExpressions.Regex.IsMatch(String input, String pattern, RegexOptions options)
at NUnit.Framework.Constraints.RegexConstraint.Matches(Object actual)
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at MyCopy.Test_NUnitBug() in MyTest.cs: line 48

------
I ran NUnit using the console and ReSharper.

Tags: framework
Revision history for this message
Charlie Poole (charlie.poole) wrote :

Is.StringMatching() takes a string argument, which is to be used as a regular expression. Hence, it must be a valid regular expression. You should either use Is.EqualTo() or escape the ')' like this: @"NUnit is :\) happy!"

Revision history for this message
Charlie Poole (charlie.poole) wrote :

Although this is a user error, it's clear that Is.StringMatching isn't the best name for
this. I'll target the bug at NUnit 3.0, where we can find a better syntax.

affects: nunitv2 → nunit-3.0
Revision history for this message
Roberto Carrillo (rcarrillopadron) wrote : Re: [Bug 697919] Re: Exception in -Is.StringMatching(string)- method
Download full text (3.1 KiB)

I don't understand. I ran the "TestThatPasses()" method (below) and it passes.
It is the exactly same regular expression. Am I doing something wrong?

        [Test]
        public void Test_NUnitBug()
        {
            const string ExpectedString = "NUnit is :) happy!";
            Encoding encoding = Encoding.Unicode;
            byte[] bytes = encoding.GetBytes(ExpectedString);
            string actualString = encoding.GetString(bytes);

            Console.WriteLine(actualString);
            Console.WriteLine(ExpectedString);
            Console.WriteLine(actualString.Length);
            Console.WriteLine(ExpectedString.Length);
            Assert.That(actualString, Is.StringMatching(ExpectedString));
        }

        [Test]
        public void TestThatPasses()
        {
            const string ExpectedString = "NUnit is :) happy!";
            Console.WriteLine(ExpectedString);
            Console.WriteLine(ExpectedString.Length);
            Assert.That(ExpectedString, Is.StringMatching(ExpectedString));
        }

-Roberto

----- Mensaje original ----
De: Charlie Poole <email address hidden>
Para: <email address hidden>
Enviado: miércoles, 5 de enero, 2011 19:38:48
Asunto: [Bug 697919] Re: Exception in -Is.StringMatching(string)- method

Is.StringMatching() takes a string argument, which is to be used as a
regular expression. Hence, it must be a valid regular expression. You
should either use Is.EqualTo() or escape the ')' like this: @"NUnit is
:\) happy!"

--
You received this bug notification because you are a direct subscriber
of the bug.
https://bugs.launchpad.net/bugs/697919

Title:
  Exception in -Is.StringMatching(string)- method

Status in NUnit Test Framework:
  New

Bug description:
  This is my code:

        [Test]
        public void Test_NUnitBug()
        {
            const string ExpectedString = "NUnit is :) happy!";
            Encoding encoding = Encoding.Unicode;
            byte[] bytes = encoding.GetBytes(ExpectedString);
            string actualString = encoding.GetString(bytes);

            Console.WriteLine(actualString);
            Console.WriteLine(ExpectedString);
            Console.WriteLine(actualString.Length);
            Console.WriteLine(ExpectedString.Length);
            Assert.That(actualString, Is.StringMatching(ExpectedString));
        }

This is the exception:

NUnit is :) happy!
NUnit is :) happy!
18
18

System.ArgumentException : parsing "NUnit is :) happy!" - Too many )'s.
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions
options, Boolean useCache)
at System.Text.RegularExpressions.Regex.IsMatch(String input, String pattern,
RegexOptions options)
at NUnit.Framework.Constraints.RegexConstraint.Matches(Object actual)
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression,
String message, Object[] args)
at MyCopy.Test_NUnitBug() in MyTest.cs: line 48

------
I ran NUnit using the console and ReSharper.

To unsubscribe from this bug, go to:
https://bugs.launchpad.net/nunit-3.0/+bug/697919/...

Read more...

Revision history for this message
Charlie Poole (charlie.poole) wrote :
Download full text (3.8 KiB)

Hi Roberto,

That surprises me as well. I guess .NET is able to handle the unescaped right
parenthesis in the passing case. NUnit's RegexConstraint does no processing
of the string but just passes it on to Regex.IsMatch.

Charlie

On Wed, Jan 5, 2011 at 5:59 PM, Roberto Carrillo
<email address hidden> wrote:
> I don't understand. I ran the "TestThatPasses()" method (below) and it passes.
> It is the exactly same regular expression. Am I doing something wrong?
>
>        [Test]
>        public void Test_NUnitBug()
>        {
>            const string ExpectedString = "NUnit is :) happy!";
>            Encoding encoding = Encoding.Unicode;
>            byte[] bytes = encoding.GetBytes(ExpectedString);
>            string actualString = encoding.GetString(bytes);
>
>            Console.WriteLine(actualString);
>            Console.WriteLine(ExpectedString);
>            Console.WriteLine(actualString.Length);
>            Console.WriteLine(ExpectedString.Length);
>            Assert.That(actualString, Is.StringMatching(ExpectedString));
>        }
>
>
>        [Test]
>        public void TestThatPasses()
>        {
>            const string ExpectedString = "NUnit is :) happy!";
>            Console.WriteLine(ExpectedString);
>            Console.WriteLine(ExpectedString.Length);
>            Assert.That(ExpectedString, Is.StringMatching(ExpectedString));
>        }
>
> -Roberto
>
>
> ----- Mensaje original ----
> De: Charlie Poole <email address hidden>
> Para: <email address hidden>
> Enviado: miércoles, 5 de enero, 2011 19:38:48
> Asunto: [Bug 697919] Re: Exception in -Is.StringMatching(string)- method
>
> Is.StringMatching() takes a string argument, which is to be used as a
> regular expression. Hence, it must be a valid regular expression. You
> should either use Is.EqualTo() or escape the ')' like this: @"NUnit is
> :\) happy!"
>
> --
> You received this bug notification because you are a direct subscriber
> of the bug.
> https://bugs.launchpad.net/bugs/697919
>
> Title:
>  Exception in -Is.StringMatching(string)- method
>
> Status in NUnit Test Framework:
>  New
>
> Bug description:
>  This is my code:
>
>        [Test]
>        public void Test_NUnitBug()
>        {
>            const string ExpectedString = "NUnit is :) happy!";
>            Encoding encoding = Encoding.Unicode;
>            byte[] bytes = encoding.GetBytes(ExpectedString);
>            string actualString = encoding.GetString(bytes);
>
>            Console.WriteLine(actualString);
>            Console.WriteLine(ExpectedString);
>            Console.WriteLine(actualString.Length);
>            Console.WriteLine(ExpectedString.Length);
>            Assert.That(actualString, Is.StringMatching(ExpectedString));
>        }
>
> This is the exception:
>
> NUnit is :) happy!
> NUnit is :) happy!
> 18
> 18
>
> System.ArgumentException : parsing "NUnit is :) happy!" - Too many )'s.
> at System.Text.RegularExpressions.RegexParser.ScanRegex()
> at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
> at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions
> options, Boolean useCache)
> at System.Text.RegularExpressions.Regex.IsMatch...

Read more...

tags: added: framework
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.