Skip to content

Commit

Permalink
some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-hodgson authored Oct 14, 2022
1 parent ba79933 commit 13617d6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Benjamin.Pizza.DocTest/Benjamin.Pizza.DocTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

<PropertyGroup>
<IsPackable>true</IsPackable>
<IsTestProject>false</IsTestProject>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.assert" />
<PackageReference Include="xunit.core" />
</ItemGroup>

</Project>
31 changes: 13 additions & 18 deletions Benjamin.Pizza.DocTest/DocTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.RegularExpressions;

using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;

using Xunit;
Expand All @@ -20,18 +18,22 @@ namespace Benjamin.Pizza.DocTest;
[SuppressMessage("naming", "CA1724", Justification = "DGAF")] // "The type name conflicts in whole or in part with the namespace name"
public class DocTest
{
private readonly Assembly _assembly;
private static readonly Regex _outputRegex = new(@"// Output:\s*", RegexOptions.Compiled);
private static readonly Regex _commentRegex = new(@"^\s*(//( |$))?", RegexOptions.Compiled);
private readonly Script _script;
private readonly string _name;
private readonly string _code;
private readonly string? _preamble;

/// <summary>Constructor</summary>
public DocTest(Assembly assembly, string name, string code, string? preamble)
public DocTest(string name, string code, Script preamble)
{
_assembly = assembly;
_name = name;
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(code);
ArgumentNullException.ThrowIfNull(preamble);

_script = preamble.ContinueWith(code);
_code = code;
_preamble = preamble;
_name = name;
}

/// <summary>ToString</summary>
Expand All @@ -40,23 +42,16 @@ public DocTest(Assembly assembly, string name, string code, string? preamble)
/// <summary>Run the test</summary>
public async Task Run()
{
var (output, error) = await RunDocTest();
var (output, error) = await RedirectConsole(() => _script.RunAsync());
Assert.Equal("", error);
Assert.Equal(GetExpected(), SplitLines(output));
}

private Task<(string output, string error)> RunDocTest()
{
var options = ScriptOptions.Default.AddReferences(_assembly).AddImports("System");

return RedirectConsole(() => CSharpScript.RunAsync(_preamble + _code, options));
}

private IEnumerable<string> GetExpected()
{
var match = new Regex(@"// Output:\s*").Match(_code);
var match = _outputRegex.Match(_code);
return SplitLines(_code[(match.Index + match.Length)..])
.Select(line => new Regex(@"^\s*(//( |$))?").Replace(line, ""));
.Select(line => _commentRegex.Replace(line, ""));
}

private static async Task<(string output, string error)> RedirectConsole(Func<Task> action)
Expand Down
9 changes: 8 additions & 1 deletion Benjamin.Pizza.DocTest/DocTestDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Reflection;
using System.Xml.Linq;

using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;

using Xunit.Sdk;

namespace Benjamin.Pizza.DocTest;
Expand Down Expand Up @@ -30,6 +33,10 @@ public DocTestDataAttribute(Type typeInAssemblyToDoctest)
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var assembly = TypeInAssemblyToDoctest.Assembly;

var options = ScriptOptions.Default.AddReferences(assembly).AddImports("System");
var script = CSharpScript.Create(Preamble ?? "", options);

var path = Path.ChangeExtension(assembly.Location, "xml");
var xml = XDocument.Parse(File.ReadAllText(path));
return (
Expand All @@ -44,7 +51,7 @@ from ex in mem.Descendants()
from c in codes
let name = ex.Attribute("name")!.Value
+ (codes.Count() > 1 ? " > " + c.ix : "")
select new DocTest(assembly, name, c.code, Preamble)
select new DocTest(name, c.code, script)
).Distinct().Select(x => new[] { x });
}
}
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="xunit" Version="2.4.1" />
<PackageVersion Include="xunit.assert" Version="2.4.1" />
<PackageVersion Include="xunit.core" Version="2.4.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
</Project>

0 comments on commit 13617d6

Please sign in to comment.