-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandsTests.cs
More file actions
123 lines (102 loc) · 3.36 KB
/
CommandsTests.cs
File metadata and controls
123 lines (102 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Microsoft.ClearScript.V8;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;
namespace MermaidGraph.Tests;
[TestFixture]
public class CommandsTests
{
private V8ScriptEngine? _engine;
private V8ScriptEngine Js {
get
{
if (_engine is null)
{
_engine ??= new V8ScriptEngine();
_engine.Execute(File.ReadAllText("js\\mermaid.min.js"));
_engine.Script.mermaid.initialize();
}
return _engine;
}
}
[OneTimeTearDown]
public void Disposal()
{
_engine?.Dispose();
}
[Test]
public void DogFoodSolutionTest()
{
var solutionPath = FindFileDownTree("*.sln");
Assert.That(solutionPath, Is.Not.Null);
var info = new FileInfo(solutionPath!);
Assert.That(info.Exists);
var graph = new Commands().Solution(info);
Console.WriteLine(graph);
var graphType = DetectType(ExtractMermaid(graph));
Assert.That(graphType, Is.EqualTo("class"));
Console.WriteLine(graphType);
}
[Test]
public void DogFoodProjectTestAsync()
{
var filePath = FindFileDownTree("*.csproj");
Assert.That(filePath, Is.Not.Null);
var info = new FileInfo(filePath!);
Assert.That(info.Exists);
var graph = new Commands().Project(info);
Console.WriteLine(graph);
var graphType = DetectType(ExtractMermaid(graph));
Assert.That(graphType, Is.EqualTo("class"));
Console.WriteLine(graphType);
}
[Test]
public void CommandLineProjectTest()
{
var filePath = FindFileDownTree("*.csproj");
Assert.That(filePath, Is.Not.Null);
Assert.That(Program.Main(filePath), Is.EqualTo(0));
}
[Test]
public void CommandLineSolutionTest()
{
var filePath = FindFileDownTree("*.sln");
Assert.That(filePath, Is.Not.Null);
Assert.That(Program.Main(filePath), Is.EqualTo(0));
}
[Test]
[TestCase(null, 1)]
[TestCase("File Not Found", 2)]
[TestCase("mermaid-graph.dll", 3)]
public void CommandLineFailTests(string? file, int hResult)
{
var filePath = FindFileDownTree("*.csproj");
Assert.That(filePath, Is.Not.Null);
Assert.That(Program.Main(file), Is.EqualTo(hResult));
}
private static string ExtractMermaid(string markup)
{
Assert.That(markup, Does.StartWith(Commands.MermaidBegin));
markup = markup.Substring(Commands.MermaidBegin.Length + Environment.NewLine.Length);
Assert.That(markup, Does.EndWith(Commands.Fence + Environment.NewLine));
return markup.Substring(0, markup.Length - Commands.MermaidBegin.Length + Environment.NewLine.Length);
}
private string? DetectType(string markup)
{
return Js.Script.mermaid.detectType(markup);
}
private static string? FindFileDownTree(string searchPattern)
{
var currentDir = Directory.GetCurrentDirectory();
while (currentDir != null)
{
var solutionFiles = Directory.EnumerateFiles(currentDir, searchPattern);
var file = solutionFiles.FirstOrDefault();
if (file is not null)
{
return file;
}
currentDir = Directory.GetParent(currentDir)?.FullName;
}
return null;
}
}