Skip to content

Commit de2eb7a

Browse files
committed
Add unit tests for UML fragment classes
1 parent e379154 commit de2eb7a

File tree

7 files changed

+1148
-0
lines changed

7 files changed

+1148
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
using PlantUml.Builder;
3+
4+
namespace DendroDocs.Uml.Extensions.Tests;
5+
6+
[TestClass]
7+
public class IHaveModifiersExtensionsTests
8+
{
9+
[TestMethod]
10+
public void ToPlantUmlVisibility_NullModifiers_Should_Throw()
11+
{
12+
// Arrange
13+
IHaveModifiers modifiers = null!;
14+
15+
// Act
16+
Action action = () => modifiers.ToPlantUmlVisibility();
17+
18+
// Assert
19+
action.ShouldThrow<ArgumentNullException>();
20+
}
21+
22+
[TestMethod]
23+
public void ToPlantUmlVisibility_PublicModifier_Should_ReturnPublic()
24+
{
25+
// Arrange
26+
var modifiers = new TestModifiers { Modifiers = Modifier.Public };
27+
28+
// Act
29+
var result = modifiers.ToPlantUmlVisibility();
30+
31+
// Assert
32+
result.ShouldBe(VisibilityModifier.Public);
33+
}
34+
35+
[TestMethod]
36+
public void ToPlantUmlVisibility_InternalModifier_Should_ReturnPackagePrivate()
37+
{
38+
// Arrange
39+
var modifiers = new TestModifiers { Modifiers = Modifier.Internal };
40+
41+
// Act
42+
var result = modifiers.ToPlantUmlVisibility();
43+
44+
// Assert
45+
result.ShouldBe(VisibilityModifier.PackagePrivate);
46+
}
47+
48+
[TestMethod]
49+
public void ToPlantUmlVisibility_ProtectedModifier_Should_ReturnProtected()
50+
{
51+
// Arrange
52+
var modifiers = new TestModifiers { Modifiers = Modifier.Protected };
53+
54+
// Act
55+
var result = modifiers.ToPlantUmlVisibility();
56+
57+
// Assert
58+
result.ShouldBe(VisibilityModifier.Protected);
59+
}
60+
61+
[TestMethod]
62+
public void ToPlantUmlVisibility_PrivateModifier_Should_ReturnPrivate()
63+
{
64+
// Arrange
65+
var modifiers = new TestModifiers { Modifiers = Modifier.Private };
66+
67+
// Act
68+
var result = modifiers.ToPlantUmlVisibility();
69+
70+
// Assert
71+
result.ShouldBe(VisibilityModifier.Private);
72+
}
73+
74+
[TestMethod]
75+
public void ToPlantUmlVisibility_NoVisibilityModifier_Should_ReturnNone()
76+
{
77+
// Arrange
78+
var modifiers = new TestModifiers { Modifiers = Modifier.Static };
79+
80+
// Act
81+
var result = modifiers.ToPlantUmlVisibility();
82+
83+
// Assert
84+
result.ShouldBe(VisibilityModifier.None);
85+
}
86+
87+
[TestMethod]
88+
public void ToPlantUmlVisibility_NoModifiers_Should_ReturnNone()
89+
{
90+
// Arrange
91+
var modifiers = new TestModifiers();
92+
93+
// Act
94+
var result = modifiers.ToPlantUmlVisibility();
95+
96+
// Assert
97+
result.ShouldBe(VisibilityModifier.None);
98+
}
99+
100+
[TestMethod]
101+
public void ToPlantUmlVisibility_CombinedModifiers_Should_ReturnFirstVisibilityMatch()
102+
{
103+
// Arrange
104+
var modifiers = new TestModifiers { Modifiers = Modifier.Public | Modifier.Static };
105+
106+
// Act
107+
var result = modifiers.ToPlantUmlVisibility();
108+
109+
// Assert
110+
result.ShouldBe(VisibilityModifier.Public);
111+
}
112+
113+
[TestMethod]
114+
public void ToPlantUmlVisibility_PriorityOrder_Should_ReturnPublicFirst()
115+
{
116+
// Arrange - This scenario shouldn't occur in real code, but tests the method's priority
117+
var modifiers = new TestModifiers { Modifiers = Modifier.Public | Modifier.Private };
118+
119+
// Act
120+
var result = modifiers.ToPlantUmlVisibility();
121+
122+
// Assert
123+
result.ShouldBe(VisibilityModifier.Public);
124+
}
125+
126+
[TestMethod]
127+
public void ToPlantUmlVisibility_InternalOverProtected_Should_ReturnPackagePrivate()
128+
{
129+
// Arrange
130+
var modifiers = new TestModifiers { Modifiers = Modifier.Internal | Modifier.Protected };
131+
132+
// Act
133+
var result = modifiers.ToPlantUmlVisibility();
134+
135+
// Assert
136+
result.ShouldBe(VisibilityModifier.PackagePrivate);
137+
}
138+
139+
[TestMethod]
140+
public void ToPlantUmlVisibility_ProtectedOverPrivate_Should_ReturnProtected()
141+
{
142+
// Arrange
143+
var modifiers = new TestModifiers { Modifiers = Modifier.Protected | Modifier.Private };
144+
145+
// Act
146+
var result = modifiers.ToPlantUmlVisibility();
147+
148+
// Assert
149+
result.ShouldBe(VisibilityModifier.Protected);
150+
}
151+
152+
// Helper class for testing IHaveModifiers
153+
private class TestModifiers : IHaveModifiers
154+
{
155+
public Modifier Modifiers { get; set; }
156+
}
157+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
namespace DendroDocs.Uml.Fragments.Tests;
2+
3+
[TestClass]
4+
public class AltSectionTests
5+
{
6+
[TestMethod]
7+
public void Constructor_Should_InitializeWithNullProperties()
8+
{
9+
// Act
10+
var section = new AltSection();
11+
12+
// Assert
13+
section.GroupType.ShouldBeNull();
14+
section.Label.ShouldBeNull();
15+
section.Fragments.ShouldBeEmpty();
16+
}
17+
18+
[TestMethod]
19+
public void GroupType_SetAndGet_Should_WorkCorrectly()
20+
{
21+
// Arrange
22+
var section = new AltSection();
23+
const string groupType = "alt";
24+
25+
// Act
26+
section.GroupType = groupType;
27+
28+
// Assert
29+
section.GroupType.ShouldBe(groupType);
30+
}
31+
32+
[TestMethod]
33+
public void Label_SetAndGet_Should_WorkCorrectly()
34+
{
35+
// Arrange
36+
var section = new AltSection();
37+
const string label = "condition1";
38+
39+
// Act
40+
section.Label = label;
41+
42+
// Assert
43+
section.Label.ShouldBe(label);
44+
}
45+
46+
[TestMethod]
47+
public void GroupType_SetToNull_Should_AcceptNull()
48+
{
49+
// Arrange
50+
var section = new AltSection { GroupType = "alt" };
51+
52+
// Act
53+
section.GroupType = null;
54+
55+
// Assert
56+
section.GroupType.ShouldBeNull();
57+
}
58+
59+
[TestMethod]
60+
public void Label_SetToNull_Should_AcceptNull()
61+
{
62+
// Arrange
63+
var section = new AltSection { Label = "condition" };
64+
65+
// Act
66+
section.Label = null;
67+
68+
// Assert
69+
section.Label.ShouldBeNull();
70+
}
71+
72+
[TestMethod]
73+
public void AddFragment_Should_WorkAsInteractionFragment()
74+
{
75+
// Arrange
76+
var section = new AltSection { GroupType = "alt", Label = "condition" };
77+
var arrow = new Arrow { Source = "A", Target = "B", Name = "message" };
78+
79+
// Act
80+
section.AddFragment(arrow);
81+
82+
// Assert
83+
section.Fragments.ShouldHaveSingleItem();
84+
section.Fragments[0].ShouldBe(arrow);
85+
arrow.Parent.ShouldBe(section);
86+
}
87+
88+
[TestMethod]
89+
public void InheritsFromInteractionFragment_Should_HaveFragmentBehavior()
90+
{
91+
// Arrange
92+
var alt = new Alt();
93+
var section = new AltSection { GroupType = "alt", Label = "condition" };
94+
95+
// Act
96+
alt.AddSection(section);
97+
98+
// Assert
99+
section.Parent.ShouldBe(alt);
100+
alt.Sections.ShouldHaveSingleItem();
101+
alt.Sections[0].ShouldBe(section);
102+
}
103+
104+
[TestMethod]
105+
public void DebuggerDisplay_Should_ShowCorrectFormat()
106+
{
107+
// Arrange
108+
var section = new AltSection { GroupType = "alt", Label = "x > 0" };
109+
110+
// Act & Assert
111+
// Note: We can't directly test DebuggerDisplay attribute, but we can verify the properties exist
112+
section.GroupType.ShouldBe("alt");
113+
section.Label.ShouldBe("x > 0");
114+
}
115+
116+
[TestMethod]
117+
public void AddFragments_MultipleLevels_Should_BuildHierarchy()
118+
{
119+
// Arrange
120+
var section = new AltSection { GroupType = "alt", Label = "condition" };
121+
var nestedAlt = new Alt();
122+
var nestedSection = new AltSection { GroupType = "opt", Label = "nested" };
123+
var arrow = new Arrow { Source = "A", Target = "B" };
124+
125+
// Act
126+
nestedSection.AddFragment(arrow);
127+
nestedAlt.AddSection(nestedSection);
128+
section.AddFragment(nestedAlt);
129+
130+
// Assert
131+
section.Fragments.ShouldHaveSingleItem();
132+
nestedAlt.Parent.ShouldBe(section);
133+
nestedSection.Parent.ShouldBe(nestedAlt);
134+
arrow.Parent.ShouldBe(nestedSection);
135+
}
136+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
namespace DendroDocs.Uml.Fragments.Tests;
2+
3+
[TestClass]
4+
public class AltTests
5+
{
6+
[TestMethod]
7+
public void Constructor_Should_InitializeEmptySections()
8+
{
9+
// Act
10+
var alt = new Alt();
11+
12+
// Assert
13+
alt.Sections.ShouldNotBeNull();
14+
alt.Sections.ShouldBeEmpty();
15+
}
16+
17+
[TestMethod]
18+
public void AddSection_ValidSection_Should_AddToSections()
19+
{
20+
// Arrange
21+
var alt = new Alt();
22+
var section = new AltSection { GroupType = "alt", Label = "condition" };
23+
24+
// Act
25+
alt.AddSection(section);
26+
27+
// Assert
28+
alt.Sections.ShouldHaveSingleItem();
29+
alt.Sections[0].ShouldBe(section);
30+
section.Parent.ShouldBe(alt);
31+
}
32+
33+
[TestMethod]
34+
public void AddSection_NullSection_Should_Throw()
35+
{
36+
// Arrange
37+
var alt = new Alt();
38+
39+
// Act
40+
Action action = () => alt.AddSection(null!);
41+
42+
// Assert
43+
action.ShouldThrow<ArgumentNullException>();
44+
}
45+
46+
[TestMethod]
47+
public void AddSection_MultipleSections_Should_MaintainOrder()
48+
{
49+
// Arrange
50+
var alt = new Alt();
51+
var section1 = new AltSection { GroupType = "alt", Label = "condition1" };
52+
var section2 = new AltSection { GroupType = "else", Label = "condition2" };
53+
var section3 = new AltSection { GroupType = "else", Label = "condition3" };
54+
55+
// Act
56+
alt.AddSection(section1);
57+
alt.AddSection(section2);
58+
alt.AddSection(section3);
59+
60+
// Assert
61+
alt.Sections.Count.ShouldBe(3);
62+
alt.Sections[0].ShouldBe(section1);
63+
alt.Sections[1].ShouldBe(section2);
64+
alt.Sections[2].ShouldBe(section3);
65+
}
66+
67+
[TestMethod]
68+
public void AddSection_SectionWithFragments_Should_WorkCorrectly()
69+
{
70+
// Arrange
71+
var alt = new Alt();
72+
var section = new AltSection { GroupType = "alt", Label = "condition" };
73+
var arrow = new Arrow { Source = "A", Target = "B", Name = "message" };
74+
section.AddFragment(arrow);
75+
76+
// Act
77+
alt.AddSection(section);
78+
79+
// Assert
80+
alt.Sections.ShouldHaveSingleItem();
81+
alt.Sections[0].Fragments.ShouldHaveSingleItem();
82+
alt.Sections[0].Fragments[0].ShouldBe(arrow);
83+
section.Parent.ShouldBe(alt);
84+
}
85+
86+
[TestMethod]
87+
public void InheritsFromInteractionFragment_Should_HaveFragmentBehavior()
88+
{
89+
// Arrange
90+
var parentFragment = new Interactions();
91+
var alt = new Alt();
92+
var section = new AltSection { GroupType = "alt", Label = "condition" };
93+
alt.AddSection(section);
94+
95+
// Act
96+
parentFragment.AddFragment(alt);
97+
98+
// Assert
99+
alt.Parent.ShouldBe(parentFragment);
100+
parentFragment.Fragments.ShouldHaveSingleItem();
101+
parentFragment.Fragments[0].ShouldBe(alt);
102+
}
103+
}

0 commit comments

Comments
 (0)