From 792c003908becdc47cde7e789d7afacc8b23559c Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 7 Mar 2023 20:50:26 +0100 Subject: [PATCH 1/3] remove Token equality operators to reduce public API size, we were also not using it anywhere --- ...m_CommandLine_api_is_not_changed.approved.txt | 2 -- src/System.CommandLine/Parsing/Token.cs | 16 ---------------- 2 files changed, 18 deletions(-) diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index 9688ad3dcf..a3b93785f7 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -374,8 +374,6 @@ System.CommandLine.Parsing public T GetValue(Argument argument) public T GetValue(Option option) public class Token, System.IEquatable - public static System.Boolean op_Equality(Token left, Token right) - public static System.Boolean op_Inequality(Token left, Token right) .ctor(System.String value, TokenType type, System.CommandLine.Symbol symbol) public TokenType Type { get; } public System.String Value { get; } diff --git a/src/System.CommandLine/Parsing/Token.cs b/src/System.CommandLine/Parsing/Token.cs index 5e195215b1..db390dd6d0 100644 --- a/src/System.CommandLine/Parsing/Token.cs +++ b/src/System.CommandLine/Parsing/Token.cs @@ -59,21 +59,5 @@ internal Token(string? value, TokenType type, Symbol? symbol, int position) /// public override string ToString() => Value; - - /// - /// Checks if two specified instances have the same value. - /// - /// The first . - /// The second . - /// if the objects are equal. - public static bool operator ==(Token? left, Token? right) => left is null ? right is null : left.Equals(right); - - /// - /// Checks if two specified instances have different values. - /// - /// The first . - /// The second . - /// if the objects are not equal. - public static bool operator !=(Token? left, Token? right) => left is null ? right is not null : !left.Equals(right); } } From 84b4a21baa8b220163f087239e27fe86c8ef248e Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 7 Mar 2023 21:01:22 +0100 Subject: [PATCH 2/3] make Token property public, as it's set by a public ctor --- ...ovalTests.System_CommandLine_api_is_not_changed.approved.txt | 1 + src/System.CommandLine/Parsing/Token.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index a3b93785f7..6fba4ad08c 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -375,6 +375,7 @@ System.CommandLine.Parsing public T GetValue(Option option) public class Token, System.IEquatable .ctor(System.String value, TokenType type, System.CommandLine.Symbol symbol) + public System.CommandLine.Symbol Symbol { get; } public TokenType Type { get; } public System.String Value { get; } public System.Boolean Equals(System.Object obj) diff --git a/src/System.CommandLine/Parsing/Token.cs b/src/System.CommandLine/Parsing/Token.cs index db390dd6d0..ea4b0fb1e5 100644 --- a/src/System.CommandLine/Parsing/Token.cs +++ b/src/System.CommandLine/Parsing/Token.cs @@ -46,7 +46,7 @@ internal Token(string? value, TokenType type, Symbol? symbol, int position) /// /// The Symbol represented by the token (if any). /// - internal Symbol? Symbol { get; set; } + public Symbol? Symbol { get; internal set; } /// public override bool Equals(object? obj) => Equals(obj as Token); From fb0876b0ddb86da9be7904c63a5e51a49e8fe941 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 7 Mar 2023 21:05:35 +0100 Subject: [PATCH 3/3] add tests for Token --- src/System.CommandLine.Tests/TokenTests.cs | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/System.CommandLine.Tests/TokenTests.cs diff --git a/src/System.CommandLine.Tests/TokenTests.cs b/src/System.CommandLine.Tests/TokenTests.cs new file mode 100644 index 0000000000..50e35964a6 --- /dev/null +++ b/src/System.CommandLine.Tests/TokenTests.cs @@ -0,0 +1,53 @@ +using FluentAssertions; +using System.CommandLine.Parsing; +using Xunit; + +namespace System.CommandLine.Tests +{ + public class TokenTests + { + [Fact] + public void Tokens_are_equal_when_they_use_same_value_type_and_symbol() + { + Option count = new ("--count"); + Token token = new (count.Name, TokenType.Option, count); + Token same = new (count.Name, TokenType.Option, count); + + token.Equals(same).Should().BeTrue(); + token.Equals((object)same).Should().BeTrue(); + } + + [Fact] + public void Tokens_are_not_equal_when_they_do_not_use_same_value_type_and_symbol() + { + Option symbol = new("--count"); + Option symbolWithSameName = new("--count"); + + Token token = new(symbol.Name, TokenType.Option, symbol); + Token differentValue = new("different", TokenType.Option, symbol); + Token differentType = new(symbol.Name, TokenType.Argument, symbol); + Token differentSymbol = new(symbol.Name, TokenType.Option, symbolWithSameName); + + Assert(token, differentValue); + Assert(token, differentType); + Assert(token, differentSymbol); + Assert(token, null); + + static void Assert(Token token, Token different) + { + token.Equals(different).Should().BeFalse(); + token.Equals((object)different).Should().BeFalse(); + } + } + + [Fact] + public void Symbol_property_returns_value_provided_in_constructor() + { + Option symbol = new("--count"); + + Token token = new(symbol.Name, TokenType.Option, symbol); + + token.Symbol.Should().Be(symbol); + } + } +}