Skip to content

Commit

Permalink
Add some player wrapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 1, 2024
1 parent 60238f4 commit 5792633
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
8 changes: 6 additions & 2 deletions GangsAPI/Data/PlayerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public PlayerWrapper(ulong steam, string? name) {
public PlayerWrapper WithFlags(params string[] flags) {
var flagMap = new Dictionary<string, HashSet<string>>();
foreach (var flag in flags) {
if (!flag.StartsWith(USER_CHAR) && !flag.StartsWith(GROUP_CHAR))
if (!flag.StartsWith(USER_CHAR))
throw new ArgumentException(
$"Expected flag ${flag} to start with {USER_CHAR} or {GROUP_CHAR}");
$"Expected flag ${flag} to start with {USER_CHAR}");

var slashIndex = flag.IndexOf('/', StringComparison.Ordinal);

Expand Down Expand Up @@ -76,6 +76,10 @@ public bool HasFlags(params string[] flags) {
var domain = flag[1..slashIndex];
var permission = flag[(slashIndex + 1)..];

if (permission.Length == 0)
throw new ArgumentException(
$"Expected flag ${flag} to contain a permission after / character");

if (!Data.Flags.TryGetValue(domain, out var perms)) return false;
if (perms.Contains("root")) return true;
if (!perms.Any(p => permission.StartsWith(p))) return false;
Expand Down
30 changes: 20 additions & 10 deletions GangsTest/API/PlayerWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ public void PlayerWrapper_Init_Flags() {
Assert.True(player.Data.Flags.ContainsKey("test"));
}

[Fact]
public void PlayerWrapper_Init_InvalidFlag() {
Assert.ThrowsAny<ArgumentException>(() => testPlayer.WithFlags("invalid"));
Assert.ThrowsAny<ArgumentException>(()
=> testPlayer.WithFlags(GROUP_CHAR + "invalid"));
Assert.ThrowsAny<ArgumentException>(() => testPlayer.WithFlags("_invalid"));
Assert.ThrowsAny<ArgumentException>(()
=> testPlayer.WithFlags(USER_CHAR + "test"));
Assert.ThrowsAny<ArgumentException>(()
=> testPlayer.WithFlags(USER_CHAR + "test/"));
[Theory]
[InlineData("test/flag")]
[InlineData("test/flag/child")]
[InlineData("#test/flag")]
[InlineData("_test/flag")]
[InlineData("@test")]
[InlineData("@test/")]
public void PlayerWrapper_Init_InvalidFlag(string flag) {
Assert.ThrowsAny<ArgumentException>(() => testPlayer.WithFlags(flag));
}

[Theory]
[InlineData("test/flag")]
[InlineData("test/flag/child")]
[InlineData("#test/flag")]
[InlineData("_test/flag")]
[InlineData("@test")]
[InlineData("@test/")]
public void PlayerWrapper_Test_InvalidFlag(string flag) {
Assert.ThrowsAny<ArgumentException>(() => testPlayer.HasFlags(flag));
}

[Fact]
Expand Down

0 comments on commit 5792633

Please sign in to comment.