Skip to content

Commit

Permalink
Add basic scripting unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
natenho committed Dec 31, 2022
1 parent 3c59fdb commit 548c535
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Mockaco.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.Tests", "test\Mockaco.Tests\Mockaco.Tests.csproj", "{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mockaco.AspNetCore", "src\Mockaco.AspNetCore\Mockaco.AspNetCore.csproj", "{7766C592-9887-4162-8B9C-51003ED30335}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.AspNetCore", "src\Mockaco.AspNetCore\Mockaco.AspNetCore.csproj", "{7766C592-9887-4162-8B9C-51003ED30335}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,14 +27,14 @@ Global
{8BAA1EC5-0BF5-4DA2-87F7-ED0C7B652517}.Nuget|Any CPU.Build.0 = Nuget|Any CPU
{8BAA1EC5-0BF5-4DA2-87F7-ED0C7B652517}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BAA1EC5-0BF5-4DA2-87F7-ED0C7B652517}.Release|Any CPU.Build.0 = Release|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Nuget|Any CPU.ActiveCfg = Release|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Release|Any CPU.Build.0 = Release|Any CPU
{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Nuget|Any CPU.ActiveCfg = Debug|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Nuget|Any CPU.Build.0 = Debug|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Nuget|Any CPU.ActiveCfg = Release|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Nuget|Any CPU.Build.0 = Release|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7766C592-9887-4162-8B9C-51003ED30335}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
25 changes: 11 additions & 14 deletions test/Mockaco.Tests/Mockaco.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="Templating\Response\mockaco.jpg" />
<None Remove="Templating\Transforms_Plain_Json_Template.json" />
<None Remove="Templating\Transforms_Scripted_Json_Template.json" />
</ItemGroup>

<ItemGroup>
<Content Include="Templating\Response\mockaco.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Templating\Transforms_Scripted_Json_Template.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Templating\Transforms_Plain_Json_Template.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand All @@ -45,4 +31,15 @@
<ProjectReference Include="..\..\src\Mockaco\Mockaco.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Templating\Transforms_Plain_Json_Template.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templating\Transforms_Scripted_Json_Template.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions test/Mockaco.Tests/Templating/Scripting/ScriptRunnerFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Mockaco.Tests.Templating.Scripting
{
public class ScriptRunnerFactoryTest
{
[Theory]
[InlineData(@"JsonConvert.SerializeObject(new DateTime(2012, 04, 23, 18, 25, 43, 511, DateTimeKind.Utc))", @"\""2012-04-23T18:25:43\.511Z\""")]
[InlineData(@"new PhoneNumbers().BrazilianPhoneNumber()", @"[0-9]+")]
[InlineData(@"new Faker().Random.Guid().ToString()", @"[a-z0-9\-]+")]
[InlineData(@"new Regex(@"".*"").IsMatch(""abc"").ToString()", @"True")]
[InlineData(@"new[] {1, 2, 3, 4, 5, 6, 7}.Count().ToString()", @"7")]
public async Task Can_Run_Scripts_From_Builtin_Namespaces(string input, string regexPattern)
{
var mockLogger = Moq.Mock.Of<ILogger<ScriptRunnerFactory>>();
var mockOptions = Moq.Mock.Of<IOptionsMonitor<MockacoOptions>>(o => o.CurrentValue == new MockacoOptions());

var runner = new ScriptRunnerFactory(mockLogger, mockOptions);

var result = await runner.Invoke<string, string>("", input);

Assert.Matches(regexPattern, result);
}
}
}

0 comments on commit 548c535

Please sign in to comment.