Skip to content

Commit 3249c62

Browse files
Validate generated graphs with mermaid
Using the original JS methods
1 parent 979344e commit 3249c62

File tree

2 files changed

+667
-368
lines changed

2 files changed

+667
-368
lines changed

mermaid-graphTests/CommandsTests.cs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,31 @@
44

55
namespace MermaidGraph.Tests;
66

7-
[TestFixture()]
7+
[TestFixture]
88
public class CommandsTests
99
{
10-
[Test()]
10+
private V8ScriptEngine? _engine;
11+
private V8ScriptEngine Js {
12+
get
13+
{
14+
if (_engine is null)
15+
{
16+
_engine ??= new V8ScriptEngine();
17+
_engine.Execute(File.ReadAllText("js\\mermaid.min.js"));
18+
_engine.Script.mermaid.initialize();
19+
}
20+
21+
return _engine;
22+
}
23+
}
24+
25+
[OneTimeTearDown]
26+
public void Disposal()
27+
{
28+
_engine?.Dispose();
29+
}
30+
31+
[Test]
1132
public void DogFoodSolutionTest()
1233
{
1334
var solutionPath = FindFileDownTree("*.sln");
@@ -17,12 +38,14 @@ public void DogFoodSolutionTest()
1738
var graph = new Commands().Solution(info);
1839

1940
Console.WriteLine(graph);
41+
2042
var graphType = DetectType(ExtractMermaid(graph));
43+
Assert.That(graphType, Is.EqualTo("class"));
2144
Console.WriteLine(graphType);
2245
}
2346

24-
[Test()]
25-
public async Task DogFoodProjectTestAsync()
47+
[Test]
48+
public void DogFoodProjectTestAsync()
2649
{
2750
var filePath = FindFileDownTree("*.csproj");
2851
Assert.That(filePath, Is.Not.Null);
@@ -32,29 +55,28 @@ public async Task DogFoodProjectTestAsync()
3255

3356
Console.WriteLine(graph);
3457

35-
var graphType = await MermaidRender(ExtractMermaid(graph));
58+
var graphType = DetectType(ExtractMermaid(graph));
59+
Assert.That(graphType, Is.EqualTo("class"));
3660
Console.WriteLine(graphType);
3761
}
3862

39-
[Test()]
63+
[Test]
4064
public void CommandLineProjectTest()
4165
{
4266
var filePath = FindFileDownTree("*.csproj");
4367
Assert.That(filePath, Is.Not.Null);
44-
4568
Assert.That(Program.Main(filePath), Is.EqualTo(0));
4669
}
4770

48-
[Test()]
71+
[Test]
4972
public void CommandLineSolutionTest()
5073
{
5174
var filePath = FindFileDownTree("*.sln");
5275
Assert.That(filePath, Is.Not.Null);
53-
5476
Assert.That(Program.Main(filePath), Is.EqualTo(0));
5577
}
5678

57-
[Test()]
79+
[Test]
5880
[TestCase(null, 1)]
5981
[TestCase("File Not Found", 2)]
6082
[TestCase("mermaid-graph.dll", 3)]
@@ -63,7 +85,6 @@ public void CommandLineFailTests(string? file, int hResult)
6385
{
6486
var filePath = FindFileDownTree("*.csproj");
6587
Assert.That(filePath, Is.Not.Null);
66-
6788
Assert.That(Program.Main(file), Is.EqualTo(hResult));
6889
}
6990

@@ -76,24 +97,9 @@ private static string ExtractMermaid(string markup)
7697
return markup.Substring(0, markup.Length - Commands.MermaidBegin.Length + Environment.NewLine.Length);
7798
}
7899

79-
private static string DetectType(string markup)
100+
private string? DetectType(string markup)
80101
{
81-
using var engine = new V8ScriptEngine();
82-
var mermaidJs = File.ReadAllText("js\\mermaid.min.js");
83-
engine.Execute(mermaidJs);
84-
85-
return engine.Script.mermaid.detectType(markup);
86-
}
87-
88-
private static async Task<string> MermaidRender(string markup)
89-
{
90-
using var engine = new V8ScriptEngine();
91-
var mermaidJs = File.ReadAllText("js\\mermaid.min.js");
92-
engine.Execute(mermaidJs);
93-
94-
var svg = await engine.Script.mermaid.render(markup);
95-
Assert.That(svg, Is.Not.Null);
96-
return svg.ToString();
102+
return Js.Script.mermaid.detectType(markup);
97103
}
98104

99105
private static string? FindFileDownTree(string searchPattern)

0 commit comments

Comments
 (0)