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..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 @@ -374,9 +374,8 @@ 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 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.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); + } + } +} diff --git a/src/System.CommandLine/Parsing/Token.cs b/src/System.CommandLine/Parsing/Token.cs index 5e195215b1..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); @@ -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); } }