public void Method_Parse_MultiOutput() { var parser = Cache(ProduceMulti(() => new[] { "abc" })); var input = new StringCharacterSequence("TEST"); var start = input.Checkpoint(); var cache = new MemoryCacheResultsCache(); var state = new ParseState <char>(input, _ => { }, cache); // First attempt, we parse the string for the first time. There's nothing in cache var result = parser.Parse(state); result.Success.Should().BeTrue(); result.Results[0].Value.Should().Be("abc"); var stats = cache.GetStatistics(); stats.Attempts.Should().Be(1); stats.Misses.Should().Be(1); stats.Hits.Should().Be(0); // Second attempt, we start from the beginning again, but now the value is in cache // already. We keep the same statistics from last time (1 attempt, 1 miss) and append // the new hit start.Rewind(); result = parser.Parse(state); result.Success.Should().BeTrue(); result.Results[0].Value.Should().Be("abc"); stats = cache.GetStatistics(); stats.Attempts.Should().Be(2); stats.Misses.Should().Be(1); stats.Hits.Should().Be(1); }
public void Checkpoint_Test() { var target = new StringCharacterSequence("abc"); var cp = target.Checkpoint(); target.GetNext().Should().Be('a'); target.GetNext().Should().Be('b'); target.GetNext().Should().Be('c'); target.GetNext().Should().Be('\0'); cp.Rewind(); target.GetNext().Should().Be('a'); target.GetNext().Should().Be('b'); target.GetNext().Should().Be('c'); target.GetNext().Should().Be('\0'); }
public void Location_Rewind() { var target = new StringCharacterSequence("abc\nde"); target.GetNext(); target.GetNext(); target.GetNext(); target.CurrentLocation.Line.Should().Be(1); target.CurrentLocation.Column.Should().Be(3); var checkpoint = target.Checkpoint(); target.GetNext().Should().Be('\n'); target.CurrentLocation.Line.Should().Be(2); target.CurrentLocation.Column.Should().Be(0); checkpoint.Rewind(); target.CurrentLocation.Line.Should().Be(1); target.CurrentLocation.Column.Should().Be(3); }