From 86db56d3feb5b034a4d24957b736cb7e18e77b05 Mon Sep 17 00:00:00 2001 From: moh-hassan Date: Tue, 14 Dec 2021 19:15:06 +0200 Subject: [PATCH] Fix CommentParser eol issue #184 --- src/Sprache/CommentParser.cs | 2 +- test/Sprache.Tests/CommentParserTest.cs | 35 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/Sprache.Tests/CommentParserTest.cs diff --git a/src/Sprache/CommentParser.cs b/src/Sprache/CommentParser.cs index 2c003b3..1645058 100644 --- a/src/Sprache/CommentParser.cs +++ b/src/Sprache/CommentParser.cs @@ -77,7 +77,7 @@ public Parser SingleLineComment throw new ParseException("Field 'Single' is null; single-line comments not allowed."); return from first in Parse.String(Single) - from rest in Parse.CharExcept(NewLine).Many().Text() + from rest in Parse.CharExcept(new[] {'\r', '\n' }).Many().Text() select rest; } private set { } diff --git a/test/Sprache.Tests/CommentParserTest.cs b/test/Sprache.Tests/CommentParserTest.cs new file mode 100644 index 0000000..e8ee8b8 --- /dev/null +++ b/test/Sprache.Tests/CommentParserTest.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Sprache.Tests +{ + public class CommentParserTest + { + [Fact] + public void Check_single_line_comment_for_both_eol_windows_linux() + { + var parser = new CommentParser(); + var expected = "this is a line comment"; + var result = parser.SingleLineComment.Parse("//this is a line comment\r\nint i=5;"); + Assert.Equal(result, expected); + result = parser.SingleLineComment.Parse("//this is a line comment\nint i=5;"); + Assert.Equal(result, expected); + + } + [Fact] + public void Check_any_comment_for_both_eol_windows_linux() + { + var parser = new CommentParser(); + var expected = "this is a line comment"; + var result = parser.AnyComment.Parse("//this is a line comment\r\nint i=5;"); + Assert.Equal(result, expected); + result = parser.AnyComment.Parse("//this is a line comment\nint i=5;"); + Assert.Equal(result, expected); + + } + } +}