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

chore: namespace type cleanup and tests #175

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Tests/WalletConnectSharp.Sign.Test/NamespaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Xunit;
using WalletConnectSharp.Sign.Models;

namespace WalletConnectSharp.Sign.Test;

public class NamespaceTests
{
[Fact, Trait("Category", "unit")]
public void WithMethod_AppendsNewMethod()
{
var ns = new Namespace();
ns.WithMethod("newMethod");

Assert.Contains("newMethod", ns.Methods);
}

[Fact, Trait("Category", "unit"), Trait("Category", "unit")]
public void WithChain_AppendsNewChain_WhenChainsIsNull()
{
var ns = new Namespace();
ns.WithChain("newChain");

Assert.Contains("newChain", ns.Chains);
}

[Fact, Trait("Category", "unit"), Trait("Category", "unit")]
public void WithChain_AppendsNewChain_WhenChainsIsNotNull()
{
var ns = new Namespace();
ns.WithChain("existingChain");
ns.WithChain("newChain");

Assert.Contains("newChain", ns.Chains);
}

[Fact, Trait("Category", "unit")]
public void WithEvent_AppendsNewEvent_WhenEventsIsNull()
{
var ns = new Namespace();
ns.WithEvent("newEvent");

Assert.Contains("newEvent", ns.Events);
}

[Fact, Trait("Category", "unit")]
public void WithEvent_AppendsNewEvent_WhenEventsIsNotNull()
{
var ns = new Namespace();
ns.WithEvent("existingEvent");
ns.WithEvent("newEvent");

Assert.Contains("newEvent", ns.Events);
}

[Fact, Trait("Category", "unit")]
public void WithAccount_AppendsNewAccount_WhenAccountsIsNull()
{
var ns = new Namespace();
ns.WithAccount("newAccount");

Assert.Contains("newAccount", ns.Accounts);
}

[Fact, Trait("Category", "unit")]
public void WithAccount_AppendsNewAccount_WhenAccountsIsNotNull()
{
var ns = new Namespace();
ns.WithAccount("existingAccount");
ns.WithAccount("newAccount");

Assert.Contains("newAccount", ns.Accounts);
}

[Fact, Trait("Category", "unit")]
public void Equals_ReturnsTrue_WhenNamespacesAreIdentical()
{
var ns1 = new Namespace();
ns1.WithMethod("method").WithChain("chain").WithEvent("event").WithAccount("account");

var ns2 = new Namespace();
ns2.WithMethod("method").WithChain("chain").WithEvent("event").WithAccount("account");

Assert.True(ns1.Equals(ns2));
}

[Fact, Trait("Category", "unit")]
public void Equals_ReturnsFalse_WhenNamespacesAreDifferent()
{
var ns1 = new Namespace();
ns1.WithMethod("method1").WithChain("chain1").WithEvent("event1").WithAccount("account1");

var ns2 = new Namespace();
ns2.WithMethod("method2").WithChain("chain2").WithEvent("event2").WithAccount("account2");

Assert.False(ns1.Equals(ns2));
}
}
34 changes: 22 additions & 12 deletions WalletConnectSharp.Sign/Models/Namespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WalletConnectSharp.Sign.Models
/// A namespace that holds accounts, methods and events enabled. Also includes
/// extension namespaces that are enabled
/// </summary>
public class Namespace
public sealed class Namespace
{
public Namespace(ProposedNamespace proposedNamespace)
{
Expand All @@ -22,13 +22,13 @@ public Namespace() { }
/// </summary>
[JsonProperty("accounts")]
public string[] Accounts;

/// <summary>
/// An array of all methods enabled in this namespace
/// </summary>
[JsonProperty("methods")]
public string[] Methods;

/// <summary>
/// An array of all events enabled in this namespace
/// </summary>
Expand All @@ -42,36 +42,45 @@ public Namespace() { }

public Namespace WithMethod(string method)
{
Methods = Methods.Append(method).ToArray();
Methods = Methods == null
? [method]
: Methods.Append(method).ToArray();
return this;
}

public Namespace WithChain(string chain)
{
Chains = Chains.Append(chain).ToArray();
Chains = Chains == null
? [chain]
: Chains.Append(chain).ToArray();
return this;
}

public Namespace WithEvent(string @event)
{
Events = Events.Append(@event).ToArray();
Events = Events == null
? [@event]
: Events.Append(@event).ToArray();
return this;
}

public Namespace WithAccount(string account)
{
Accounts = Accounts.Append(account).ToArray();
Accounts = Accounts == null
? [account]
: Accounts.Append(account).ToArray();
return this;
}
protected bool ArrayEquals(string[] a, string[] b)

protected static bool ArrayEquals(string[] a, string[] b)
{
return a.Length == b.Length && a.All(b.Contains) && b.All(a.Contains);
}

protected bool Equals(Namespace other)
{
return ArrayEquals(Accounts, other.Accounts) && ArrayEquals(Methods, other.Methods) && ArrayEquals(Events, other.Events);
return ArrayEquals(Accounts, other.Accounts) && ArrayEquals(Methods, other.Methods) &&
ArrayEquals(Events, other.Events);
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -123,7 +132,8 @@ public bool Equals(Namespace x, Namespace y)
return false;
}

return x.Accounts.SequenceEqual(y.Accounts) && x.Methods.SequenceEqual(y.Methods) && x.Events.SequenceEqual(y.Events);
return x.Accounts.SequenceEqual(y.Accounts) && x.Methods.SequenceEqual(y.Methods) &&
x.Events.SequenceEqual(y.Events);
}

public int GetHashCode(Namespace obj)
Expand Down
17 changes: 10 additions & 7 deletions WalletConnectSharp.Sign/Models/ProposedNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace WalletConnectSharp.Sign.Models
/// <summary>
/// A required namespace that holds chains, methods and events enabled.
/// </summary>
public class ProposedNamespace
public sealed class ProposedNamespace
{
/// <summary>
/// A list of all chains that are required to be enabled in this namespace
/// </summary>
[JsonProperty("chains")]
public string[] Chains;

/// <summary>
/// A list of all methods that are required to be enabled in this namespace
/// </summary>
[JsonProperty("methods")]
public string[] Methods;

/// <summary>
/// A list of all events that are required to be enabled in this namespace
/// </summary>
Expand Down Expand Up @@ -58,7 +58,7 @@ public ProposedNamespace WithMethod(string method)
Methods = Methods.Append(method).ToArray();
return this;
}

/// <summary>
/// Add an event as required in this namespace
/// </summary>
Expand All @@ -82,7 +82,8 @@ protected bool ArrayEquals(string[] a, string[] b)

protected bool Equals(ProposedNamespace other)
{
return ArrayEquals(Chains, other.Chains) && ArrayEquals(Methods, other.Methods) && ArrayEquals(Events, other.Events);
return ArrayEquals(Chains, other.Chains) && ArrayEquals(Methods, other.Methods) &&
ArrayEquals(Events, other.Events);
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -134,7 +135,8 @@ public bool Equals(ProposedNamespace x, ProposedNamespace y)
return false;
}

return x.Chains.SequenceEqual(y.Chains) && x.Methods.SequenceEqual(y.Methods) && x.Events.SequenceEqual(y.Events);
return x.Chains.SequenceEqual(y.Chains) && x.Methods.SequenceEqual(y.Methods) &&
x.Events.SequenceEqual(y.Events);
}

public int GetHashCode(ProposedNamespace obj)
Expand All @@ -143,6 +145,7 @@ public int GetHashCode(ProposedNamespace obj)
}
}

public static IEqualityComparer<ProposedNamespace> RequiredNamespaceComparer { get; } = new RequiredNamespaceEqualityComparer();
public static IEqualityComparer<ProposedNamespace> RequiredNamespaceComparer { get; } =
new RequiredNamespaceEqualityComparer();
}
}
Loading