-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathPlantUmlErrorMessagesCheck.cs
More file actions
108 lines (92 loc) · 3.24 KB
/
PlantUmlErrorMessagesCheck.cs
File metadata and controls
108 lines (92 loc) · 3.24 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.xUnitV3;
using Xunit;
using static ArchUnitNET.Fluent.ArchRuleDefinition;
namespace ArchUnitNETTests.Domain.PlantUml
{
public class PlantUmlErrorMessagesCheck
{
private static readonly Architecture Architecture =
StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture;
private readonly string _umlFile;
public PlantUmlErrorMessagesCheck()
{
_umlFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Domain",
"PlantUml",
"zzz_test_version_with_errors.puml"
);
}
[Fact]
public void NoDuplicatesInErrorMessageTest()
{
var testPassed = CheckByPuml(out var rawErrormessage);
Assert.False(testPassed);
//CheckForDuplicates returns false when errormessage contains duplicates or is empty
var containsNoDuplicates = ContainsNoDuplicates(
rawErrormessage,
out var explainErrormessage
);
var errormessage =
"\nOriginal (ArchUnitNet) Exception:\n"
+ rawErrormessage
+ "\n\nAssert Error:\n"
+ explainErrormessage
+ "\n";
Assert.True(containsNoDuplicates, errormessage);
}
private bool CheckByPuml(out string errormessage)
{
errormessage = null;
try
{
IArchRule adhereToPlantUmlDiagram = Types()
.Should()
.AdhereToPlantUmlDiagram(_umlFile);
adhereToPlantUmlDiagram.Check(Architecture);
}
//xUnit
catch (FailedArchRuleException exception)
{
errormessage = exception.Message;
return false;
}
return true;
}
private static bool ContainsNoDuplicates(string uncutMessage, out string errormessage)
{
if (string.IsNullOrWhiteSpace(uncutMessage))
{
errormessage = "Error message is empty.";
return false;
}
var sources = new List<string>();
var errors = uncutMessage.Split('\n').Skip(1);
foreach (var error in errors.Where(e => !string.IsNullOrWhiteSpace(e)))
{
var splitError = error.Split(" does depend on ");
var source = splitError[0].Trim();
var targets = splitError[1].Trim().Split(" and ");
if (sources.Contains(source))
{
errormessage = $"Two errors with {source} as source found.";
return false;
}
sources.Add(source);
if (targets.Distinct().Count() < targets.Length)
{
errormessage = $"The error \"{error}\" contains duplicate targets.";
return false;
}
}
errormessage = "No duplicates found.";
return true;
}
}
}