Rename the folder Tests to tests

This commit is contained in:
runebaas 2020-08-13 17:22:18 +02:00
parent 61ce14a61d
commit 97ad54df9e
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
9 changed files with 1 additions and 1 deletions

View file

@ -0,0 +1,94 @@
using Geekbot.Core.Converters;
using Xunit;
namespace Tests.Core.Converters
{
public class EmojiConverterTest
{
public class NumberToEmojiTestDto
{
public int Number { get; set; }
public string Expected { get; set; }
}
public static TestData<NumberToEmojiTestDto> NumberToEmojiTestData =>
new TestData<NumberToEmojiTestDto>
{
{
"2",
new NumberToEmojiTestDto
{
Number = 2,
Expected = ":two:"
}
},
{
"10",
new NumberToEmojiTestDto
{
Number = 10,
Expected = "🔟"
}
},
{
"15",
new NumberToEmojiTestDto
{
Number = 15,
Expected = ":one::five:"
}
},
{
"null",
new NumberToEmojiTestDto
{
Number = 0,
Expected = ":zero:"
}
}
};
[Theory, MemberData(nameof(NumberToEmojiTestData))]
public void NumberToEmoji(string testName, NumberToEmojiTestDto testData)
{
var emojiConverter = new EmojiConverter();
var result = emojiConverter.NumberToEmoji(testData.Number);
Assert.Equal(result, testData.Expected);
}
public class TextToEmojiTestDto
{
public string Text { get; set; }
public string Expected { get; set; }
}
public static TestData<TextToEmojiTestDto> TextToEmojiTestData =>
new TestData<TextToEmojiTestDto>
{
{
"Test",
new TextToEmojiTestDto
{
Text = "test",
Expected = ":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: "
}
},
{
"Best3+?",
new TextToEmojiTestDto
{
Text = "Best3+?",
Expected = ":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: "
}
}
};
[Theory, MemberData(nameof(TextToEmojiTestData))]
public void TextToEmoji(string testName, TextToEmojiTestDto testData)
{
var emojiConverter = new EmojiConverter();
var result = emojiConverter.TextToEmoji(testData.Text);
Assert.Equal(result, testData.Expected);
}
}
}

View file

@ -0,0 +1,208 @@
using System.Collections.Generic;
using System.Text.Json;
using Geekbot.Core.DiceParser;
using Geekbot.Core.RandomNumberGenerator;
using Xunit;
namespace Tests.Core.DiceParser
{
public class DiceParserTest
{
private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator();
public struct DiceParserTestDto
{
public string Input { get; set; }
public DiceInput Expected { get; set; }
}
public static TestData<DiceParserTestDto> DiceParserTestData =>
new TestData<DiceParserTestDto>
{
{
"Empty Input",
new DiceParserTestDto
{
Input = "",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.None
}
}
}
}
},
{
"Simple 1d20 input",
new DiceParserTestDto
{
Input = "1d20",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.None
}
}
}
}
},
{
"2d8 with advantage",
new DiceParserTestDto
{
Input = "+2d8",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 8,
AdvantageType = DieAdvantageType.Advantage
}
}
}
}
},
{
"2d8 with disadvantage",
new DiceParserTestDto
{
Input = "-2d8",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 8,
AdvantageType = DieAdvantageType.Disadvantage
}
}
}
}
},
{
"multiple dice",
new DiceParserTestDto
{
Input = "2d8 2d6",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 8
},
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 6
}
}
}
}
},
{
"with skill modifier, no dice",
new DiceParserTestDto
{
Input = "+2",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20
}
},
SkillModifier = 2
}
}
},
{
"8d6 with total",
new DiceParserTestDto
{
Input = "8d6 total",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 8,
Sides = 6
}
},
Options = new DiceInputOptions
{
ShowTotal = true
}
}
}
},
{
"All posibilities",
new DiceParserTestDto
{
Input = "2d20 +1d20 -1d20 +1 total",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 20
},
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.Advantage
},
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.Disadvantage
},
},
Options = new DiceInputOptions
{
ShowTotal = true
},
SkillModifier = 1
}
}
},
};
[Theory, MemberData(nameof(DiceParserTestData))]
public void DiceParserTestFunc(string testName, DiceParserTestDto testData)
{
var parser = new Geekbot.Core.DiceParser.DiceParser(_randomNumberGenerator);
var result = parser.Parse(testData.Input);
Assert.Equal(JsonSerializer.Serialize(result), JsonSerializer.Serialize(testData.Expected));
}
}
}

View file

@ -0,0 +1,125 @@
using Geekbot.Core.DiceParser;
using Geekbot.Core.RandomNumberGenerator;
using Xunit;
namespace Tests.Core.DiceParser
{
public class SingleDieTest
{
public struct SingleDieNameTestDto
{
public DieAdvantageType AdvantageType { get; set; }
public string Expected { get; set; }
}
public static TestData<SingleDieNameTestDto> SingleDieNameTestData => new TestData<SingleDieNameTestDto>
{
{
"No advantage",
new SingleDieNameTestDto()
{
AdvantageType = DieAdvantageType.None,
Expected = "1d20"
}
},
{
"With advantage",
new SingleDieNameTestDto()
{
AdvantageType = DieAdvantageType.Advantage,
Expected = "1d20 (with advantage)"
}
},
{
"With disadvantage",
new SingleDieNameTestDto()
{
AdvantageType = DieAdvantageType.Disadvantage,
Expected = "1d20 (with disadvantage)"
}
}
};
[Theory, MemberData(nameof(SingleDieNameTestData))]
public void SingleDieNameTestFunc(string testName, SingleDieNameTestDto testData)
{
var die = new SingleDie(new RandomNumberGenerator()) {AdvantageType = testData.AdvantageType};
Assert.Equal(die.DiceName, testData.Expected);
}
public struct SingleDieValidationTestDto
{
public int Sides { get; set; }
public int Amount { get; set; }
public bool PassValidation { get; set; }
}
public static TestData<SingleDieValidationTestDto> SingleDieValidationTestData => new TestData<SingleDieValidationTestDto>
{
{
"To many sides",
new SingleDieValidationTestDto()
{
Amount = 1,
Sides = 200,
PassValidation = false
}
},
{
"To few sides",
new SingleDieValidationTestDto()
{
Amount = 1,
Sides = 1,
PassValidation = false
}
},
{
"To many Dice",
new SingleDieValidationTestDto()
{
Amount = 200,
Sides = 20,
PassValidation = false
}
},
{
"To few Dice",
new SingleDieValidationTestDto()
{
Amount = 0,
Sides = 20,
PassValidation = false
}
},
{
"Correct Dice",
new SingleDieValidationTestDto()
{
Amount = 1,
Sides = 20,
PassValidation = true
}
}
};
[Theory, MemberData(nameof(SingleDieValidationTestData))]
public void SingleDieValidationTestFunc(string testName, SingleDieValidationTestDto testData)
{
var die = new SingleDie(new RandomNumberGenerator())
{
Amount = testData.Amount,
Sides = testData.Sides
};
if (testData.PassValidation)
{
die.ValidateDie();
}
else
{
Assert.Throws<DiceException>(() => die.ValidateDie());
}
}
}
}

View file

@ -0,0 +1,59 @@
using Geekbot.Core.Levels;
using Xunit;
namespace Tests.Core.Levels
{
public class LevelCalcTest
{
public class LevelCalcTestDto
{
public int Messages { get; set; }
public int ExpectedLevel { get; set; }
}
public static TestData<LevelCalcTestDto> LevelCalcTestData =>
new TestData<LevelCalcTestDto>()
{
{
"500",
new LevelCalcTestDto
{
Messages = 500,
ExpectedLevel = 13
}
},
{
"41659",
new LevelCalcTestDto
{
Messages = 41659,
ExpectedLevel = 55
}
},
{
"0",
new LevelCalcTestDto
{
Messages = 0,
ExpectedLevel = 1
}
},
{
"4000000",
new LevelCalcTestDto
{
Messages = 4000000,
ExpectedLevel = 101
}
}
};
[Theory, MemberData(nameof(LevelCalcTestData))]
public void GetLevel(string testName, LevelCalcTestDto testData)
{
var levelCalc = new LevelCalc();
var result = levelCalc.GetLevel(testData.Messages);
Assert.Equal(result, testData.ExpectedLevel);
}
}
}

View file

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using Geekbot.Core.Localization;
using Moq;
using Xunit;
namespace Tests.Core.Localization
{
public class TranslationGuildContext_test
{
public class FormatDateTimeAsRemainingTestDto
{
public DateTimeOffset DateTime { get; set; }
public string Expected { get; set; }
}
public static TestData<FormatDateTimeAsRemainingTestDto> FormatDateTimeAsRemainingData =>
new TestData<FormatDateTimeAsRemainingTestDto>
{
{
"Wait for days",
new FormatDateTimeAsRemainingTestDto
{
DateTime = DateTimeOffset.Now.AddDays(5),
Expected = "4 days, 23 hours, 59 minutes and 59 seconds"
}
},
{
"Wait for minutes",
new FormatDateTimeAsRemainingTestDto
{
DateTime = DateTimeOffset.Now.AddMinutes(5),
Expected = "4 minutes and 59 seconds"
}
},
{
"Wait for seconds",
new FormatDateTimeAsRemainingTestDto
{
DateTime = DateTimeOffset.Now.AddSeconds(5),
Expected = "4 seconds"
}
}
};
[Theory, MemberData(nameof(FormatDateTimeAsRemainingData))]
public void FormatDateTimeAsRemaining(string testName, FormatDateTimeAsRemainingTestDto testData)
{
var translationHandlerMock = new Mock<ITranslationHandler>(MockBehavior.Loose);
translationHandlerMock
.Setup(thm => thm.GetString("EN", "dateTime", "Days"))
.Returns("day|days");
translationHandlerMock
.Setup(thm => thm.GetString("EN", "dateTime", "Hours"))
.Returns("hour|hours");
translationHandlerMock
.Setup(thm => thm.GetString("EN", "dateTime", "Minutes"))
.Returns("minute|minutes");
translationHandlerMock
.Setup(thm => thm.GetString("EN", "dateTime", "Seconds"))
.Returns("second|seconds");
translationHandlerMock
.Setup(thm => thm.GetString("EN", "dateTime", "And"))
.Returns("and");
var context = new TranslationGuildContext(translationHandlerMock.Object, "EN", new Dictionary<string, string>());
var result = context.FormatDateTimeAsRemaining(testData.DateTime);
Assert.Equal(result, testData.Expected);
}
}
}

View file

@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
namespace Tests.Core.Localization
{
public class Translations_test
{
[Fact]
public void TranslationsYamlIsValid()
{
// Read the file
var translationFile = File.ReadAllText(Path.GetFullPath("./../../../../src/Core/Localization/Translations.yml"));
// Deserialize
var input = new StringReader(translationFile);
var mergingParser = new MergingParser(new Parser(input));
var deserializer = new DeserializerBuilder().Build();
var rawTranslations = deserializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(mergingParser);
// These languages must be supported
var supportedLanguages = new List<string>
{
"EN",
"CHDE"
};
// Iterate every single key to make sure it's populated
foreach (var command in rawTranslations)
{
foreach (var str in command.Value)
{
str.Value.Select(e => e.Key).ToList().Should().BeEquivalentTo(supportedLanguages, str.Key);
foreach (var lang in str.Value)
{
lang.Value.Should().NotBeNullOrEmpty($"{command.Key} / {str.Key} / {lang.Key}");
}
}
}
}
}
}

21
tests/TestData.cs Normal file
View file

@ -0,0 +1,21 @@
using Xunit;
namespace Tests
{
/// <summary>
/// Represents a set of data for a theory. Data can be added to the data set using the collection initializer syntax.
/// </summary>
/// <typeparam name="T"></typeparam>
public class TestData<T> : TheoryData
{
/// <summary>
/// Adds a theory to the Test
/// </summary>
/// <param name="testName">Name of the Test</param>
/// <param name="testObject">Data used in the Test</param>
public void Add(string testName, T testObject)
{
AddRow(testName, testObject);
}
}
}

20
tests/Tests.csproj Normal file
View file

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<NoWarn>NU1701</NoWarn>
<NoWarn>xUnit1026</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="YamlDotNet" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Bot\Bot.csproj" />
</ItemGroup>
</Project>