public void FinishedShouldBeTrueWhenNoDataRemains() { //Arrange var keeper = new StringKeeper("test"); keeper.Take(); //t keeper.Take(); //e keeper.Take(); //s keeper.Take(); //t //Act/Assert keeper.Finished.Should().BeTrue(); }
public void TakeReturnsNulWhenNoDataRemains() { //Arrange var keeper = new StringKeeper("test"); keeper.Take(); //t keeper.Take(); //e keeper.Take(); //s keeper.Take(); //t //Act var result = keeper.Take(); //Assert result.Should().Be("\0"); }
public static IEnumerable <ApplyIfToken> Analyse(string source) { var stringKeeper = new StringKeeper(source); while (!stringKeeper.IsEmpty) { if (stringKeeper.IsWhitespace) { stringKeeper = stringKeeper.Take(); continue; } StringKeeper newStringKeeper; ApplyIfToken token; if (TryTake(stringKeeper, Tokens, out newStringKeeper, out token) || TryTakeIdentifier(stringKeeper, out newStringKeeper, out token)) { stringKeeper = newStringKeeper; yield return(token); } else { var finish = new StringKeeper(stringKeeper); while (!finish.IsEmpty) { finish = finish.Take(); } yield return(new ApplyIfToken(ApplyIfTokenType.Error, finish.Difference(stringKeeper))); stringKeeper = finish; } } }
public void TakeMovesThePositionAlong() { //Arrange var keeper = new StringKeeper("test"); //Act keeper.Take(); //Assert keeper.Next.Should().Be('e'); }
public void TakeReturnsTheNextCharacter() { //Arrange var keeper = new StringKeeper("test"); //Act var result = keeper.Take(); //Assert result.Should().Be("t"); }
public void CopiedInstancesShouldHaveTheSamePositionAsTheOriginal() { //Arrange var keeper = new StringKeeper("test"); keeper.Take(); //Act var keeper2 = new StringKeeper(keeper); //Assert keeper2.TakeAll().Should().Be(keeper.TakeAll()); }
public void WhitespaceSkippedDoesNotHaveToBeAtTheStart() { //Arrange var keeper = new StringKeeper("X \t\n\r\ftest"); keeper.Take(); //Act keeper.SkipWhiteSpace(); //Assert keeper.Next.Should().Be('t'); }
public void TakeAllReturnsRemainingText() { //Arrange var keeper = new StringKeeper("test"); keeper.Take(); //Act var result = keeper.TakeAll(); //Assert result.Should().Be("est"); }
private static bool TryTake(StringKeeper stringKeeper, Dictionary <string, ApplyIfToken> tokens, out StringKeeper newStringKeeper, out ApplyIfToken token) { var thisToken = tokens.FirstOrDefault(x => stringKeeper.IsNext(x.Key, false)); if (thisToken.Key != null) { var work = stringKeeper.Take(thisToken.Key.Length); newStringKeeper = work; token = thisToken.Value; return(true); } newStringKeeper = stringKeeper; token = null; return(false); }
private static bool TryTakeIdentifier(StringKeeper stringKeeper, out StringKeeper newStringKeeper, out ApplyIfToken token) { var work = new StringKeeper(stringKeeper); var value = string.Empty; while (!IsTerminator(work)) { value += work.Next; work = work.Take(); } if (!string.IsNullOrWhiteSpace(value)) { newStringKeeper = work; token = new ApplyIfToken(ApplyIfTokenType.Identifier, value); return(true); } newStringKeeper = stringKeeper; token = null; return(false); }