Skip to content

Commit

Permalink
Added use of the Characters enum as a parameter to the Character() fu…
Browse files Browse the repository at this point in the history
…nction as per Issue #2
  • Loading branch information
Timothep committed Jul 1, 2015
1 parent 37d7822 commit 89c76d3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
45 changes: 45 additions & 0 deletions MagicExpression.Test/GithubIssuesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MagicExpression.Test
{
[TestClass]
public class GithubIssuesTests
{
/// <summary>
/// Test showing the use of Character(Characters.Numerals)
/// </summary>
[TestMethod]
public void TestIssue2_CharacterEnumWithoutCharacterIn()
{
var ipAdress = Magex.New().Alternative(
Magex.New()
.Character('2')
.CharacterIn('0', '-', '4')

This comment has been minimized.

Copy link
@ghusse

ghusse Jul 1, 2015

Owner

Not fan of this syntax, because you have to know regexp syntax to use it: not very self-explanatory. We probably need to add a way to do this without needing to bend the model like that.

This comment has been minimized.

Copy link
@ghusse

ghusse Jul 1, 2015

Owner

And in this case, the character '-' should probably be escaped.

This comment has been minimized.

Copy link
@Timothep

Timothep Jul 1, 2015

Author Collaborator

Am I understanding you well? You mean that the fact that CharacterIn() produces a class whereas Character() does not, is not intuitive enough?

.Character(Characters.Numeral),

This comment has been minimized.

Copy link
@ghusse

ghusse Jul 1, 2015

Owner

Actually, you are testing only this line. Why do you create such complicated test?

This comment has been minimized.

Copy link
@Timothep

Timothep Jul 1, 2015

Author Collaborator

I just wanted to relate to the Github Issue #2 and reuse exactly the example given in the issue for that. But fair enough that can be reduced to this line only. In fact it would probably be better not to run into issues down the line if we modify the syntax, you're right.

This comment has been minimized.

Copy link
@Timothep

Timothep Jul 7, 2015

Author Collaborator
Magex.New()
.String("25")
.CharacterIn('0', '-', '5'),
Magex.New()
.CharacterIn('0', '1').Repeat.AtMostOnce()
.Character(Characters.Numeral)
.Character(Characters.Numeral).Repeat.AtMostOnce());

var expression = Magex.New().CaptureAs("First", ipAdress)
.Character('.')
.CaptureAs("Second", ipAdress)
.Character('.')
.CaptureAs("Third", ipAdress)
.Character('.')
.CaptureAs("Fourth", ipAdress);

Assert.AreEqual(
@"(?<First>(?:2[0-4]\\d|25[0-5]|[01]?\\d\\d?))\.(?<Second>(?:2[0-4]\\d|25[0-5]|[01]?\\d\\d?))\.(?<Third>(?:2[0-4]\\d|25[0-5]|[01]?\\d\\d?))\.(?<Fourth>(?:2[0-4]\\d|25[0-5]|[01]?\\d\\d?))"
, expression.Expression, expression.Expression);
}

}
}
1 change: 1 addition & 0 deletions MagicExpression.Test/MagicExpression.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</Choose>
<ItemGroup>
<Compile Include="BackReferenceTest.cs" />
<Compile Include="GithubIssuesTests.cs" />
<Compile Include="MagexBuilderTest.cs" />
<Compile Include="RepetitionTest.cs" />
<Compile Include="SyntaxTest.cs" />
Expand Down
1 change: 1 addition & 0 deletions MagicExpression/IMagex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public interface IMagex : IExpressionElement

IMagex String(string val);
IRepeatable Character(char theChar);
IRepeatable Character(Characters ensemble);

IRepeatable Alternative(params IExpressionElement[] alternatives);
IRepeatable Alternative(params Action<IMagex>[] alternatives);
Expand Down
11 changes: 11 additions & 0 deletions MagicExpression/Magex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ public IRepeatable Character(char theChar)
return this;
}

/// <summary>
/// Adds a specific character set
/// </summary>
/// <param name="theChar">A Characters enum.</param>
/// <returns>This, as a repeatable object.</returns>
public IRepeatable Character(Characters charEnsemble)
{
this.expression.Add(new StringElement(RegexCharacters.Get(charEnsemble)));

This comment has been minimized.

Copy link
@ghusse

ghusse Jul 1, 2015

Owner

+1

This comment has been minimized.

Copy link
@Timothep

Timothep Jul 1, 2015

Author Collaborator

Hu?

return this;
}

/// <summary>
/// Adds a line start/chain start (depending on options)
/// </summary>
Expand Down

0 comments on commit 89c76d3

Please sign in to comment.