public void Constructor_WhenGivenANullExpression_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); var codeSpan = new CodeSpan("OK", start, end); Assert.That(() => new TypedExpression(codeSpan, null), Throws.InstanceOf<ArgumentNullException>()); }
public void ToString_WhenConstructedWithANullValue_ReturnsTheCodeVerbatim() { var start = new Cursor("OK"); var end = start.Advance(2); var codeSpan = new CodeSpan("OK", start, end); Assert.That(codeSpan.ToString(), Is.EqualTo("OK")); }
protected override IEnumerable<Token> Tokenize(string source) { Cursor c = new Cursor(source); while (c.Offset < source.Length) { var currentChar = source[c.Offset]; if (operators.Contains(currentChar)) { yield return new Token(c.Offset, c.Offset + 1, "Operator.Symbol"); c.Advance(1); continue; } if (grouping.Contains(currentChar)) { yield return new Token(c.Offset, c.Offset + 1, "Grouping.Statements"); c.Advance(1); continue; } var match = whitespace.Match(source, c.Offset); if (match.Success && match.Index == c.Offset) { yield return new Token(c.Offset, c.Offset + match.Length, "Whitespace.Insignificant"); c.Advance(match.Length); continue; } match = nonOperator.Match(source, c.Offset); yield return new Token(c.Offset, c.Offset + match.Length, "Comment.Block"); c.Advance(match.Length); } }
public void Advance_WhenAdvancingPastTheEndOfTheText_ThrowsException() { var subject = "OK_OK_OK_OK_OK"; var cursor = new Cursor(subject); cursor.Advance(subject.Length); Assert.That(() => cursor.Advance(1), Throws.InstanceOf<InvalidOperationException>()); }
public void Constructor_WhenGivenANullSettingsCollection_DoesNotThrow() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Grammar(new Rule[0], null, end), Throws.Nothing); }
public void Constructor_WhenGivenANullCollectionOfRules_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Grammar(null, new Dictionary<Identifier, object>(), end), Throws.InstanceOf<ArgumentNullException>()); }
public void Constructor_WhenGivenANullValue_ThrowsException(bool ignoreCase, bool fromResource) { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new LiteralExpression(start, end, null, ignoreCase, fromResource), Throws.InstanceOf<ArgumentNullException>()); }
public void GetHashCode_WithEqualSubjectsAndIndexesAndStateKey_ReturnsSameValue([Values(0, 1, 2)] int index) { var subjectA = new Cursor("OK", index); var subjectB = subjectA.Advance(0); Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode())); }
public void OpEquality_WithEqualSubjectAndIndex_ReturnsTrue() { var subjectA = new Cursor("OK"); var subjectB = subjectA.Advance(0); Assert.That(subjectA == subjectB, Is.True); }
public void OpInequality_WithEqualSubjectAndIndex_ReturnsFalse() { var subjectA = new Cursor("OK"); var subjectB = subjectA.Advance(0); Assert.That(subjectA != subjectB, Is.False); }
public void Constructor_WhenGivenANullFlagsCollection_DoesNotThrow() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Rule(new Identifier("OK", start, end), new WildcardExpression(), null), Throws.Nothing); }
public void Constructor_WhenGivenANullCollectionOfRules_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Grammar(null, new Dictionary <Identifier, object>(), end), Throws.InstanceOf <ArgumentNullException>()); }
public void Constructor_WhenGivenANullExpression_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Identifier(null, start, end), Throws.InstanceOf<ArgumentNullException>()); }
public void Constructor_WhenGivenNullStartCursor_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new CodeSpan("OK", null, end), Throws.InstanceOf <ArgumentNullException>()); }
public void Constructor_WhenGivenNullStartCursor_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new CodeSpan("OK", null, end), Throws.InstanceOf<ArgumentNullException>()); }
public void Constructor_WhenGivenANullValue_ThrowsException(bool ignoreCase, bool fromResource) { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new LiteralExpression(start, end, null, ignoreCase, fromResource), Throws.InstanceOf <ArgumentNullException>()); }
private IParseResult <string> ParseClass(ref Cursor cursor, string characterRanges, bool negated = false, bool ignoreCase = false) { if (cursor.Location + 1 <= cursor.Subject.Length) { var c = cursor.Subject[cursor.Location]; bool match = false; for (int i = 0; !match && i < characterRanges.Length; i += 2) { match = c >= characterRanges[i] && c <= characterRanges[i + 1]; } if (!match && ignoreCase && (char.IsUpper(c) || char.IsLower(c))) { var cs = c.ToString(); for (int i = 0; !match && i < characterRanges.Length; i += 2) { var min = characterRanges[i]; var max = characterRanges[i + 1]; for (char o = min; !match && o <= max; o++) { match = (char.IsUpper(o) || char.IsLower(o)) && cs.Equals(o.ToString(), StringComparison.CurrentCultureIgnoreCase); } } } if (match ^ negated) { var endCursor = cursor.Advance(1); var substr = cursor.Subject.Substring(cursor.Location, 1); var result = this.ReturnHelper <string>(cursor, ref endCursor, state => substr); cursor = endCursor; return(result); } } return(null); }
public void Constructor_WhenGivenANullExpression_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); Assert.That(() => new Rule(new Identifier("OK", start, end), null, new Identifier[0]), Throws.InstanceOf <ArgumentNullException>()); }
public void Constructor_WhenGivenANullExpression_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); var quantifier = new Quantifier(start, end, 0); Assert.That(() => new RepetitionExpression(null, quantifier), Throws.InstanceOf <ArgumentNullException>()); }
public void Constructor_WhenGivenANullStartCursor_ThrowsException(int min, int? max, bool nullDelimiter) { var start = new Cursor("OK"); var end = start.Advance(2); var delimiter = nullDelimiter ? null : new WildcardExpression(); Assert.That(() => new Quantifier(null, end, min, max, delimiter), Throws.InstanceOf<ArgumentNullException>()); }
public void Constructor_WhenGivenANullStartCursor_ThrowsException(int min, int?max, bool nullDelimiter) { var start = new Cursor("OK"); var end = start.Advance(2); var delimiter = nullDelimiter ? null : new WildcardExpression(); Assert.That(() => new Quantifier(null, end, min, max, delimiter), Throws.InstanceOf <ArgumentNullException>()); }
public void Constructor_WhenGivenANullExpression_ThrowsException() { var start = new Cursor("OK"); var end = start.Advance(2); var codeSpan = new CodeSpan("OK", start, end); Assert.That(() => new TypedExpression(codeSpan, null), Throws.InstanceOf <ArgumentNullException>()); }
public void OpEquality_WithEqualValuesAndCursors_ReturnsTrue() { var start = new Cursor("OK", 0); var end = start.Advance(1); var subjectA = new ParseResult<int>(start, end, 0); var subjectB = new ParseResult<int>(start, end, 0); Assert.That(subjectA == subjectB, Is.True); }
public void Equals_WithOtherObject_ReturnsFalse() { var start = new Cursor("OK", 0); var end = start.Advance(1); var subjectA = new ParseResult<int>(start, end, 0); var subjectB = new object(); Assert.That(subjectA.Equals(subjectB), Is.False); }
public void OpEquality_WithNullReferenceOnRight_ReturnsFalse() { var start = new Cursor("OK", 0); var end = start.Advance(1); var subjectA = new ParseResult<int>(start, end, 0); var subjectB = (ParseResult<int>)null; Assert.That(subjectA == subjectB, Is.False); }
public void OpEquality_WithUnequalValues_ReturnsFalse() { var start = new Cursor("OK"); var end = start.Advance(1); var subjectA = new ParseResult <int>(start, end, 1); var subjectB = new ParseResult <int>(end, end, 0); Assert.That(subjectA == subjectB, Is.False); }
public void OpEquality_WithEqualValuesAndCursors_ReturnsTrue() { var start = new Cursor("OK"); var end = start.Advance(1); var subjectA = new ParseResult <int>(start, end, 0); var subjectB = new ParseResult <int>(start, end, 0); Assert.That(subjectA == subjectB, Is.True); }
public void OpInequality_WithNullReferenceOnLeft_ReturnsTrue() { var start = new Cursor("OK"); var end = start.Advance(1); var subjectA = (ParseResult <int>)null; var subjectB = new ParseResult <int>(start, end, 0); Assert.That(subjectA != subjectB, Is.True); }
public void GetHashCode_WithEqualValuesAndCursors_ReturnsSameValue([Values(0, 1, 2)] int index) { var start = new Cursor("OK"); var end = start.Advance(index); var subjectA = new ParseResult <int>(start, end, 0); var subjectB = new ParseResult <int>(start, end, 0); Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode())); }
public void OpEquality_WithNullReferenceOnRight_ReturnsFalse() { var start = new Cursor("OK"); var end = start.Advance(1); var subjectA = new ParseResult <int>(start, end, 0); var subjectB = (ParseResult <int>)null; Assert.That(subjectA == subjectB, Is.False); }
public void Equals_WithOtherObject_ReturnsFalse() { var start = new Cursor("OK"); var end = start.Advance(1); var subjectA = new ParseResult <int>(start, end, 0); var subjectB = new object(); Assert.That(subjectA.Equals(subjectB), Is.False); }
public void GetHashCode_WithEqualValuesAndCursors_ReturnsSameValue([Values(0, 1, 2)] int index) { var start = new Cursor("OK"); var end = start.Advance(index); var subjectA = new ParseResult<int>(start, end, 0); var subjectB = new ParseResult<int>(start, end, 0); Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode())); }
public void OpInequality_WithUnequalEndCursors_ReturnsTrue() { var start = new Cursor("OK"); var one = start.Advance(1); var two = one.Advance(1); var subjectA = new ParseResult <int>(start, one, 0); var subjectB = new ParseResult <int>(start, two, 0); Assert.That(subjectA != subjectB, Is.True); }
public void OpEquality_WithUnequalStartCursors_ReturnsFalse() { var start = new Cursor("OK", 0); var one = start.Advance(1); var two = one.Advance(1); var subjectA = new ParseResult<int>(start, two, 0); var subjectB = new ParseResult<int>(one, two, 0); Assert.That(subjectA == subjectB, Is.False); }
public void OpEquality_WithUnequalStartCursors_ReturnsFalse() { var start = new Cursor("OK"); var one = start.Advance(1); var two = one.Advance(1); var subjectA = new ParseResult <int>(start, two, 0); var subjectB = new ParseResult <int>(one, two, 0); Assert.That(subjectA == subjectB, Is.False); }
public void AdvanceShouldReturnOnEof() { var c = new Cursor("Lorem ipsum"); for (var i = 0; i < c.Buffer.Length - 1; i++) { c.Advance(); Assert.False(c.Eof); } Assert.Equal('m', c.Current); c.Advance(); Assert.True(c.Eof); Assert.Equal(Cursor.NullChar, c.Current); c.Advance(); Assert.True(c.Eof); Assert.Equal(Cursor.NullChar, c.Current); }
public void Advance_IncrementsTheOffset() { var subject = "OK_OK_OK_OK_OK"; var cursor = new Cursor(subject); for (int i = 1; i <= subject.Length; i++) { cursor.Advance(1); Assert.That(cursor.Offset, Is.EqualTo(i)); } }
public void Advance_WhenOnASingleLine_IncrementsTheColumnNumber() { var subject = "OK_OK_OK_OK_OK"; var cursor = new Cursor(subject); for (int i = 1; i <= subject.Length; i++) { cursor.Advance(1); Assert.That(cursor.Column, Is.EqualTo(i + 1)); } }
private IParseResult <string> ParseAny(ref Cursor cursor) { if (cursor.Location + 1 <= cursor.Subject.Length) { var substr = cursor.Subject.Substring(cursor.Location, 1); var endCursor = cursor.Advance(1); var result = this.ReturnHelper <string>(cursor, ref endCursor, state => substr); cursor = endCursor; return(result); } return(null); }
private IParseResult <string> ParseLiteral(ref Cursor cursor, string literal, bool ignoreCase = false) { if (cursor.Location + literal.Length <= cursor.Subject.Length) { var substr = cursor.Subject.Substring(cursor.Location, literal.Length); if (ignoreCase ? substr.Equals(literal, StringComparison.OrdinalIgnoreCase) : substr == literal) { var endCursor = cursor.Advance(substr.Length); var result = this.ReturnHelper <string>(cursor, ref endCursor, state => substr); cursor = endCursor; return(result); } } return(null); }
public override void HandleMessage(object message) { if (message is KeyPressedMessage msg) { char c = msg.Key.KeyChar; if (c == BackspaceChar) { cursor.Retract(); } var drawMsg = new DrawMessage((cursor.Pos.X, cursor.Pos.Y), new DrawInfo(c)); MessageBus.PostMessage(drawMsg); //matrix[cursor.Pos.X, cursor.Pos.Y] = c; if (c != BackspaceChar) { cursor.Advance(); } } }
public void OpInequality_WithUnequalEndCursors_ReturnsTrue() { var start = new Cursor("OK", 0); var one = start.Advance(1); var two = one.Advance(1); var subjectA = new ParseResult<int>(start, one, 0); var subjectB = new ParseResult<int>(start, two, 0); Assert.That(subjectA != subjectB, Is.True); }
public void Advance_WhenTheCursorIsMutable_ThrowsException() { var cursor = new Cursor("OK", 0).WithMutability(mutable: true); Assert.That(() => cursor.Advance(0), Throws.InstanceOf<InvalidOperationException>()); }
public void OpInequality_WithNullReferenceOnLeft_ReturnsTrue() { var start = new Cursor("OK", 0); var end = start.Advance(1); var subjectA = (ParseResult<int>)null; var subjectB = new ParseResult<int>(start, end, 0); Assert.That(subjectA != subjectB, Is.True); }
public void Advance_WhenPassingALineTerminator_ResetsTheColumnNumberToOne(string lineTerminator) { var subject = "OK" + lineTerminator + "OK"; var cursor = new Cursor(subject); cursor.Advance("OK".Length + lineTerminator.Length); Assert.That(cursor.Column, Is.EqualTo(1)); }
public void Advance_WhenTheCursorIsMutable_ThrowsException() { var cursor = new Cursor("OK", 0).WithMutability(mutable: true); Assert.That(() => cursor.Advance(0), Throws.InstanceOf <InvalidOperationException>()); }
public void Advance_WhenPassingLFWhichIsFollowedByCRLF_IncrementsTheColumnNumber() { var subject = "OK\n\r\n"; var cursor = new Cursor(subject); cursor.Advance("OK".Length + 1); Assert.That(cursor.Line, Is.EqualTo(2)); }
public void OpEquality_WithUnequalValues_ReturnsFalse() { var start = new Cursor("OK", 0); var end = start.Advance(1); var subjectA = new ParseResult<int>(start, end, 1); var subjectB = new ParseResult<int>(end, end, 0); Assert.That(subjectA == subjectB, Is.False); }
public void AdvanceShouldCountLinesAndColumns() { var c = new Cursor("123\n456\r\n789"); Assert.Equal('1', c.Current); Assert.Equal(0, c.Position.Offset); Assert.Equal(1, c.Position.Column); Assert.Equal(1, c.Position.Line); c.Advance(); Assert.Equal('2', c.Current); Assert.Equal(1, c.Position.Offset); Assert.Equal(2, c.Position.Column); Assert.Equal(1, c.Position.Line); c.Advance(); Assert.Equal('3', c.Current); Assert.Equal(2, c.Position.Offset); Assert.Equal(3, c.Position.Column); Assert.Equal(1, c.Position.Line); c.Advance(); Assert.Equal('\n', c.Current); Assert.Equal(3, c.Position.Offset); Assert.Equal(4, c.Position.Column); Assert.Equal(1, c.Position.Line); c.Advance(); Assert.Equal('4', c.Current); Assert.Equal(4, c.Position.Offset); Assert.Equal(1, c.Position.Column); Assert.Equal(2, c.Position.Line); c.Advance(); Assert.Equal('5', c.Current); Assert.Equal(5, c.Position.Offset); Assert.Equal(2, c.Position.Column); Assert.Equal(2, c.Position.Line); c.Advance(); Assert.Equal('6', c.Current); Assert.Equal(6, c.Position.Offset); Assert.Equal(3, c.Position.Column); Assert.Equal(2, c.Position.Line); c.Advance(); Assert.Equal('\n', c.Current); Assert.Equal(8, c.Position.Offset); Assert.Equal(3, c.Position.Column); Assert.Equal(2, c.Position.Line); c.Advance(); Assert.Equal('7', c.Current); Assert.Equal(9, c.Position.Offset); Assert.Equal(1, c.Position.Column); Assert.Equal(3, c.Position.Line); c.Advance(); Assert.Equal('8', c.Current); Assert.Equal(10, c.Position.Offset); Assert.Equal(2, c.Position.Column); Assert.Equal(3, c.Position.Line); c.Advance(); Assert.Equal('9', c.Current); Assert.Equal(11, c.Position.Offset); Assert.Equal(3, c.Position.Column); Assert.Equal(3, c.Position.Line); c.Advance(); Assert.Equal(Cursor.NullChar, c.Current); Assert.Equal(12, c.Position.Offset); Assert.Equal(4, c.Position.Column); Assert.Equal(3, c.Position.Line); }
public void Advance_WhenPassingTheTheFirstCharacterOfCRLF_DoesNotIncrementTheLineNumber() { var subject = "OK\r\n"; var cursor = new Cursor(subject); cursor.Advance("OK".Length + 1); Assert.That(cursor.Line, Is.EqualTo(1)); }
public void Advance_WhenPassingALineTerminator_IncrementsTheLineNumber(string lineTerminator) { int count = 10; var subject = string.Join(string.Empty, from i in Enumerable.Range(0, count) select lineTerminator); // Create a subject containing 10 copies of the line terminator. var cursor = new Cursor(subject); for (int i = 1; i <= count; i++) { cursor.Advance(lineTerminator.Length); Assert.That(cursor.Line, Is.EqualTo(i + 1)); } }