Skip to content

Commit

Permalink
[.NET] Adopt some modern c# language feature, enable warnings as erro…
Browse files Browse the repository at this point in the history
…rs, adopt system.text.json (#275)

* [.NET] Adopt c# 10 ImplicitUsings

* [.NET] Replace TinyJson to system.text.json

* [.NET] Adopt c#12 collection initializers

* Fix acceptance tests

* Enable tests for .NET Framework 4.6.2

* Added test to block system.text.json update
  • Loading branch information
Romfos committed Sep 10, 2024
1 parent d90788d commit a8ffa89
Show file tree
Hide file tree
Showing 61 changed files with 95 additions and 536 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
- [Ruby] update dependency messages up to v26 ([#267](https://github.com/cucumber/gherkin/pull/267))

### Changed
- [.NET] Drop unsupported frameworks. Now supported target frameworks are .NET 8, .NET Framework 4.6.2, .NET Standard 2.0 ([#265](https://github.com/cucumber/gherkin/pull/265))
- [.NET] Drop unsupported frameworks. Now supported target frameworks are .NET 8, .NET Standard 2.0 ([#265](https://github.com/cucumber/gherkin/pull/265))
- [.NET] Adopt File Scoped Namespaces c# feature ([#271](https://github.com/cucumber/gherkin/pull/271))
- [.NET] Adopt c# 12 primary constructors ([#272](https://github.com/cucumber/gherkin/pull/272))
- [.NET] Adopt c# 10 ImplicitUsings
- [.NET] Replace TinyJson to System.Text.Json
- [.NET] Enable warnings as errors

## [29.0.0] - 2024-08-12
### Added
Expand Down
2 changes: 2 additions & 0 deletions dotnet/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<LangVersion>12</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/AstBuildingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using FluentAssertions;
using Gherkin.CucumberMessages.Types;
using Gherkin.Specs.Helper;
Expand Down
12 changes: 7 additions & 5 deletions dotnet/Gherkin.Specs/CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Gherkin.CucumberMessages;
using Gherkin.Specs.EventStubs;
using Gherkin.Specs.Tokens;
using Utf8Json.Resolvers;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Gherkin.Specs.CLI;

Expand Down Expand Up @@ -102,7 +100,11 @@ private static int PrintEvents(PrintEventsArgs args)
{
foreach (var evt in gherkinEventsProvider.GetEvents(sourceEventEvent))
{
var jsonString = Utf8Json.JsonSerializer.ToJsonString((object)evt, StandardResolver.ExcludeNullCamelCase);
var jsonString = JsonSerializer.Serialize(evt, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
Converters = { new JsonStringEnumConverter() },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});
Console.WriteLine(jsonString);
}
}
Expand Down
21 changes: 21 additions & 0 deletions dotnet/Gherkin.Specs/DependencyValidationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if NETFRAMEWORK
using System.Reflection;
using System.Text.Json;
using Xunit;

namespace Gherkin.Specs;

public sealed class DependencyValidationTests
{
[Fact]
public void SystemTextJsonUpgradeBlocker()
{
// this test was made intentionally to block upgrade for system.text.json nuget package for .NET standard version of library
// discussion: https://github.com/cucumber/messages/pull/237#issuecomment-2225649432

var version = typeof(JsonSerializer).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

Assert.Equal("6.0.21.52210", version); // System.Text.Json Version 6.0.0
}
}
#endif
3 changes: 0 additions & 3 deletions dotnet/Gherkin.Specs/EventStubs/GherkinEventsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Gherkin.CucumberMessages;
using Gherkin.CucumberMessages.Pickles;
using Gherkin.CucumberMessages.Types;
using System;
using System.Collections.Generic;
using System.IO;

namespace Gherkin.Specs.EventStubs;

Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin.Specs/EventStubs/SourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using Gherkin.CucumberMessages.Types;

namespace Gherkin.Specs.EventStubs;
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin.Specs/EventTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using FluentAssertions;
using Gherkin.CucumberMessages;
Expand Down
4 changes: 2 additions & 2 deletions dotnet/Gherkin.Specs/Gherkin.Specs.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net462</TargetFrameworks>
<OutputType>Exe</OutputType>
<StartupObject>Gherkin.Specs.CLI.Program</StartupObject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
14 changes: 10 additions & 4 deletions dotnet/Gherkin.Specs/Helper/NDJsonParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Utf8Json;
using System.Text.Json;
using System.Text.Json.Serialization;


namespace Gherkin.Specs.Helper;

Expand All @@ -14,7 +14,13 @@ public static List<T> Deserialize<T>(string ndjson)

foreach (var line in lines)
{
var deserializedObject = JsonSerializer.Deserialize<T>(line);
var deserializedObject = JsonSerializer.Deserialize<T>(line, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
}
});
result.Add(deserializedObject);
}

Expand Down
6 changes: 1 addition & 5 deletions dotnet/Gherkin.Specs/Helper/TestFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Gherkin.Specs.Helper;

public class TestFileProvider
Expand All @@ -21,7 +17,7 @@ private static IEnumerable<object[]> GetTestFiles(string category)
string testFileFolder = GetTestFileFolder(category);

return Directory.GetFiles(testFileFolder, "*.feature")
.Where(f => Path.GetFileName(f) != "escaped_pipes.feature") //currently failing, because of https://github.com/neuecc/Utf8Json/pull/96
.Where(f => Path.GetFileName(f) != "escaped_pipes.feature")
.Select(f => new object[] { Path.GetFileName(f) });
}

Expand Down
6 changes: 1 addition & 5 deletions dotnet/Gherkin.Specs/Helper/TestFolders.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.IO;
using System.Reflection;

namespace Gherkin.Specs.Helper;

internal static class TestFolders
Expand All @@ -10,7 +6,7 @@ public static string InputFolder
{
get
{
var inputFolder = Path.GetDirectoryName(typeof(TestFolders).GetTypeInfo().Assembly.Location);
var inputFolder = Environment.CurrentDirectory;

inputFolder = Path.Combine(inputFolder, "..");

Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/PicklesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using FluentAssertions;
using Gherkin.CucumberMessages.Types;
using Gherkin.Specs.Helper;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/SourceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using FluentAssertions;
using Gherkin.CucumberMessages.Types;
using Gherkin.Specs.Helper;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/StringUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Xunit;

namespace Gherkin.Specs;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/SuccessfulParsingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using Gherkin.Specs.Helper;
using Xunit;

Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/TokenizationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.IO;
using Gherkin.Specs.Helper;
using Xunit;

Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin.Specs/Tokens/TestTokenFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;

namespace Gherkin.Specs.Tokens;

class TestTokenFormatter
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin.Specs/Tokens/TokenFormatterBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Text;

namespace Gherkin.Specs.Tokens;
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin.Specs/Tokens/TokensGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.IO;

namespace Gherkin.Specs.Tokens;

public class TokensGenerator
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin/Ast/DataTable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace Gherkin.Ast;

public class DataTable : StepArgument, IHasRows, IHasLocation
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin/Ast/Examples.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Collections.Generic;
using System.Linq;

namespace Gherkin.Ast;

public class Examples(Tag[] tags, Location location, string keyword, string name, string description, TableRow header, TableRow[] body)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/Feature.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public class Feature(Tag[] tags, Location location, string language, string keyword, string name, string description, IHasLocation[] children)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/GherkinDocument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public class GherkinDocument(Feature feature, Comment[] comments)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/IHasChildren.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public interface IHasChildren
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/IHasRows.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public interface IHasRows
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/IHasSteps.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public interface IHasSteps
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/IHasTags.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public interface IHasTags
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/Rule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public class Rule(Tag[] tags, Location location, string keyword, string name, string description, IHasLocation[] children)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/Scenario.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public class Scenario(Tag[] tags, Location location, string keyword, string name, string description, Step[] steps, Examples[] examples)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/StepsContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public abstract class StepsContainer(Location location, string keyword, string name, string description, Step[] steps)
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/Ast/TableRow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Gherkin.Ast;

public class TableRow(Location location, TableCell[] cells) : IHasLocation
Expand Down
5 changes: 1 addition & 4 deletions dotnet/Gherkin/AstBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Gherkin.Ast;

namespace Gherkin;
Expand Down Expand Up @@ -261,7 +258,7 @@ private Tag[] GetTags(AstNode node)
{
var tagsNode = node.GetSingle<AstNode>(RuleType.Tags);
if (tagsNode == null)
return new Tag[0];
return [];

return tagsNode.GetTokens(TokenType.TagLine)
.SelectMany(t => t.MatchedItems, (t, tagItem) =>
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin/AstNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Collections.Generic;
using System.Linq;

namespace Gherkin;

public class AstNode(RuleType ruleType)
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin/CucumberMessages/AstMessagesConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Gherkin.Ast;
using Gherkin.CucumberMessages.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using Background = Gherkin.CucumberMessages.Types.Background;
using Comment = Gherkin.CucumberMessages.Types.Comment;
using DataTable = Gherkin.CucumberMessages.Types.DataTable;
Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/CucumberMessages/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Gherkin.CucumberMessages;

Expand Down
2 changes: 0 additions & 2 deletions dotnet/Gherkin/CucumberMessages/IdGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Gherkin.CucumberMessages;

public interface IIdGenerator
Expand Down
3 changes: 0 additions & 3 deletions dotnet/Gherkin/CucumberMessages/Pickles/PickleCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Gherkin.CucumberMessages.Types;
using System;
using System.Collections.Generic;
using System.Linq;

// ReSharper disable PossibleMultipleEnumeration

Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/Background.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/DataTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/Envelope.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/Examples.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/Feature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/GherkinDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/Pickle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/PickleStep.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/PickleTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
1 change: 0 additions & 1 deletion dotnet/Gherkin/CucumberMessages/Types/PickleTableRow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Gherkin.CucumberMessages.Types;
Expand Down
Loading

0 comments on commit a8ffa89

Please sign in to comment.