Skip to content
Open
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
198 changes: 198 additions & 0 deletions YamlDotNet.Test/Core/ScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,204 @@ public void CommentsAreOmittedUnlessRequested()
StreamEnd);
}

[Fact]
public void JsonCommentsAreReturnedWhenRequested()
{
AssertSequenceOfTokensFrom(new Scanner(Yaml.ReaderForText(@"
// Top comment
- first // Comment on first item
- second /* First comment on second item */ /* Second comment on second item */
/*
* Bottom comment
*/
"), skipComments: false, allowJsonComments: true, maxKeySize: 1024),
StreamStart,
StandaloneComment("Top comment"),
BlockSequenceStart,
BlockEntry,
PlainScalar("first"),
InlineComment("Comment on first item"),
BlockEntry,
PlainScalar("second"),
StandaloneComment("First comment on second item "),
InlineComment("Second comment on second item "),
StandaloneComment("\n * Bottom comment\n "),
BlockEnd,
StreamEnd);
}

[Fact]
public void JsonCommentsAreCorrectlyMarked()
{
var sut = new Scanner(Yaml.ReaderForText(@"
/*
* Comment before first item
*/
- first // Comment on first item
"), skipComments: false, allowJsonComments: true, maxKeySize: 1024);


while (sut.MoveNext())
{
if (sut.Current is Comment comment)
{
Assert.Equal(0, comment.Start.Index);
Assert.Equal(35, comment.End.Index);

break;
}
}

while (sut.MoveNext())
{
if (sut.Current is Comment comment)
{
Assert.Equal(44, comment.Start.Index);
Assert.Equal(68, comment.End.Index);

return;
}
}

Assert.Fail("Did not find the comments");
}

[Fact]
public void JsonCommentsAreOmittedUnlessRequested()
{
AssertSequenceOfTokensFrom(new Scanner(Yaml.ReaderForText(@"
// Top comment
- first // Comment on first item
- second /* First comment on second item */ /* Second comment on second item */
/*
* Bottom comment
*/
"), skipComments: true, allowJsonComments: true, maxKeySize: 1024),
StreamStart,
BlockSequenceStart,
BlockEntry,
PlainScalar("first"),
BlockEntry,
PlainScalar("second"),
BlockEnd,
StreamEnd);
}

[Fact]
public void JsonCommentsAreAllowedAfterValues()
{
AssertSequenceOfTokensFrom(new Scanner(Yaml.ReaderForText(@"
{
comma: 1,// comment after comma
singleQuotes: '1'// comment after single-quoted string
doubleQuotes: ""1""// comment after double-quoted string
object: {}//comment after object
array: []//comment after array
null: null//comment after null
false: false//comment after false
true: true//comment after true
positiveNumber: 123.4e5//comment after positive number
negativeNumber: -123.4E-5//comment after negative number
}
"), skipComments: false, allowJsonComments: true, maxKeySize: 1024),
StreamStart,
FlowMappingStart,
Key,
PlainScalar("comma"),
Value,
PlainScalar("1"),
FlowEntry,
InlineComment("comment after comma"),
Key,
PlainScalar("singleQuotes"),
Value,
SingleQuotedScalar("1"),
InlineComment("comment after single-quoted string"),
PlainScalar("doubleQuotes"),
Value,
DoubleQuotedScalar("1"),
InlineComment("comment after double-quoted string"),
PlainScalar("object"),
Value,
FlowMappingStart,
FlowMappingEnd,
InlineComment("comment after object"),
PlainScalar("array"),
Value,
FlowSequenceStart,
FlowSequenceEnd,
InlineComment("comment after array"),
PlainScalar("null"),
Value,
PlainScalar("null"),
InlineComment("comment after null"),
PlainScalar("false"),
Value,
PlainScalar("false"),
InlineComment("comment after false"),
PlainScalar("true"),
Value,
PlainScalar("true"),
InlineComment("comment after true"),
PlainScalar("positiveNumber"),
Value,
PlainScalar("123.4e5"),
InlineComment("comment after positive number"),
PlainScalar("negativeNumber"),
Value,
PlainScalar("-123.4E-5"),
InlineComment("comment after negative number"),
FlowMappingEnd,
StreamEnd);
}

[Fact]
public void MultilineJsonCommentsAreMarkedAsStandalone()
{
AssertSequenceOfTokensFrom(new Scanner(Yaml.ReaderForText(@"
{
/*
* Top comment.
*/
/*0*/ /*1*/""foo""/*2*/ /*3*/:/*4*/ /*5*/false/*6*/ /*7*/,// 8
// Middle comment.
/*9*/""bar""/*10*/:/*11*/true/*12*/
/*
* Bottom comment.
*/
}
"), skipComments: false, allowJsonComments: true, maxKeySize: 1024),
StreamStart,
FlowMappingStart,
StandaloneComment("\n * Top comment.\n "),
StandaloneComment("0"),
StandaloneComment("1"),
Key,
DoubleQuotedScalar("foo"),
StandaloneComment("2"),
StandaloneComment("3"),
Value,
StandaloneComment("4"),
StandaloneComment("5"),
PlainScalar("false"),
StandaloneComment("6"),
StandaloneComment("7"),
FlowEntry,
InlineComment("8"),
StandaloneComment("Middle comment."),
StandaloneComment("9"),
Key,
DoubleQuotedScalar("bar"),
StandaloneComment("10"),
Value,
StandaloneComment("11"),
PlainScalar("true"),
InlineComment("12"),
StandaloneComment("\n * Bottom comment.\n "),
FlowMappingEnd,
StreamEnd);
}

[Fact]
public void MarksOnDoubleQuotedScalarsAreCorrect()
{
Expand Down
114 changes: 114 additions & 0 deletions YamlDotNet.Test/Helpers/JsonHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Text;
using Xunit;
using YamlDotNet.Helpers;

namespace YamlDotNet.Test.Helpers
{
public class JsonHelperTests
{
[Fact]
public void IsJsonLiteralReturnsTrueForNull()
{
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("null")));
}

[Fact]
public void IsJsonLiteralReturnsTrueForBooleans()
{
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("true")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("false")));
}

[Fact]
public void IsJsonLiteralReturnsTrueForNumbers()
{
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0.0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0.0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0e0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0e+0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0e-0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0e0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0e+0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0e-0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0.0e0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0.0e+0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("0.0e-0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0.0e0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0.0e+0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-0.0e-0")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123.45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123.45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123e45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123e+45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123e-45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123e45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123e+45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123e-45")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e+67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e-67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123.45e67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123.45e+67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123.45e-67")));
Assert.True(JsonHelper.IsJsonLiteral(new StringBuilder("-123.45E+67")));
}

[Fact]
public void IsJsonLiteralReturnsFalseForNonLiteralValues()
{
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("foo")));

Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("Null")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("True")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("False")));

Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("NULL")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("TRUE")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("FALSE")));

Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("-")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("+")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("+123")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("01")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("-01")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45.67")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder(".123")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder(".")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e67e8")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e67.8")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123e67.8")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e+")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("123.45e-")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("e")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("0xcoffee")));
Assert.False(JsonHelper.IsJsonLiteral(new StringBuilder("0b101010")));
}
}
}
Loading