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

Fixes #79 and #82 #81

Merged
merged 12 commits into from
Feb 1, 2024
14 changes: 9 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ on:

jobs:
build:
runs-on: windows-latest

strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0

- name: Install .NET 7.0
uses: actions/setup-dotnet@v3
- name: Install .NET 6 & 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'
dotnet-version: |
6
8

- name: Build, Test, Pack, Publish
shell: bash
Expand Down
1 change: 1 addition & 0 deletions src/Tomlyn.Signed/Tomlyn.Signed.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageId>Tomlyn.Signed</PackageId>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Tomlyn\**\*.cs" Exclude="..\Tomlyn\obj\**;..\Tomlyn\bin\**">
Expand Down
10 changes: 9 additions & 1 deletion src/Tomlyn.Tests/AssertHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System;
Expand Down Expand Up @@ -27,5 +27,13 @@ public static string NormalizeEndOfLine(string text)
{
return text.Replace("\r\n", "\n");
}

#if !NET5_0_OR_GREATER
public static string ReplaceLineEndings(this string text, string? newLine = null)
{
newLine ??= Environment.NewLine;
return text.Replace("\r\n", "\n").Replace("\n", newLine);
}
#endif
}
}
90 changes: 90 additions & 0 deletions src/Tomlyn.Tests/FloatingRoundtripTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using NUnit.Framework;
using Tomlyn.Model;

namespace Tomlyn.Tests
{
[TestFixture]
public class FloatingRoundtripTests{


[TestCase(0.0)]
[TestCase(-0.0)]
[TestCase(1.0)]
[TestCase(-1.0)]
[TestCase(double.PositiveInfinity)]
[TestCase(double.NegativeInfinity)]
[TestCase(double.NaN)]
#if NET8_0_OR_GREATER
[TestCase(double.NegativeZero)]
#endif
[TestCase(double.Epsilon)]
[TestCase(-double.Epsilon)]
// [TestCase(0.1)] - - These fail. Adjusting g16->g17 fixes some but not all.
lilith marked this conversation as resolved.
Show resolved Hide resolved
// [TestCase(0.99)]
// [TestCase(0.3)]
// [TestCase(double.MinValue)]
// [TestCase(double.MaxValue)]
public void TestDoublesRoundtrip(double number)
{
// we want to increment f64 by the smallest possible value
// and verify it changes the serialized value
// IEEE 754 compliance means -0.0 and +0.0 are different
// special values +inf=inf, -inf, nan=+nan, -nan
var model = new TomlTable
{
["float"] = (float)number,
["double"] = number
};
var toml = Toml.FromModel(model);
var parsed = Toml.Parse(toml);
var parsedDouble = (double)parsed.ToModel()["double"];
var parsedFloat = (double)parsed.ToModel()["float"];
Assert.True(number == parsedDouble || double.IsNaN(number),
$"(f64->str->f64) expected {number:g30} but got {parsedDouble:g30}. \nString form: \n{toml}");
Assert.AreEqual(number, parsedDouble);
Assert.True((float)number == parsedFloat || double.IsNaN(number),
$"(f64->f32->str->f64->f32) expected {(float)number:g30} but got {parsedFloat:g30}. \nString form: \n{toml}");
Assert.AreEqual((float)number, parsedFloat);
}

[TestCase(0.0f)]
[TestCase(-0.0f)]
[TestCase(1.0f)]
[TestCase(-1.0f)]
[TestCase(float.PositiveInfinity)]
[TestCase(float.NegativeInfinity)]
[TestCase(float.NaN)]
#if NET8_0_OR_GREATER
[TestCase(float.NegativeZero)]
#endif
[TestCase(float.Epsilon)]
[TestCase(-float.Epsilon)]
// [TestCase(0.1f)] - These fail. Adjusting g16->g17 fixes them
// [TestCase(0.99f)]
// [TestCase(0.3f)]
// [TestCase(float.MinValue)]
// [TestCase(float.MaxValue)]
public void TestFloatsRoundtrip(float number)
{
var model = new TomlTable
{
["float"] = number,
["double"] = (double)number
};
var toml = Toml.FromModel(model);

var parsed = Toml.Parse(toml);
var parsedDouble = (double)parsed.ToModel()["double"];
var parsedFloatAsDouble = (double)parsed.ToModel()["float"];
Assert.True((double)number == parsedDouble || double.IsNaN(number),
$"(f32->f64->str->f64) expected double {(double)number:g64} but got double {parsedDouble:g64}. \nString form: \n{toml}");
Assert.True(number == (float)parsedFloatAsDouble || double.IsNaN(number),
$"(f32->str->f64->f32) expected float {number:g64} but got float {(float)parsedFloatAsDouble:g64}. \nString form: \n{toml}");

}
}
}
8 changes: 8 additions & 0 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ public void TestPrimitives()
Float64Value = 2.5,
DateTime = new DateTime(1970, 1, 1),
DateTimeOffset = new DateTimeOffset(1980, 1, 1, 0, 23, 1, TimeSpan.FromHours(-2)),
#if NET6_0_OR_GREATER
DateOnly = new DateOnly(1970, 5, 27),
TimeOnly = new TimeOnly(7, 32, 0, 999),
#endif
TomlDateTime = new TomlDateTime(new DateTimeOffset(new DateTime(1990, 11, 15)), 0, TomlDateTimeKind.LocalDateTime)
};

Expand Down Expand Up @@ -149,8 +151,10 @@ public void TestPrimitiveFields()
Float64Value = 2.5,
DateTime = new DateTime(1970, 1, 1),
DateTimeOffset = new DateTimeOffset(1980, 1, 1, 0, 23, 1, TimeSpan.FromHours(-2)),
#if NET6_0_OR_GREATER
DateOnly = new DateOnly(1970, 5, 27),
TimeOnly = new TimeOnly(7, 32, 0, 999),
#endif
TomlDateTime = new TomlDateTime(new DateTimeOffset(new DateTime(1990, 11, 15)), 0, TomlDateTimeKind.LocalDateTime)
};

Expand Down Expand Up @@ -207,8 +211,10 @@ public void TestNullableValueTypes()
Float64Value = 2.5,
DateTime = new DateTime(1970, 1, 1),
DateTimeOffset = new DateTimeOffset(1980, 1, 1, 0, 23, 1, TimeSpan.FromHours(-2)),
#if NET6_0_OR_GREATER
DateOnly = new DateOnly(1970, 5, 27),
TimeOnly = new TimeOnly(7, 32, 0, 999),
#endif
TomlDateTime = new TomlDateTime(new DateTimeOffset(new DateTime(1990, 11, 15)), 0, TomlDateTimeKind.LocalDateTime)
};

Expand Down Expand Up @@ -280,8 +286,10 @@ public void TestNullableValueTypeFields()
Float64Value = 2.5,
DateTime = new DateTime(1970, 1, 1),
DateTimeOffset = new DateTimeOffset(1980, 1, 1, 0, 23, 1, TimeSpan.FromHours(-2)),
#if NET6_0_OR_GREATER
DateOnly = new DateOnly(1970, 5, 27),
TimeOnly = new TimeOnly(7, 32, 0, 999),
#endif
TomlDateTime = new TomlDateTime(new DateTimeOffset(new DateTime(1990, 11, 15)), 0, TomlDateTimeKind.LocalDateTime)
};

Expand Down
14 changes: 13 additions & 1 deletion src/Tomlyn.Tests/Tomlyn.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">net6.0;net8.0;net48</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="nunit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<PackageReference Include="Portable.System.DateTimeOnly" Version="8.0.0" Condition="'$(TargetFramework)' == 'net48'" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" Condition="'$(TargetFramework)' == 'net48'" />
<PackageReference Include="PolySharp" Version="1.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/Tomlyn/Model/ModelToTomlTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void WriteKey(string name)
private string EscapeKey(string name)
{
if (string.IsNullOrWhiteSpace(name)) return $"\"{name.EscapeForToml()}\"";

// A-Za-z0-9_-
foreach (var c in name)
{
Expand Down Expand Up @@ -174,7 +174,6 @@ private bool VisitObject(ObjectDynamicAccessor accessor, object currentObject, b
try
{


// Pre-convert values to TOML values
var convertToToml = _context.ConvertToToml;
if (convertToToml != null)
Expand All @@ -200,8 +199,8 @@ private bool VisitObject(ObjectDynamicAccessor accessor, object currentObject, b
}

// Sort primitive first
properties = properties.OrderBy(_ => _,
Comparer<KeyValuePair<string,object>>.Create((left, right) =>
properties = properties.OrderBy(p => p,
Comparer<KeyValuePair<string,object?>>.Create((left, right) =>
{
var leftValue = left.Value;
var rightValue = right.Value;
Expand Down
1 change: 1 addition & 0 deletions src/Tomlyn/Tomlyn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Import Project="Tomlyn.props" />
<PropertyGroup>
<PackageId>Tomlyn</PackageId>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Folder Include="Compatibility\" />
Expand Down
16 changes: 14 additions & 2 deletions src/Tomlyn/Tomlyn.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>

<LangVersion>latest</LangVersion>
<Description>Tomlyn is a TOML parser, validator and authoring library for .NET.</Description>
<Copyright>Alexandre Mutel</Copyright>
<Authors>Alexandre Mutel</Authors>
Expand All @@ -21,6 +21,12 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>




</PropertyGroup>

<ItemGroup>
Expand All @@ -35,6 +41,12 @@
</PackageReference>
<!--Add support for sourcelink-->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.*" PrivateAssets="All" />
<!--Polyfill language features for .NET Standard 2.0 et all, generating internal
items only for the compiler. Enables langversion=latest-->
<PackageReference Include="PolySharp" Version="1.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<Target Name="PatchVersion" AfterTargets="MinVer">
Expand Down
10 changes: 5 additions & 5 deletions src/global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"version": "7.0.100",
"rollForward": "latestMinor",
"allowPrerelease": false
}
"sdk": {
"version": "8.0.0",
"rollForward": "latestMinor",
"allowPrerelease": false
}
}
Loading