Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 24 additions & 47 deletions src/System.Text.RegularExpressions/tests/CaptureCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,50 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System;
using System.Text.RegularExpressions;
using System.Collections;
using Xunit;

namespace Test
{
/// <summary>
/// Tests the CaptureCollection class.
/// </summary>
public class CaptureCollectionTests
{
[Fact]
public static void CaptureCollection_GetEnumeratorTest_Negative()
public static void GetEnumerator()
{
Regex rgx1 = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)");
String strInput = "aaabbccccccccccaaaabc";
Match mtch1 = rgx1.Match(strInput);
CaptureCollection captrc1 = mtch1.Captures;

IEnumerator enmtr1 = captrc1.GetEnumerator();

Capture currentCapture;

Assert.Throws<InvalidOperationException>(() => currentCapture = (Capture)enmtr1.Current);
Regex regex = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)");
Match match = regex.Match("aaabbccccccccccaaaabc");


for (int i = 0; i < captrc1.Count; i++)
CaptureCollection captures = match.Captures;
IEnumerator enumerator = captures.GetEnumerator();
for (int i = 0; i < 2; i++)
{
enmtr1.MoveNext();
int counter = 0;
while (enumerator.MoveNext())
{
Assert.Equal(captures[counter], enumerator.Current);
counter++;
}
Assert.Equal(captures.Count, counter);
enumerator.Reset();
}

enmtr1.MoveNext();

Assert.Throws<InvalidOperationException>(() => currentCapture = (Capture)enmtr1.Current);
enmtr1.Reset();

Assert.Throws<InvalidOperationException>(() => currentCapture = (Capture)enmtr1.Current);
}

[Fact]
public static void CaptureCollection_GetEnumeratorTest()
public static void GetEnumeratorTest_Invalid()
{
Regex rgx1 = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)");
String strInput = "aaabbccccccccccaaaabc";
Match mtch1 = rgx1.Match(strInput);
CaptureCollection captrc1 = mtch1.Captures;
Regex regex = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)");
Match match = regex.Match("aaabbccccccccccaaaabc");
IEnumerator enumerator = match.Captures.GetEnumerator();

IEnumerator enmtr1 = captrc1.GetEnumerator();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);

for (int i = 0; i < captrc1.Count; i++)
{
enmtr1.MoveNext();

Assert.Equal(enmtr1.Current, captrc1[i]);
}

Assert.False(enmtr1.MoveNext(), "Err_5! enmtr1.MoveNext returned true");
while (enumerator.MoveNext()) ;
Assert.Throws<InvalidOperationException>(() => enumerator.Current);

enmtr1.Reset();

for (int i = 0; i < captrc1.Count; i++)
{
enmtr1.MoveNext();

Assert.Equal(enmtr1.Current, captrc1[i]);
}
enumerator.Reset();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
}
}
}
61 changes: 28 additions & 33 deletions src/System.Text.RegularExpressions/tests/CaptureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,57 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System;
using System.Text.RegularExpressions;
using System.Collections;
using Xunit;

namespace Test
{
/// <summary>
/// Tests the Capture class.
/// </summary>
public class CaptureTests
{
[Fact]
public static void Capture_Test()
{
Match match = Regex.Match("adfadsfSUCCESSadsfadsf", @".*\B(SUCCESS)\B.*");
Int32[] iMatch1 = { 0, 22 };
String strMatch1 = "adfadsfSUCCESSadsfadsf";
int[] iMatch1 = { 0, 22 };
string strMatch1 = "adfadsfSUCCESSadsfadsf";

String[] strGroup1 = { "adfadsfSUCCESSadsfadsf", "SUCCESS" };
Int32[] iGroup1 = { 7, 7 };
String[] strGrpCap1 = { "SUCCESS" };
string[] strGroup1 = { "adfadsfSUCCESSadsfadsf", "SUCCESS" };
int[] iGroup1 = { 7, 7 };
string[] strGrpCap1 = { "SUCCESS" };

Assert.True(match.Success, "Fail Do not found a match");
Assert.True(match.Success);

Assert.True(match.Value.Equals(strMatch1), "Expected to return TRUE");
Assert.Equal(match.Index, iMatch1[0]);
Assert.Equal(match.Length, iMatch1[1]);
Assert.Equal(match.Captures.Count, 1);
Assert.Equal(strMatch1, match.Value);
Assert.Equal(iMatch1[0], match.Index);
Assert.Equal(iMatch1[1], match.Length);
Assert.Equal(1, match.Captures.Count);

Assert.True(match.Captures[0].Value.Equals(strMatch1), "Expected to return TRUE");
Assert.Equal(match.Captures[0].Index, iMatch1[0]);
Assert.Equal(match.Captures[0].Length, iMatch1[1]);
Assert.Equal(strMatch1, match.Captures[0].Value);
Assert.Equal(iMatch1[0], match.Captures[0].Index);
Assert.Equal(iMatch1[1], match.Captures[0].Length);

Assert.Equal(match.Groups.Count, 2);
Assert.Equal(2, match.Groups.Count);

//Group 0 always is the Match
Assert.True(match.Groups[0].Value.Equals(strMatch1), "Expected to return TRUE");
Assert.Equal(match.Groups[0].Index, iMatch1[0]);
Assert.Equal(match.Groups[0].Length, iMatch1[1]);
Assert.Equal(match.Groups[0].Captures.Count, 1);
// Group 0 always is the Match
Assert.Equal(strMatch1, match.Groups[0].Value);
Assert.Equal(iMatch1[0], match.Groups[0].Index);
Assert.Equal(iMatch1[1], match.Groups[0].Length);
Assert.Equal(1, match.Groups[0].Captures.Count);


//Group 0's Capture is always the Match
Assert.True(match.Groups[0].Captures[0].Value.Equals(strMatch1), "Expected to return TRUE");
Assert.Equal(match.Groups[0].Captures[0].Index, iMatch1[0]);
Assert.Equal(match.Groups[0].Captures[0].Length, iMatch1[1]);
// Group 0's Capture is always the Match
Assert.Equal(strMatch1, match.Groups[0].Captures[0].Value);
Assert.Equal(iMatch1[0], match.Groups[0].Captures[0].Index);
Assert.Equal(iMatch1[1], match.Groups[0].Captures[0].Length);

for (int i = 1; i < match.Groups.Count; i++)
{
Assert.True(match.Groups[i].Value.Equals(strGroup1[i]), "Expected to return TRUE");
Assert.Equal(match.Groups[i].Index, iGroup1[0]);
Assert.Equal(match.Groups[i].Length, iGroup1[0]);
Assert.Equal(match.Groups[i].Captures.Count, 1);
Assert.Equal(strGroup1[i], match.Groups[i].Value);
Assert.Equal(iGroup1[0], match.Groups[i].Index);
Assert.Equal(iGroup1[0], match.Groups[i].Length);
Assert.Equal(1, match.Groups[i].Captures.Count);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,13 @@

public class CharacterClassSubtraction
{
// This tests CharacterClassSubtraction by specifying pattern, input and expected groups
[Fact]
public static void CharacterClassSubtractionTestCase()
{
//////////// Global Variables used for all tests
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;

try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////

for (int i = 0; i < s_regexTests.Length; i++)
{
iCountTestcases++;
if (!s_regexTests[i].Run())
{
Console.WriteLine("Err_79872asnko! Test {0} FAILED Pattern={1}, Input={2}\n", i, s_regexTests[i].Pattern, s_regexTests[i].Input);
iCountErrors++;
}
}
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
for (int i = 0; i < s_regexTests.Length; i++)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
Assert.True(s_regexTests[i].Run());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this RegexTestClass thing can be unraveled in the future, but for now, should this be a theory? e.g.

[Theory]
[MemberData(nameof(CharacterClassSubtractionTestCases))]
public void CharacterClassSubtraction(RegexTestCase case)
{
    case.Run();
}

?

//// Finish Diagnostics
Assert.Equal(0, iCountErrors);
}

private static RegexTestCase[] s_regexTests = new RegexTestCase[] {
Expand Down
49 changes: 4 additions & 45 deletions src/System.Text.RegularExpressions/tests/EscapeUnescapeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,12 @@

public class EscapeUnescapeTests
{
/*
Tested Methods:

public static String Escape(String str); round tripping "#$^*+(){}<>\\|. "

public static String Unescape(string str);

*/

[Fact]
public static void EscapeUnescape()
{
//////////// Global Variables used for all tests
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
String s1;
String s2;
String s3;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
// [] public static String Escape(String str); round tripping "#$^*+(){}<>\\|. "
// public static String Unescape(string str);
//-----------------------------------------------------------------
strLoc = "Loc_498yg";
iCountTestcases++;
s1 = "#$^*+(){}<>\\|. ";
s2 = Regex.Escape(s1);
s3 = Regex.Unescape(s2);
if (!s1.Equals(s3))
{
iCountErrors++;
Console.WriteLine("Err_234fsadg! doesnot match");
}
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
}

//// Finish Diagnostics
Assert.Equal(0, iCountErrors);
string source = "#$^*+(){}<>\\|. ";
string escaped = Regex.Escape(source);
string unescaped = Regex.Unescape(escaped);
Assert.Equal(source, unescaped);
}
}
Loading