Skip to content

Commit

Permalink
Merge pull request #912 from EdwardCooke/ec-732
Browse files Browse the repository at this point in the history
Fixed #732, quote strings starting with whitespace characters

+semver:fix
  • Loading branch information
EdwardCooke authored Feb 26, 2024
2 parents 19d7875 + 66c68a2 commit 33ed33b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
13 changes: 10 additions & 3 deletions YamlDotNet.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using BenchmarkDotNet.Running;
using YamlDotNet.Benchmark;
using YamlDotNet.Serialization;

BenchmarkSwitcher.FromAssembly(typeof(YamlStreamBenchmark).Assembly).Run(args);
var serializer = new SerializerBuilder()
.WithQuotingNecessaryStrings()
.Build();

var s = "\t, something";
var yaml = serializer.Serialize(s);
Console.WriteLine(yaml);
var deserializer = new DeserializerBuilder().Build();
var value = deserializer.Deserialize(yaml);
30 changes: 30 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,36 @@ public void SerializeEnumAsNumber()
Assert.Equal(TestEnumAsNumber.Test1 | TestEnumAsNumber.Test2, v);
}

[Fact]
public void TabsGetQuotedWhenQuoteNecessaryStringsIsOn()
{
var serializer = new SerializerBuilder()
.WithQuotingNecessaryStrings()
.Build();

var s = "\t, something";
var yaml = serializer.Serialize(s);
Console.WriteLine(yaml);
var deserializer = new DeserializerBuilder().Build();
var value = deserializer.Deserialize(yaml);
Assert.Equal(s, value);
}

[Fact]
public void SpacesGetQuotedWhenQuoteNecessaryStringsIsOn()
{
var serializer = new SerializerBuilder()
.WithQuotingNecessaryStrings()
.Build();

var s = " , something";
var yaml = serializer.Serialize(s);
Console.WriteLine(yaml);
var deserializer = new DeserializerBuilder().Build();
var value = deserializer.Deserialize(yaml);
Assert.Equal(s, value);
}

[Flags]
private enum TestEnumAsNumber
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public sealed class TypeAssigningEventEmitter : ChainedEventEmitter
+ @"|[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?" // float number
+ @"|[-+]?(\.inf|\.Inf|\.INF)"
+ @"|\.nan|\.NaN|\.NAN"
+ @"|\s.*"
+ @")$";

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion tools/build/AutoNumberToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS

if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
return reader.GetString()!;
}

using var document = JsonDocument.ParseValue(ref reader);
Expand Down
8 changes: 4 additions & 4 deletions tools/build/BuildDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task<GitVersion> ResolveVersion(Options options, PreviousRel
};
jsonOptions.Converters.Add(new AutoNumberToStringConverter());

version = JsonSerializer.Deserialize<GitVersion>(versionJson, jsonOptions);
version = JsonSerializer.Deserialize<GitVersion>(versionJson, jsonOptions)!;

// Workaround to prevent issues with some consumers of the NuGet API that build
// links manually instead of following the links that come in the response.
Expand Down Expand Up @@ -290,7 +290,7 @@ public static async Task LinkPullRequestsToReleases(GitVersion version)

var release = await releaseResponse.EnsureSuccessStatusCode().Content.ReadAsAsync<GitHubApiModels.Release>();

var linkedIssues = Regex.Matches(release.body, @"#(\d+)").Select(m => m.Groups[1].Value);
var linkedIssues = Regex.Matches(release.body ?? string.Empty, @"#(\d+)").Select(m => m.Groups[1].Value);
WriteVerbose($"Found the following issues / pull requests: {string.Join(",", linkedIssues)}");

foreach (var issueNumber in linkedIssues)
Expand Down Expand Up @@ -342,12 +342,12 @@ public static Task Document(Options options)

const string ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";

var testDefinitions = report.Root
var testDefinitions = report.Root!
.Element(XName.Get("TestDefinitions", ns))!
.Elements(XName.Get("UnitTest", ns))
.Select(e =>
{
var testMethod = e.Element(XName.Get("TestMethod", ns));
var testMethod = e.Element(XName.Get("TestMethod", ns))!;
var sampleClassName = testMethod.Attribute("className")!.Value;
var sampleMethodName = testMethod.Attribute("name")!.Value;
Expand Down

0 comments on commit 33ed33b

Please sign in to comment.