Permalink
Browse files

Simplify token test assertion helpers.

  • Loading branch information...
plioi committed Apr 1, 2012
1 parent bf54d88 commit b53bf19adccc554aaa280ffec1e20372f4121fd5
@@ -17,7 +17,7 @@ public void CanDefineMutuallyRecursiveRules()
alpha.Rule = from a in Token("A") select a.Literal;
parenthesizedExpresion.Rule = Between(Token("("), expression, Token(")"));
- expression.Parses(tokens).IntoValue("A");
+ expression.Parses(tokens).WithValue("A");
}
[Fact]
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using Should;
using Xunit;
@@ -36,6 +37,16 @@ public GrammarTests()
COMMA = Token(",");
}
+ private static Action<Token> Literal(string expectedLiteral)
+ {
+ return t => t.Literal.ShouldEqual(expectedLiteral);
+ }
+
+ private static Action<IEnumerable<Token>> Literals(params string[] expectedLiterals)
+ {
+ return tokens => tokens.ShouldList(expectedLiterals.Select(Literal).ToArray());
+ }
+
[Fact]
public void CanFailWithoutConsumingInput()
{
@@ -45,25 +56,25 @@ public void CanFailWithoutConsumingInput()
[Fact]
public void CanDetectTheEndOfInputWithoutAdvancing()
{
- EndOfInput.Parses(Tokenize("")).IntoToken("");
+ EndOfInput.Parses(Tokenize("")).WithValue(Literal(""));
EndOfInput.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): end of input expected");
}
[Fact]
public void CanDemandThatAGivenKindOfTokenAppearsNext()
{
- Token(SampleLexer.Letter).Parses(Tokenize("A")).IntoToken("A");
+ Token(SampleLexer.Letter).Parses(Tokenize("A")).WithValue(Literal("A"));
Token(SampleLexer.Letter).FailsToParse(Tokenize("0")).LeavingUnparsedTokens("0").WithMessage("(1, 1): Letter expected");
Token(SampleLexer.Digit).FailsToParse(Tokenize("A")).LeavingUnparsedTokens("A").WithMessage("(1, 1): Digit expected");
- Token(SampleLexer.Digit).Parses(Tokenize("0")).IntoToken("0");
+ Token(SampleLexer.Digit).Parses(Tokenize("0")).WithValue(Literal("0"));
}
[Fact]
public void CanDemandThatAGivenTokenLiteralAppearsNext()
{
- Token("A").Parses(Tokenize("A")).IntoToken("A");
- Token("A").PartiallyParses(Tokenize("A!")).LeavingUnparsedTokens("!").IntoToken("A");
+ Token("A").Parses(Tokenize("A")).WithValue(Literal("A"));
+ Token("A").PartiallyParses(Tokenize("A!")).LeavingUnparsedTokens("!").WithValue(Literal("A"));
Token("A").FailsToParse(Tokenize("B")).LeavingUnparsedTokens("B").WithMessage("(1, 1): A expected");
}
@@ -72,10 +83,19 @@ public void ApplyingARuleZeroOrMoreTimes()
{
var parser = ZeroOrMore(AB);
- parser.Parses(Tokenize("")).IntoTokens();
- parser.PartiallyParses(Tokenize("AB!")).LeavingUnparsedTokens("!").IntoTokens("AB");
- parser.PartiallyParses(Tokenize("ABAB!")).LeavingUnparsedTokens("!").IntoTokens("AB", "AB");
- parser.FailsToParse(Tokenize("ABABA!")).LeavingUnparsedTokens("!").WithMessage("(1, 6): B expected");
+ parser.Parses(Tokenize("")).Value.ShouldBeEmpty();
+
+ parser.PartiallyParses(Tokenize("AB!"))
+ .LeavingUnparsedTokens("!")
+ .WithValue(Literals("AB"));
+
+ parser.PartiallyParses(Tokenize("ABAB!"))
+ .LeavingUnparsedTokens("!")
+ .WithValue(Literals("AB", "AB"));
+
+ parser.FailsToParse(Tokenize("ABABA!"))
+ .LeavingUnparsedTokens("!")
+ .WithMessage("(1, 6): B expected");
Parser<Token> succeedWithoutConsuming = new LambdaParser<Token>(tokens => new Parsed<Token>(null, tokens));
Action infiniteLoop = () => ZeroOrMore(succeedWithoutConsuming).Parse(new TokenStream(Tokenize("")));
@@ -88,9 +108,18 @@ public void ApplyingARuleOneOrMoreTimes()
var parser = OneOrMore(AB);
parser.FailsToParse(Tokenize("")).AtEndOfInput().WithMessage("(1, 1): A expected");
- parser.PartiallyParses(Tokenize("AB!")).LeavingUnparsedTokens("!").IntoTokens("AB");
- parser.PartiallyParses(Tokenize("ABAB!")).LeavingUnparsedTokens("!").IntoTokens("AB", "AB");
- parser.FailsToParse(Tokenize("ABABA!")).LeavingUnparsedTokens("!").WithMessage("(1, 6): B expected");
+
+ parser.PartiallyParses(Tokenize("AB!"))
+ .LeavingUnparsedTokens("!")
+ .WithValue(Literals("AB"));
+
+ parser.PartiallyParses(Tokenize("ABAB!"))
+ .LeavingUnparsedTokens("!")
+ .WithValue(Literals("AB", "AB"));
+
+ parser.FailsToParse(Tokenize("ABABA!"))
+ .LeavingUnparsedTokens("!")
+ .WithMessage("(1, 6): B expected");
Parser<Token> succeedWithoutConsuming = new LambdaParser<Token>(tokens => new Parsed<Token>(null, tokens));
Action infiniteLoop = () => OneOrMore(succeedWithoutConsuming).Parse(new TokenStream(Tokenize("")));
@@ -102,10 +131,10 @@ public void ApplyingARuleZeroOrMoreTimesInterspersedByASeparatorRule()
{
var parser = ZeroOrMore(AB, COMMA);
- parser.Parses(Tokenize("")).IntoTokens();
- parser.Parses(Tokenize("AB")).IntoTokens("AB");
- parser.Parses(Tokenize("AB,AB")).IntoTokens("AB", "AB");
- parser.Parses(Tokenize("AB,AB,AB")).IntoTokens("AB", "AB", "AB");
+ parser.Parses(Tokenize("")).Value.ShouldBeEmpty();
+ parser.Parses(Tokenize("AB")).WithValue(Literals("AB"));
+ parser.Parses(Tokenize("AB,AB")).WithValue(Literals("AB", "AB"));
+ parser.Parses(Tokenize("AB,AB,AB")).WithValue(Literals("AB", "AB", "AB"));
parser.FailsToParse(Tokenize("AB,")).AtEndOfInput().WithMessage("(1, 4): A expected");
parser.FailsToParse(Tokenize("AB,A")).AtEndOfInput().WithMessage("(1, 5): B expected");
}
@@ -116,9 +145,9 @@ public void ApplyingARuleOneOrMoreTimesInterspersedByASeparatorRule()
var parser = OneOrMore(AB, COMMA);
parser.FailsToParse(Tokenize("")).AtEndOfInput().WithMessage("(1, 1): A expected");
- parser.Parses(Tokenize("AB")).IntoTokens("AB");
- parser.Parses(Tokenize("AB,AB")).IntoTokens("AB", "AB");
- parser.Parses(Tokenize("AB,AB,AB")).IntoTokens("AB", "AB", "AB");
+ parser.Parses(Tokenize("AB")).WithValue(Literals("AB"));
+ parser.Parses(Tokenize("AB,AB")).WithValue(Literals("AB", "AB"));
+ parser.Parses(Tokenize("AB,AB,AB")).WithValue(Literals("AB", "AB", "AB"));
parser.FailsToParse(Tokenize("AB,")).AtEndOfInput().WithMessage("(1, 4): A expected");
parser.FailsToParse(Tokenize("AB,A")).AtEndOfInput().WithMessage("(1, 5): B expected");
}
@@ -134,22 +163,22 @@ public void ApplyingARuleBetweenTwoOtherRules()
parser.FailsToParse(Tokenize("AA")).LeavingUnparsedTokens("A").WithMessage("(1, 2): B expected");
parser.FailsToParse(Tokenize("AB")).AtEndOfInput().WithMessage("(1, 3): A expected");
parser.FailsToParse(Tokenize("ABB")).LeavingUnparsedTokens("B").WithMessage("(1, 3): A expected");
- parser.Parses(Tokenize("ABA")).IntoToken("B");
+ parser.Parses(Tokenize("ABA")).WithValue(Literal("B"));
}
[Fact]
public void ParsingAnOptionalRuleZeroOrOneTimes()
{
- Optional(AB).PartiallyParses(Tokenize("AB.")).LeavingUnparsedTokens(".").IntoToken("AB");
- Optional(AB).PartiallyParses(Tokenize(".")).LeavingUnparsedTokens(".").IntoValue(token => token.ShouldBeNull());
+ Optional(AB).PartiallyParses(Tokenize("AB.")).LeavingUnparsedTokens(".").WithValue(Literal("AB"));
+ Optional(AB).PartiallyParses(Tokenize(".")).LeavingUnparsedTokens(".").WithValue(token => token.ShouldBeNull());
Optional(AB).FailsToParse(Tokenize("AC.")).LeavingUnparsedTokens("C", ".").WithMessage("(1, 2): B expected");
}
[Fact]
public void AttemptingToParseRuleButBacktrackingUponFailure()
{
//When p succeeds, Attempt(p) is the same as p.
- Attempt(AB).Parses(Tokenize("AB")).IntoToken("AB");
+ Attempt(AB).Parses(Tokenize("AB")).WithValue(Literal("AB"));
//When p fails without consuming input, Attempt(p) is the same as p.
Attempt(AB).FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): A expected");
@@ -164,17 +193,25 @@ public void ImprovingDefaultMessagesWithAKnownExpectation()
var labeled = Label(AB, "'A' followed by 'B'");
//When p succeeds after consuming input, Label(p) is the same as p.
- AB.Parses(Tokenize("AB")).IntoToken("AB").WithNoMessage();
- labeled.Parses(Tokenize("AB")).IntoToken("AB").WithNoMessage();
+ AB.Parses(Tokenize("AB")).WithNoMessage().WithValue(Literal("AB"));
+ labeled.Parses(Tokenize("AB")).WithNoMessage().WithValue(Literal("AB"));
//When p fails after consuming input, Label(p) is the same as p.
AB.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected");
labeled.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected");
//When p succeeds but does not consume input, Label(p) still succeeds but the potential error is included.
var succeedWithoutConsuming = new Token(null, null, "$").SucceedWithThisValue();
- succeedWithoutConsuming.PartiallyParses(Tokenize("!")).IntoToken("$").LeavingUnparsedTokens("!").WithNoMessage();
- Label(succeedWithoutConsuming, "nothing").PartiallyParses(Tokenize("!")).IntoToken("$").LeavingUnparsedTokens("!").WithMessage("(1, 1): nothing expected");
+ succeedWithoutConsuming
+ .PartiallyParses(Tokenize("!"))
+ .LeavingUnparsedTokens("!")
+ .WithNoMessage()
+ .WithValue(Literal("$"));
+ Label(succeedWithoutConsuming, "nothing")
+ .PartiallyParses(Tokenize("!"))
+ .LeavingUnparsedTokens("!")
+ .WithMessage("(1, 1): nothing expected")
+ .WithValue(Literal("$"));
//When p fails but does not consume input, Label(p) fails with the given expectation.
AB.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): A expected");
@@ -198,6 +235,11 @@ public AlternationTests()
C = Token("C");
}
+ private static Action<Token> Literal(string expectedLiteral)
+ {
+ return t => t.Literal.ShouldEqual(expectedLiteral);
+ }
+
[Fact]
public void ChoosingBetweenZeroAlternativesAlwaysFails()
{
@@ -207,22 +249,22 @@ public void ChoosingBetweenZeroAlternativesAlwaysFails()
[Fact]
public void ChoosingBetweenOneAlternativeParserIsEquivalentToThatParser()
{
- Choice(A).Parses(Tokenize("A")).IntoToken("A");
- Choice(A).PartiallyParses(Tokenize("AB")).IntoToken("A").LeavingUnparsedTokens("B");
+ Choice(A).Parses(Tokenize("A")).WithValue(Literal("A"));
+ Choice(A).PartiallyParses(Tokenize("AB")).LeavingUnparsedTokens("B").WithValue(Literal("A"));
Choice(A).FailsToParse(Tokenize("B")).LeavingUnparsedTokens("B").WithMessage("(1, 1): A expected");
}
[Fact]
public void FirstParserCanSucceedWithoutExecutingOtherAlternatives()
{
- Choice(A, NeverExecuted).Parses(Tokenize("A")).IntoToken("A");
+ Choice(A, NeverExecuted).Parses(Tokenize("A")).WithValue(Literal("A"));
}
[Fact]
public void SubsequentParserCanSucceedWhenPreviousParsersFailWithoutConsumingInput()
{
- Choice(B, A).Parses(Tokenize("A")).IntoToken("A");
- Choice(C, B, A).Parses(Tokenize("A")).IntoToken("A");
+ Choice(B, A).Parses(Tokenize("A")).WithValue(Literal("A"));
+ Choice(C, B, A).Parses(Tokenize("A")).WithValue(Literal("A"));
}
[Fact]
@@ -16,31 +16,31 @@ public void ParsesTrueLiteral()
{
var tokens = Tokenize("true");
- Json.Parses(tokens).IntoValue(value => ((bool)value).ShouldBeTrue());
+ Json.Parses(tokens).WithValue(value => ((bool)value).ShouldBeTrue());
}
[Fact]
public void ParsesFalseLiteral()
{
var tokens = Tokenize("false");
- Json.Parses(tokens).IntoValue(value => ((bool)value).ShouldBeFalse());
+ Json.Parses(tokens).WithValue(value => ((bool)value).ShouldBeFalse());
}
[Fact]
public void ParsesNullLiteral()
{
var tokens = Tokenize("null");
- Json.Parses(tokens).IntoValue(value => value.ShouldBeNull());
+ Json.Parses(tokens).WithValue(value => value.ShouldBeNull());
}
[Fact]
public void ParsesNumbers()
{
var tokens = Tokenize("10.123E-11");
- Json.Parses(tokens).IntoValue(value => value.ShouldEqual(10.123E-11m));
+ Json.Parses(tokens).WithValue(value => value.ShouldEqual(10.123E-11m));
}
[Fact]
@@ -50,8 +50,8 @@ public void ParsesQuotations()
var filled = Tokenize("\"abc \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u263a def\"");
const string expected = "abc \" \\ / \b \f \n \r \t ☺ def";
- Json.Parses(empty).IntoValue(value => value.ShouldEqual(""));
- Json.Parses(filled).IntoValue(value => value.ShouldEqual(expected));
+ Json.Parses(empty).WithValue(value => value.ShouldEqual(""));
+ Json.Parses(filled).WithValue(value => value.ShouldEqual(expected));
}
[Fact]
@@ -60,9 +60,9 @@ public void ParsesArrays()
var empty = Tokenize("[]");
var filled = Tokenize("[0, 1, 2]");
- Json.Parses(empty).IntoValue(value => ((object[])value).ShouldBeEmpty());
+ Json.Parses(empty).WithValue(value => ((object[])value).ShouldBeEmpty());
- Json.Parses(filled).IntoValue(value => value.ShouldEqual(new[] { 0m, 1m, 2m }));
+ Json.Parses(filled).WithValue(value => value.ShouldEqual(new[] { 0m, 1m, 2m }));
}
[Fact]
@@ -71,9 +71,9 @@ public void ParsesDictionaries()
var empty = Tokenize("{}");
var filled = Tokenize("{\"zero\" : 0, \"one\" : 1, \"two\" : 2}");
- Json.Parses(empty).IntoValue(value => ((Dictionary<string, object>)value).Count.ShouldEqual(0));
+ Json.Parses(empty).WithValue(value => ((Dictionary<string, object>)value).Count.ShouldEqual(0));
- Json.Parses(filled).IntoValue(value =>
+ Json.Parses(filled).WithValue(value =>
{
var dictionary = (Dictionary<string, object>) value;
dictionary["zero"].ShouldEqual(0m);
@@ -102,7 +102,7 @@ public void ParsesComplexJsonValues()
var tokens = Tokenize(complex);
- Json.Parses(tokens).IntoValue(value =>
+ Json.Parses(tokens).WithValue(value =>
{
var json = (Dictionary<string, object>)value;
json["numbers"].ShouldEqual(new[] {10m, 20m, 30m});
@@ -8,7 +8,7 @@ public class LambdaParserTests
public void CreatesParsersFromLambdas()
{
var succeeds = new LambdaParser<string>(tokens => new Parsed<string>("AA", tokens.Advance().Advance()));
- succeeds.PartiallyParses(new CharLexer().Tokenize("AABB")).LeavingUnparsedTokens("B", "B").IntoValue("AA");
+ succeeds.PartiallyParses(new CharLexer().Tokenize("AABB")).LeavingUnparsedTokens("B", "B").WithValue("AA");
var fails = new LambdaParser<string>(tokens => new Error<string>(tokens, ErrorMessage.Unknown()));
fails.FailsToParse(new CharLexer().Tokenize("AABB")).LeavingUnparsedTokens("A", "A", "B", "B").WithMessage("(1, 1): Parse error.");
@@ -118,7 +118,7 @@ public void ProvidesErrorAtAppropriatePositionWhenExtendParsersFail()
private void Parses(string input, string expectedTree)
{
- expression.Parses(Tokenize(input)).IntoValue(e => e.ToString().ShouldEqual(expectedTree));
+ expression.Parses(Tokenize(input)).WithValue(e => e.ToString().ShouldEqual(expectedTree));
}
private static IEnumerable<Token> Tokenize(string input)
@@ -19,7 +19,7 @@ public void CanBuildParserWhichSimulatesSuccessfulParsingOfGivenValueWithoutCons
{
var parser = 1.SucceedWithThisValue();
- parser.PartiallyParses(Tokenize("input")).LeavingUnparsedTokens("i", "n", "p", "u", "t").IntoValue(1);
+ parser.PartiallyParses(Tokenize("input")).LeavingUnparsedTokens("i", "n", "p", "u", "t").WithValue(1);
}
[Fact]
@@ -28,7 +28,7 @@ public void CanBuildParserFromSingleSimplerParser()
var parser = from x in Next
select x.ToUpper();
- parser.PartiallyParses(Tokenize("xy")).LeavingUnparsedTokens("y").IntoValue("X");
+ parser.PartiallyParses(Tokenize("xy")).LeavingUnparsedTokens("y").WithValue("X");
}
[Fact]
@@ -39,7 +39,7 @@ public void CanBuildParserFromOrderedSequenceOfSimplerParsers()
from c in Next
select (a + b + c).ToUpper());
- parser.PartiallyParses(Tokenize("abcdef")).LeavingUnparsedTokens("d", "e", "f").IntoValue("ABC");
+ parser.PartiallyParses(Tokenize("abcdef")).LeavingUnparsedTokens("d", "e", "f").WithValue("ABC");
}
[Fact]
Oops, something went wrong.

0 comments on commit b53bf19

Please sign in to comment.