Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Slicker tests. Note new test for previously uncovered RegularParser.A…
…lphaNum.
  • Loading branch information
frankshearar committed Dec 1, 2014
1 parent 8585377 commit b48d7c4
Showing 1 changed file with 52 additions and 34 deletions.
86 changes: 52 additions & 34 deletions CsharpClientTest/RegularParserTests.cs
Expand Up @@ -5,6 +5,20 @@

namespace CsharpClientTest
{
public static class It
{
public static void DoesNotParse(this RegularParser<char> p, string input)
{
Assert.False(p.Recognise(input));
}

public static void Parses(this RegularParser<char> p, string input)
{
Assert.That(p.Recognise(input));
}

}

[TestFixture]
public class RegularParserTests
{
Expand All @@ -14,15 +28,15 @@ public class RegularParserTests
public void AtLeastProcessesExcessRepetitions(string input)
{
var p = RegularParser.Token('a').AtLeast(2);
AssertParses(p, input);
p.Parses(input);
}

[TestCase("")]
[TestCase("a")]
public void AtLeastRequiresInput(string input)
{
var p = RegularParser.Token('a').AtLeast(2);
AssertDoesNotParse(p, input);
p.DoesNotParse(input);
}

[Test]
Expand All @@ -37,7 +51,7 @@ public void AtLeastRequiresNonNegativeNumber()
public void AtLeastWithZeroRepsActsLikeStar(string input)
{
var p = RegularParser.Token('a').AtLeast(0);
AssertParses(p, input);
p.Parses(input);
}


Expand All @@ -58,15 +72,15 @@ public void AtMostDoesntParseLotsOfReps(string input)
public void AtMostParsesLowerReps(string input)
{
var p = RegularParser.Token('a').AtMost(2);
AssertParses(p, input);
p.Parses(input);
}

[Test]
public void AtMostParsesNoReps()
{
var p = RegularParser.Token('a').AtMost(2);
Assert.IsEmpty(p.Match("").ToList());
AssertParses(p, "");
p.Parses("");
}

[Test]
Expand Down Expand Up @@ -144,8 +158,8 @@ public void CountDemandsMaxGreaterThanMin()
public void CountPermitsExactRepCount()
{
var p = RegularParser.Token('a').Count(3);
AssertDoesNotParse(p, "aa");
AssertParses(p, "aaa");
p.DoesNotParse("aa");
p.DoesNotParse("aaaa");
var matches = p.Match("aaaa").Select(chars => new string(chars.ToArray()));
Assert.That(matches.All(s => s == "aaa"));
}
Expand All @@ -154,61 +168,65 @@ public void CountPermitsExactRepCount()
public void CountWithMinEqualToMaxUsesMin()
{
var p = RegularParser.Token('a').Count(3,3);
AssertDoesNotParse(p, "aa");
AssertParses(p, "aaa");
AssertDoesNotParse(p, "aaaa");
p.DoesNotParse("aa");
p.Parses("aaa");
p.DoesNotParse("aaaa");
}

[Test]
public void CountReturnsRepeatedParser()
{
var p = RegularParser.Token('a').Count(3, 5);
AssertDoesNotParse(p, "aa");
p.DoesNotParse("aa");
var matches = p.Match("aaaaaa");
Assert.That(matches.All(s => 3 <= s.Count() && s.Count() <= 5));

AssertParses(p, "aaa");
AssertParses(p, "aaaa");
AssertParses(p, "aaaaa");
p.Parses("aaa");
p.Parses("aaaa");
p.Parses("aaaaa");
}

[Test]
public void AlphaAcceptsEnglishCharacters()
public void ClassDefinesCharacterClass()
{
var p = RegularParser.Alpha.Count(3);
AssertParses(p, "abc");
AssertParses(p, "xyz");
var p = RegularParser.Class("abc").Star();
p.Parses("aaa");
p.Parses("cba");

AssertDoesNotParse(p, "123");
p.DoesNotParse("xyz");
}
}

[TestFixture]
public class CharRegularParserTests
{
[Test]
public void ClassDefinesCharacterClass()
public void AlphaAcceptsEnglishCharacters()
{
var p = RegularParser.Class("abc").Star();
AssertParses(p, "aaa");
AssertParses(p, "cba");
var p = RegularParser.Alpha.Count(3);
p.Parses("abc");
p.Parses("xyz");

AssertDoesNotParse(p, "xyz");
p.DoesNotParse("123");
}

[Test]
public void NumAcceptsDigits()
public void AlphaNumAcceptsEnglishCharactersAndDigits()
{
var p = RegularParser.Num.Count(10);
AssertParses(p, "0123456789");
var p = RegularParser.AlphaNum.Count(3);
p.Parses("ab2");
p.Parses("x0z");

AssertDoesNotParse(p, "abcdefghij");
p.DoesNotParse("+++");
}

private void AssertDoesNotParse(RegularParser<char> p, string input)
[Test]
public void NumAcceptsDigits()
{
Assert.False(p.Recognise(input));
}
var p = RegularParser.Num.Count(10);
p.Parses("0123456789");

private void AssertParses(RegularParser<char> p, string input)
{
Assert.That(p.Recognise(input));
p.DoesNotParse("abcdefghij");
}
}
}

0 comments on commit b48d7c4

Please sign in to comment.