Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when parsing blank comments. #19

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Tomlet.Tests/BasicKeyValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ public void MultipleKeysCanBeParsedIgnoringComments()

//Check keys
Assert.Collection(doc.Entries.Keys,
key1 => Assert.Equal("key", key1),
key2 => Assert.Equal("another", key2)
key1 => Assert.Equal("key1", key1),
key2 => Assert.Equal("key2", key2),
key3 => Assert.Equal("another", key3)
);

//Check values
Assert.Collection(doc.Entries.Values,
value1 => Assert.Equal("value", Assert.IsType<TomlString>(value1).Value),
value2 => Assert.Equal("# This is not a comment", Assert.IsType<TomlString>(value2).Value)
value1 => Assert.Equal("value1", Assert.IsType<TomlString>(value1).Value),
value2 => Assert.Equal("value2", Assert.IsType<TomlString>(value2).Value),
value3 => Assert.Equal("# This is not a comment", Assert.IsType<TomlString>(value3).Value)
);
}

Expand Down
10 changes: 7 additions & 3 deletions Tomlet.Tests/CommentDeserializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public void CommentsCanBeReadFromTheFileCorrectly()
{
var doc = GetDocument(TestResources.CommentTestInput);

var firstValue = doc.GetValue("key");
Assert.Equal("This is a full-line comment", firstValue.Comments.PrecedingComment);
Assert.Equal("This is a comment at the end of a line", firstValue.Comments.InlineComment);
var firstValue = doc.GetValue("key1");
Assert.Null(firstValue.Comments.PrecedingComment);
Assert.Null(firstValue.Comments.InlineComment);

var secondValue = doc.GetValue("key2");
Assert.Equal("This is a full-line comment", secondValue.Comments.PrecedingComment);
Assert.Equal("This is a comment at the end of a line", secondValue.Comments.InlineComment);
}
}
6 changes: 3 additions & 3 deletions Tomlet.Tests/CommentSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void CommentsOnTablesWork()
[table] # This is an inline comment
key = ""value"" # Inline comment on value";

Assert.Equal(expected, doc.SerializedValue.Trim());
Assert.Equal(expected, doc.SerializedValue.Trim().ReplaceLineEndings());
}

[Fact]
Expand Down Expand Up @@ -84,7 +84,7 @@ public void CommentsOnTableArraysWork()
[[table-array]] # This is an inline comment on the table
key = ""value"" # Inline comment on value".Trim();

Assert.Equal(expected, doc.SerializedValue.Trim());
Assert.Equal(expected, doc.SerializedValue.Trim().ReplaceLineEndings());
}

[Fact]
Expand All @@ -105,7 +105,7 @@ public void CommentsOnPrimitiveArraysWork()
]";

//Replace tabs with spaces because this source file uses spaces
var actual = doc.SerializedValue.Trim().Replace("\t", " ");
var actual = doc.SerializedValue.Trim().Replace("\t", " ").ReplaceLineEndings();
Assert.Equal(expected, actual);
}

Expand Down
6 changes: 4 additions & 2 deletions Tomlet.Tests/TestResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Tomlet.Tests/TestResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CommentTestInput" xml:space="preserve">
<value># This is a full-line comment
key = "value" # This is a comment at the end of a line
<value>#
key1 = "value1" #
# This is a full-line comment
key2 = "value2" # This is a comment at the end of a line
another = "# This is not a comment"</value>
</data>
<data name="BasicKVPTestInput" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions Tomlet.Tests/TomlTableArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void TableArraySerializationWorks()
array
};

var tomlString = TomletMain.TomlStringFrom(documentRoot).Trim();
var tomlString = TomletMain.TomlStringFrom(documentRoot).Trim().ReplaceLineEndings();

var expectedResult = @"
[[array]]
Expand Down Expand Up @@ -146,7 +146,7 @@ public void TablesWithNestedArraysHaveCorrectWhitespace()

[[Root.Array]]
A = ""C""
B = ""D""", str.Trim());
B = ""D""", str.Trim().ReplaceLineEndings());
}
}
}
2 changes: 1 addition & 1 deletion Tomlet/TomlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ private void FindParentAndRelativeKey(ref TomlTable parent, ref string relativeN
{
var line = reader.ReadWhile(c => !c.IsNewline());

if(line[0] == ' ')
if(line.Length > 0 && line[0] == ' ')
line = line.Substring(1);

foreach (var i in line.Select(c => (int) c))
Expand Down