Skip to content

Commit

Permalink
Improve eval command and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bond-009 committed Jan 30, 2021
1 parent 9126e72 commit fc7d8e9
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 12 deletions.
19 changes: 18 additions & 1 deletion iTool.DiscordBot.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.12
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{16EA270D-4D44-44C8-904C-86FF9069D24A}"
EndProject
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "iTool.DiscordBot", "src\iTool.DiscordBot\iTool.DiscordBot.csproj", "{5B92E328-88E3-4012-AFB2-8FE54AD8F3E1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{340DCC9C-2FF5-407A-9D0B-3CCDF2C327D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "iTool.DiscordBot.Tests", "tests\iTool.DiscordBot.Tests\iTool.DiscordBot.Tests.csproj", "{B24C8864-7881-4E3A-B317-28B15466F7E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,11 +32,24 @@ Global
{5B92E328-88E3-4012-AFB2-8FE54AD8F3E1}.Release|x64.Build.0 = Release|Any CPU
{5B92E328-88E3-4012-AFB2-8FE54AD8F3E1}.Release|x86.ActiveCfg = Release|Any CPU
{5B92E328-88E3-4012-AFB2-8FE54AD8F3E1}.Release|x86.Build.0 = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|x64.ActiveCfg = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|x64.Build.0 = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|x86.ActiveCfg = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Debug|x86.Build.0 = Debug|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|Any CPU.Build.0 = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|x64.ActiveCfg = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|x64.Build.0 = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|x86.ActiveCfg = Release|Any CPU
{B24C8864-7881-4E3A-B317-28B15466F7E3}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5B92E328-88E3-4012-AFB2-8FE54AD8F3E1} = {16EA270D-4D44-44C8-904C-86FF9069D24A}
{B24C8864-7881-4E3A-B317-28B15466F7E3} = {340DCC9C-2FF5-407A-9D0B-3CCDF2C327D4}
EndGlobalSection
EndGlobal
67 changes: 56 additions & 11 deletions src/iTool.DiscordBot/Modules/DevModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ public Task CollectGarbage()
[RequireTrustedUser]
public async Task Eval([Remainder] string input)
{
int index1 = input.IndexOf('\n', input.IndexOf("```") + 3) + 1;
int index2 = input.LastIndexOf("```");

if (index1 == -1 || index2 == -1)
if (!TryGetCode(input, out string code))
{
throw new ArgumentException("You need to wrap the code into a code block.");
await ReplyAsync(string.Empty, embed: new EmbedBuilder()
{
Title = "Error",
Color = _settings.GetErrorColor(),
Description = "Syntax Error"
}.Build());
return;
}

string code = input.Substring(index1, index2 - index1);

Task<IUserMessage> msg = ReplyAsync(string.Empty, embed: new EmbedBuilder()
{
Title = "Evaluation",
Expand Down Expand Up @@ -81,13 +82,14 @@ public async Task Eval([Remainder] string input)
"System.Threading.Tasks"
});

result = await CSharpScript.EvaluateAsync(code, options, globals:
new RoslynGlobals()
result = await CSharpScript.EvaluateAsync(
code,
options,
globals: new RoslynGlobals()
{
Client = Context.Client,
Channel = Context.Channel as SocketTextChannel
}
).ConfigureAwait(false);
}).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand All @@ -109,6 +111,49 @@ public async Task Eval([Remainder] string input)
}.Build()).ConfigureAwait(false);
}

internal static bool TryGetCode(string input, out string code)
{
code = string.Empty;

int index1 = input.IndexOf("```");
int index2 = -1;
if (index1 == -1)
{
// Check for inline code block
index1 = input.IndexOf('`');
if (index1 == -1)
{
// No code block found, return plain text
code = input;
return true;
}

index2 = input.LastIndexOf('`');
if (index2 == index1)
{
// No closing backtick
return false;
}

index1++; // Ignore the backtick itself
}
else
{
// Check for code block
index2 = input.LastIndexOf("```");
if (index1 == index2)
{
// No closing backticks
return false;
}

index1 = input.IndexOf('\n', index1 + 3) + 1;
}

code = input[index1..index2].TrimEnd();
return code.Length != 0;
}

public class RoslynGlobals
{
public DiscordSocketClient Client { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions src/iTool.DiscordBot/iTool.DiscordBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@
<EmbeddedResource Include="Resources/Configuration/*" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>iTool.DiscordBot.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
62 changes: 62 additions & 0 deletions tests/iTool.DiscordBot.Tests/Modules/DevModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using Xunit;

namespace iTool.DiscordBot.Modules.Tests
{
public class DevModuleTests
{
[Theory]
[MemberData(nameof(TryGetCode_TestData))]
public void TryGetCodeTests(string input, bool success, string expected)
{
Assert.Equal(success, DevModule.TryGetCode(input, out string code));
Assert.Equal(expected, code.ToString());
}

public static IEnumerable<object[]> TryGetCode_TestData()
{
yield return new object[]
{
@"```cs
return true
```",
true,
"return true",
};
yield return new object[]
{
"`return true`",
true,
"return true",
};
yield return new object[]
{
"return true",
true,
"return true",
};
yield return new object[]
{
@"```cs
return true
",
false,
string.Empty,
};
yield return new object[]
{
"`return true",
false,
string.Empty,
};
yield return new object[]
{
@"```cs return true
```
",
false,
string.Empty,
};
}
}
}
25 changes: 25 additions & 0 deletions tests/iTool.DiscordBot.Tests/iTool.DiscordBot.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../src/iTool.DiscordBot/iTool.DiscordBot.csproj" />
</ItemGroup>

</Project>

0 comments on commit fc7d8e9

Please sign in to comment.