feat: add tests for definitions

This commit is contained in:
Anna 2020-09-04 14:00:46 -04:00
parent e688cd32e7
commit 983b96af5f
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
11 changed files with 329 additions and 1 deletions

View File

@ -0,0 +1,25 @@
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests.Chat {
public class FreeCompany : DefinitionTest {
public FreeCompany(DefinitionsFixture fixture) {
this.Def = fixture.defs.Chat["free_company"];
}
public static object[][] DataPositives => DefUtils.DataFromMessages(new TestMessage[] {
new TestMessage(ChatType.Shout, "<LUL> is recruiting! Join our community blah blah discord lul"),
});
public static object[][] DataNegatives => DefUtils.DataFromMessages(new TestMessage[] {
new TestMessage(ChatType.Say, "<LUL> is recruiting! Join our community blah blah discord lul"),
});
[Theory]
[MemberData(nameof(DataPositives))]
public void Positives(TestMessage message) => this.Check(message, CheckType.Positive);
[Theory]
[MemberData(nameof(DataNegatives))]
public void Negatives(TestMessage message) => this.Check(message, CheckType.Negative);
}
}

View File

@ -0,0 +1,26 @@
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests.Chat {
public class RMT : DefinitionTest {
public RMT(DefinitionsFixture fixture) {
this.Def = fixture.defs.Chat["rmt"];
}
public static object[][] DataPositives => DefUtils.DataFromMessages(new TestMessage[] {
new TestMessage(ChatType.Shout, "FF14Mog.com selling cheap Mog Station Redeem Code,Dirndl's Attire $8.99, Chocobo Carriage $14.39 ,Use 5Off Code:FF5"),
new TestMessage(ChatType.Say, "----[4KGOLD.COM]----[Best Buy Gil Store]----[Cheapest Price]-----[4KGOLD.COM]---[Ultrafast Deliveryin 10 Mins]--[6OFF Code;LOVE]---359qe"),
});
//public static object[][] DataNegatives => DefUtils.DataFromMessages(new TestMessage[] {
//});
[Theory]
[MemberData(nameof(DataPositives))]
public void Positives(TestMessage message) => this.Check(message, CheckType.Positive);
//[Theory]
//[MemberData(nameof(DataNegatives))]
//public void Negatives(TestMessage message) => this.Check(message, CheckType.Negative);
}
}

View File

@ -0,0 +1,29 @@
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests {
public abstract class DefinitionTest : IClassFixture<DefinitionsFixture> {
protected Definition Def { get; set; }
protected void Check(string message, CheckType type) => this.Def.Check(message, type);
protected void Check(TestMessage message, CheckType type) => this.Def.Check(message, type);
// an assortment of normal party finders/messages to make sure no crazy false positives are happening
public static object[][] DataGlobalNegatives = DefUtils.DataFromMessages(new TestMessage[] {
// party finders
new TestMessage("Static LF the listed roles in prep for 5.4 || T/W/Th 7:30-9:30 AM EST || Discord: Mia#0585"),
new TestMessage("Looking to learn second half of the fight, and then if all goes well farm!!"),
new TestMessage("T/N H/S Tethers DPS Towers KB prevention dps uptime, Partners for the rest no salt farm party"),
new TestMessage("another day another camp on the finder! u know the drill by now bahaprog or nael clean up "),
new TestMessage("♡Need help with anything?♥ Chat, hangout, and can craft! Levekits, gear, & more!☂ Pop in, take a seat, & maybe I can help! :)"),
// messages
new TestMessage(ChatType.FreeCompany, "forgot to leave a commendation, I forget this sometimes"),
new TestMessage(ChatType.StandardEmote, "Anri Valmet laughs at Kyoya Kazuki."),
new TestMessage(ChatType.Shout, "Need a lvl 80 crafter? Buy a leve kit and get your crafter today! PST for price info :D"),
});
[Theory]
[MemberData(nameof(DataGlobalNegatives))]
public void GlobalNegatives(TestMessage msg) => this.Check(msg, CheckType.Negative);
}
}

View File

@ -0,0 +1,56 @@
using Dalamud.Game.Chat;
using System.IO;
using System.Linq;
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests {
public class DefUtils {
public static object[][] DataFromStrings(string[] strings) => strings.Select(s => new object[] { s }).ToArray();
public static object[][] DataFromMessages(TestMessage[] messages) => messages.Select(m => new object[] { m }).ToArray();
}
public class DefinitionsFixture {
internal readonly Definitions defs = Definitions.Load(File.ReadAllText("../../../NoSoliciting/definitions.yaml"));
}
public class TestMessage {
internal ChatType channel;
internal string content;
public TestMessage(string content) : this(ChatType.None, content) { }
public TestMessage(ChatType channel, string content) {
this.content = content;
this.channel = channel;
}
public override string ToString() {
string name = channel == ChatType.None ? "PF" : channel.ToString();
return $"[{name}] {this.content}";
}
}
public enum CheckType {
Positive,
Negative,
}
internal static class DefinitionExt {
internal static void Check(this Definition def, string message, CheckType type) {
TestMessage testMsg = new TestMessage(message);
def.Check(testMsg, type);
}
internal static void Check(this Definition def, TestMessage message, CheckType type) {
switch (type) {
case CheckType.Positive:
Assert.True(def.Matches((XivChatType)message.channel, message.content), message.content);
break;
case CheckType.Negative:
Assert.False(def.Matches((XivChatType)message.channel, message.content), message.content);
break;
}
}
}
}

View File

@ -0,0 +1,31 @@
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests.PartyFinder {
public class RMT : DefinitionTest {
public RMT(DefinitionsFixture fixture) {
this.Def = fixture.defs.PartyFinder["rmt"];
}
public static object[][] DataNegatives => DefUtils.DataFromStrings(new string[] {
"Doing Art commission of your charactet with good price! more info add me on discord: d0uglaz#7409 ♥",
"Selling HQ 490 DoH/DoL sets, just in time for the Ishgard restoration project. Cheaper than MB, Whipser or join for info.",
"Looking to sell medium odder otter walls(2mil) join or tell.",
@"Selling 1x Eldthurs Horn for 8mil. Skip MB taxes \o/ Join if interested",
});
public static object[][] DataPositives => DefUtils.DataFromStrings(new string[] {
"「Best Prices」《 Shiva Unreal ★ Warrior of Light ★ Ultimates ★ Eden's Verse i500/i505",
"「 MINMAXØ 」 SALES ≪ ❶ Savage 一 ❷ Ultimates 一 ❸ Mounts Etc. ≫ World #1 teams, instant delivery. Discord → azrael#6447",
"「」™️ Found it Cheaper? We will beat it! $elling EdenVerse, BLU, Ultimates, Primals, Discord: Valentine#5943",
"「BiS」Selling 》Sac EX, Unreal Shiva 》Savages, Ultimates 》Mounts & More | Price Match Guarantee | Discord→Present#0148",
});
[Theory]
[MemberData(nameof(DataNegatives))]
public void Negatives(string msg) => this.Check(msg, CheckType.Negative);
[Theory]
[MemberData(nameof(DataPositives))]
public void Positives(string msg) => this.Check(msg, CheckType.Positive);
}
}

View File

@ -0,0 +1,27 @@
using Xunit;
namespace NoSoliciting.Tests.DefinitionsTests.Global {
public class Roleplay : DefinitionTest {
public Roleplay(DefinitionsFixture fixture) {
this.Def = fixture.defs.Global["roleplay"];
}
public static object[][] DataPositives => DefUtils.DataFromStrings(new string[] {
"If you're looking for something to do, come find monthly contests, 4 weekly RP events and more! discord.gg/LuckySevens",
"Thorned Dragon Cosplay Event Sunday 9/6! Website for more info: tdevent.carrd.co Discord: discord.gg/thorneddragonclub",
"[Rp] The Viridian Orchid is a non-profit companion's den seeking those interested in joining our community! <viriorchid.carrd.co>",
"[RP] The Thonrned Dragon Cosplay Contest entry closes tonight at midnight est. Entry is free. Full details at [discord.gg/48R4RpP]",
"● The Pearl ● a Victorian Brothel opening Tuesday! Applications closing tonight, get yours in now! [ thepearlxiv.carrd.co ]",
"[RP] Looking for adventure? Bounties, work, odd jobs? Join Bounty Call! More info in our discord: https://discord.gg/SnjZWRf",
"[RP/+18RP] - The HROTHEL - Bathhouse & Bar [LGBTQ+Friendly]\n[Fam. W.5 - P.50 Gridania][discord.io/hrothel] Hrothgar Operated",
"( RP ) Wonderlust is a new club on Midgar! We are hiring! We also want our customers to join! https://discord.gg/Mn3QNn",
"Twine Trolley Hostel is now open in Mist, 3rd Ward (Exodus) apt#88. Please be respectful of other guests and enjoy your stay.",
"DLITE Is OPEN! Come grab a courtesan and relax in our lounge and let our expert staff see to your every whim, SIren, gob W19,43",
"If you're looking for something to do, come find monthly contests, 4 weekly RP events and more! discord.gg/LuckySevens",
});
[Theory]
[MemberData(nameof(DataPositives))]
public void Positives(string msg) => this.Check(msg, CheckType.Positive);
}
}

View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.4.3\build\net452\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.4.3\build\net452\xunit.runner.visualstudio.props')" />
<Import Project="..\packages\xunit.core.2.4.1\build\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.4.1\build\xunit.core.props')" />
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1962D91F-543A-4214-88FD-788BB7ACECE3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NoSoliciting.Tests</RootNamespace>
<AssemblyName>NoSoliciting.Tests</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dalamud">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\Dalamud.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.3\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
<Reference Include="xunit.assert, Version=2.4.1.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.4.1\lib\netstandard1.1\xunit.assert.dll</HintPath>
</Reference>
<Reference Include="xunit.core, Version=2.4.1.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.4.1\lib\net452\xunit.core.dll</HintPath>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.4.1.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.execution.2.4.1\lib\net452\xunit.execution.desktop.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DefinitionsTests.cs" />
<Compile Include="DefinitionsTests.Roleplay.cs" />
<Compile Include="DefinitionTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="DefinitionsTests.PartyFinder.RMT.cs" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NoSoliciting\NoSoliciting.csproj">
<Project>{e4c12987-9064-4788-8783-be418b2c0142}</Project>
<Name>NoSoliciting</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\xunit.analyzers.0.10.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets'))" />
<Error Condition="!Exists('..\packages\xunit.core.2.4.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.4.1\build\xunit.core.props'))" />
<Error Condition="!Exists('..\packages\xunit.core.2.4.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.4.1\build\xunit.core.targets'))" />
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.4.3\build\net452\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.4.3\build\net452\xunit.runner.visualstudio.props'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" />
<Import Project="..\packages\xunit.core.2.4.1\build\xunit.core.targets" Condition="Exists('..\packages\xunit.core.2.4.1\build\xunit.core.targets')" />
</Project>

View File

@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("NoSoliciting.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NoSoliciting.Tests")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("1962d91f-543a-4214-88fd-788bb7acece3")]
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="2.1.1" targetFramework="net48" />
<package id="MSTest.TestFramework" version="2.1.1" targetFramework="net48" />
<package id="xunit" version="2.4.1" targetFramework="net48" />
<package id="xunit.abstractions" version="2.0.3" targetFramework="net48" />
<package id="xunit.analyzers" version="0.10.0" targetFramework="net48" />
<package id="xunit.assert" version="2.4.1" targetFramework="net48" />
<package id="xunit.core" version="2.4.1" targetFramework="net48" />
<package id="xunit.extensibility.core" version="2.4.1" targetFramework="net48" />
<package id="xunit.extensibility.execution" version="2.4.1" targetFramework="net48" />
<package id="xunit.runner.visualstudio" version="2.4.3" targetFramework="net48" developmentDependency="true" />
</packages>

View File

@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoSoliciting.Tests", "NoSoliciting.Tests\NoSoliciting.Tests.csproj", "{1962D91F-543A-4214-88FD-788BB7ACECE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -20,6 +22,10 @@ Global
{E4C12987-9064-4788-8783-BE418B2C0142}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4C12987-9064-4788-8783-BE418B2C0142}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4C12987-9064-4788-8783-BE418B2C0142}.Release|Any CPU.Build.0 = Release|Any CPU
{1962D91F-543A-4214-88FD-788BB7ACECE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1962D91F-543A-4214-88FD-788BB7ACECE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1962D91F-543A-4214-88FD-788BB7ACECE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1962D91F-543A-4214-88FD-788BB7ACECE3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -47,7 +47,7 @@ namespace NoSoliciting {
return defs ?? await CacheOrDefault(plugin).ConfigureAwait(true);
}
private static Definitions Load(string text) {
public static Definitions Load(string text) {
var de = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new MatcherConverter())