Browse Source

Use Moq for mocking objects, write more tests

pull/697/head
FiniteReality 8 years ago
parent
commit
0c5b4f43ca
6 changed files with 88 additions and 42 deletions
  1. +6
    -4
      test/Discord.Net.Commands.Tests/CommandServiceConfigTests.Data.cs
  2. +22
    -18
      test/Discord.Net.Commands.Tests/CommandServiceConfigTests.cs
  3. +45
    -0
      test/Discord.Net.Commands.Tests/CommandServiceTests.cs
  4. +1
    -0
      test/Discord.Net.Commands.Tests/Discord.Net.Commands.Tests.csproj
  5. +0
    -20
      test/Discord.Net.Commands.Tests/DummyCommandContext.cs
  6. +14
    -0
      test/Discord.Net.Commands.Tests/DummyModule.cs

+ 6
- 4
test/Discord.Net.Commands.Tests/CommandServiceConfigTests.Data.cs View File

@@ -16,11 +16,13 @@ namespace Discord.Net.Commands.Tests
new CommandServiceConfig{ SeparatorChar = '"'} new CommandServiceConfig{ SeparatorChar = '"'}
}.Select(x => new object[] { x, x.SeparatorChar }); }.Select(x => new object[] { x, x.SeparatorChar });


public static IEnumerable<string> DefaultAliases => _defaultAliases;
private static readonly IEnumerable<string> _defaultAliases = new List<string>
public static IEnumerable<object[]> CaseSensitivityTestData => _caseSensitivityTestData;
private static readonly IEnumerable<object[]> _caseSensitivityTestData = new object[][]
{ {
"debug ping",
"debug pong"
new object[]{ true, false },
new object[]{ true, true },
new object[]{ false, true },
new object[]{ false, false }
}; };
} }
} }

+ 22
- 18
test/Discord.Net.Commands.Tests/CommandServiceConfigTests.cs View File

@@ -1,4 +1,5 @@
using Discord.Commands; using Discord.Commands;
using Moq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -14,37 +15,40 @@ namespace Discord.Net.Commands.Tests
public async Task CommandSeparators(CommandServiceConfig config, char separatorChar) public async Task CommandSeparators(CommandServiceConfig config, char separatorChar)
{ {
var service = new CommandService(config); var service = new CommandService(config);
var module = await service.AddModuleAsync(typeof(DummyModule)).ConfigureAwait(false);
await service.AddModuleAsync(typeof(DummyModule)).ConfigureAwait(false);


Assert.True(CommandsLoaded(service, separatorChar));
var contextMock = new Mock<ICommandContext>();
var context = contextMock.Object;


var dummyContext = new DummyCommandContext();

foreach (var _alias in DefaultAliases)
foreach (var _name in DummyModule.CommandNames)
{ {
var alias = _alias.Replace(' ', separatorChar);
var name = _name.Replace(' ', separatorChar);


var result = service.Search(dummyContext, alias);
var result = service.Search(context, name);
Assert.True(result.IsSuccess, result.ErrorReason); Assert.True(result.IsSuccess, result.ErrorReason);
} }
} }


public bool CommandsLoaded(CommandService service, char separatorChar)
[Theory]
[MemberData(nameof(CaseSensitivityTestData), MemberType = typeof(CommandServiceConfigTests))]
public async Task CaseSensitivity(bool caseSensitive, bool upperCase)
{ {
if (!service.Commands.Any())
return false;
var service = new CommandService(new CommandServiceConfig { CaseSensitiveCommands = caseSensitive });
await service.AddModuleAsync(typeof(DummyModule)).ConfigureAwait(false);


var loadedAliases = service.Commands.SelectMany(x => x.Aliases);
var contextMock = new Mock<ICommandContext>();
var context = contextMock.Object;


foreach (var alias in DefaultAliases)
foreach (var _name in DummyModule.CommandNames)
{ {
if (!loadedAliases.Contains(alias.Replace(' ', separatorChar)))
{
return false;
}
}
var name = upperCase ? _name.ToUpper() : _name;


return true;
var result = service.Search(context, name);
if (caseSensitive && upperCase)
Assert.False(result.IsSuccess, $"Searching for `{name}` returned successfully");
else
Assert.True(result.IsSuccess, result.ErrorReason);
}
} }
} }
} }

+ 45
- 0
test/Discord.Net.Commands.Tests/CommandServiceTests.cs View File

@@ -0,0 +1,45 @@
using Discord.Commands;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace Discord.Net.Commands.Tests
{
class CommandServiceTests
{
[Fact]
public async Task CommandsLoad()
{
var service = new CommandService();

var module = await service.AddModuleAsync(typeof(DummyModule)).ConfigureAwait(false);
Assert.NotNull(module);

var commandAliases = module.Commands.SelectMany(x => x.Aliases);

foreach (var name in DummyModule.CommandNames)
{
Assert.True(commandAliases.Contains(name), $"The loaded module did not contain the command {name}");
}
}

[Fact]
public async Task MultipleLoadsThrows()
{
var service = new CommandService();

var module = await service.AddModuleAsync(typeof(DummyModule)).ConfigureAwait(false);
await Assert.ThrowsAsync<ArgumentException>(() => service.AddModuleAsync(typeof(DummyModule)))
.ConfigureAwait(false);
}

[Fact]
public async Task InvalidTypeThrows()
{
var service = new CommandService();
await Assert.ThrowsAsync<InvalidOperationException>(() => service.AddModuleAsync(typeof(CommandServiceTests)))
.ConfigureAwait(false);
}
}
}

+ 1
- 0
test/Discord.Net.Commands.Tests/Discord.Net.Commands.Tests.csproj View File

@@ -12,6 +12,7 @@


<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq" Version="4.7.25" />
<PackageReference Include="xunit" Version="2.2.0" /> <PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup> </ItemGroup>


+ 0
- 20
test/Discord.Net.Commands.Tests/DummyCommandContext.cs View File

@@ -1,20 +0,0 @@
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Text;

namespace Discord.Net.Commands.Tests
{
class DummyCommandContext : ICommandContext
{
public IDiscordClient Client { get; set; }

public IGuild Guild { get; set; }

public IMessageChannel Channel { get; set; }

public IUser User { get; set; }

public IUserMessage Message { get; set; }
}
}

+ 14
- 0
test/Discord.Net.Commands.Tests/DummyModule.cs View File

@@ -20,5 +20,19 @@ namespace Discord.Net.Commands.Tests
{ {
return Task.Delay(0); return Task.Delay(0);
} }

//NOTE: do not add this to CommandNames: it is intentional for this command to not be loaded!
[Command("doesNotLoad")]
private Task DoesNotLoadAsync()
{
return Task.Delay(0);
}

public static IEnumerable<string> CommandNames => _commandNames;
private static readonly IEnumerable<string> _commandNames = new List<string>
{
"debug ping",
"debug pong"
};
} }
} }

Loading…
Cancel
Save