Skip to content

Commit a7afbf8

Browse files
Fix #121: Implementation of IElement Extensions method, including operation
1 parent 46e29a9 commit a7afbf8

File tree

6 files changed

+136
-40
lines changed

6 files changed

+136
-40
lines changed

SysML2.NET.Tests/Extend/ElementExtensionsTestFixture.cs

Lines changed: 105 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
namespace SysML2.NET.Tests.Extend
2222
{
2323
using System;
24-
using System.Linq;
2524

2625
using NUnit.Framework;
2726

27+
using SysML2.NET.Core.POCO.Kernel.Packages;
2828
using SysML2.NET.Core.POCO.Root.Annotations;
2929
using SysML2.NET.Core.POCO.Root.Elements;
3030
using SysML2.NET.Core.POCO.Root.Namespaces;
@@ -43,7 +43,7 @@ public void VerifyComputeDocumentation()
4343
var documentation = new Documentation();
4444
var annotation = new Annotation();
4545

46-
Assert.That(() => element.ComputeDocumentation(), Has.Count.EqualTo(0));
46+
Assert.That(element.ComputeDocumentation, Has.Count.EqualTo(0));
4747

4848
element.AssignOwnership(annotation, documentation);
4949

@@ -61,16 +61,16 @@ public void VerifyComputeIsLibraryElement()
6161

6262
using (Assert.EnterMultipleScope())
6363
{
64-
Assert.That(() => element.ComputeIsLibraryElement(), Is.False);
65-
Assert.That(() => documentation.ComputeIsLibraryElement(), Is.False);
64+
Assert.That(element.ComputeIsLibraryElement, Is.False);
65+
Assert.That(documentation.ComputeIsLibraryElement, Is.False);
6666
}
6767

6868
element.AssignOwnership(annotation, documentation);
6969

7070
using (Assert.EnterMultipleScope())
7171
{
72-
Assert.That(() =>element.ComputeIsLibraryElement(), Is.False);
73-
Assert.That(() => documentation.ComputeIsLibraryElement(), Throws.TypeOf<NotSupportedException>());
72+
Assert.That(element.ComputeIsLibraryElement, Is.False);
73+
Assert.That(documentation.ComputeIsLibraryElement, Throws.TypeOf<NotSupportedException>());
7474
}
7575
}
7676

@@ -81,10 +81,10 @@ public void VerifyComputeName()
8181

8282
var element = new Definition();
8383

84-
Assert.That(() => element.ComputeName(), Is.Null);
84+
Assert.That(element.ComputeName, Is.Null);
8585

8686
element.DeclaredName = "definitionName";
87-
Assert.That(() => element.ComputeName(), Is.EqualTo("definitionName"));
87+
Assert.That(element.ComputeName, Is.EqualTo("definitionName"));
8888
}
8989

9090
[Test]
@@ -96,10 +96,10 @@ public void VerifyComputeOwnedAnnotation()
9696
var documentation = new Documentation();
9797
var annotation = new Annotation();
9898
element.AssignOwnership(annotation, documentation);
99-
Assert.That(() => element.ComputeOwnedAnnotation(), Has.Count.EqualTo(0));
99+
Assert.That(element.ComputeOwnedAnnotation, Has.Count.EqualTo(0));
100100

101101
annotation.AnnotatedElement = element;
102-
Assert.That(() => element.ComputeOwnedAnnotation(), Is.EquivalentTo([annotation]));
102+
Assert.That(element.ComputeOwnedAnnotation, Is.EquivalentTo([annotation]));
103103
}
104104

105105
[Test]
@@ -110,10 +110,10 @@ public void VerifyComputeOwnedElement()
110110
var element = new Definition();
111111
var documentation = new Documentation();
112112
var annotation = new Annotation();
113-
Assert.That(() => element.ComputeOwnedElement(), Has.Count.EqualTo(0));
113+
Assert.That(element.ComputeOwnedElement, Has.Count.EqualTo(0));
114114

115115
element.AssignOwnership(annotation, documentation);
116-
Assert.That(() => element.ComputeDocumentation(), Is.EquivalentTo([documentation]));
116+
Assert.That(element.ComputeDocumentation, Is.EquivalentTo([documentation]));
117117
}
118118

119119
[Test]
@@ -127,16 +127,16 @@ public void VerifyComputeOwner()
127127

128128
using (Assert.EnterMultipleScope())
129129
{
130-
Assert.That(() => element.ComputeOwner(), Is.Null);
131-
Assert.That(() => documentation.ComputeOwner(), Is.Null);
130+
Assert.That(element.ComputeOwner, Is.Null);
131+
Assert.That(documentation.ComputeOwner, Is.Null);
132132
}
133133

134134
element.AssignOwnership(annotation, documentation);
135135

136136
using (Assert.EnterMultipleScope())
137137
{
138-
Assert.That(() => element.ComputeOwner(), Is.Null);
139-
Assert.That(() => documentation.ComputeOwner(), Is.EqualTo(element));
138+
Assert.That(element.ComputeOwner, Is.Null);
139+
Assert.That(documentation.ComputeOwner, Is.EqualTo(element));
140140
}
141141
}
142142

@@ -149,21 +149,21 @@ public void VerifyComputeOwningMembership()
149149
var documentation = new Documentation();
150150
var annotation = new Annotation();
151151

152-
Assert.That(() => documentation.ComputeOwningMembership(), Is.Null);
152+
Assert.That(documentation.ComputeOwningMembership, Is.Null);
153153

154154
element.AssignOwnership(annotation, documentation);
155155

156156
using (Assert.EnterMultipleScope())
157157
{
158-
Assert.That(() => documentation.ComputeOwningMembership(), Is.Null);
159-
Assert.That(() => element.ComputeOwningMembership(), Is.Null);
158+
Assert.That(documentation.ComputeOwningMembership, Is.Null);
159+
Assert.That(element.ComputeOwningMembership, Is.Null);
160160
}
161161

162162
var membership = new OwningMembership();
163163
var namespaceElement = new Namespace();
164164

165165
namespaceElement.AssignOwnership(membership, element);
166-
Assert.That(() => element.ComputeOwningMembership(), Is.EqualTo(membership));
166+
Assert.That(element.ComputeOwningMembership, Is.EqualTo(membership));
167167
}
168168

169169
[Test]
@@ -176,12 +176,12 @@ public void VerifyComputeOwningNamespace()
176176
var annotation = new Annotation();
177177
element.AssignOwnership(annotation, documentation);
178178

179-
Assert.That(() => documentation.ComputeOwningNamespace(), Is.Null);
179+
Assert.That(documentation.ComputeOwningNamespace, Is.Null);
180180
var membership = new OwningMembership();
181181
var namespaceElement = new Namespace();
182182

183183
namespaceElement.AssignOwnership(membership, element);
184-
Assert.That(() => element.ComputeOwningNamespace(), Is.EqualTo(namespaceElement));
184+
Assert.That(element.ComputeOwningNamespace, Is.EqualTo(namespaceElement));
185185
}
186186

187187
[Test]
@@ -191,15 +191,43 @@ public void VerifyComputeQualifiedName()
191191

192192
var element = new Definition();
193193

194-
Assert.That(() => element.ComputeQualifiedName(), Is.Null);
194+
Assert.That(element.ComputeQualifiedName, Is.Null);
195195

196196
var membership = new OwningMembership();
197+
var secondMembership = new OwningMembership();
198+
197199
var namespaceElement = new Namespace();
198200

199201
namespaceElement.AssignOwnership(membership, element);
200202
element.DeclaredName = "name";
203+
204+
Assert.That(element.ComputeQualifiedName, Is.EqualTo("name"));
205+
206+
var secondElement = new Definition()
207+
{
208+
DeclaredName = "name"
209+
};
210+
211+
namespaceElement.AssignOwnership(secondMembership, secondElement);
212+
213+
using (Assert.EnterMultipleScope())
214+
{
215+
Assert.That(element.ComputeQualifiedName, Is.EqualTo("name"));
216+
Assert.That(secondElement.ComputeQualifiedName, Is.Null);
217+
}
218+
219+
var packageOwner = new Package()
220+
{
221+
DeclaredName = "owner"
222+
};
201223

202-
Assert.That(() => element.ComputeQualifiedName(), Is.EqualTo("name"));
224+
var packageMembership = new OwningMembership();
225+
packageOwner.AssignOwnership(packageMembership, namespaceElement);
226+
227+
Assert.That(element.ComputeQualifiedName, Is.Null);
228+
229+
namespaceElement.DeclaredName = "namespace";
230+
Assert.That(element.ComputeQualifiedName, Is.EqualTo("namespace::name"));
203231
}
204232

205233
[Test]
@@ -209,10 +237,10 @@ public void VerifyComputeShortName()
209237

210238
var element = new Definition();
211239

212-
Assert.That(() => element.ComputeShortName(), Is.Null);
240+
Assert.That(element.ComputeShortName, Is.Null);
213241

214242
element.DeclaredShortName = "shortName";
215-
Assert.That(() => element.ComputeShortName(), Is.EqualTo("shortName"));
243+
Assert.That(element.ComputeShortName, Is.EqualTo("shortName"));
216244
}
217245

218246
[Test]
@@ -224,11 +252,61 @@ public void VerifyComputeTextualRepresentation()
224252
var textualRepresentation = new TextualRepresentation();
225253
var annotation = new Annotation();
226254

227-
Assert.That(() => element.ComputeTextualRepresentation(), Has.Count.EqualTo(0));
255+
Assert.That(element.ComputeTextualRepresentation, Has.Count.EqualTo(0));
228256

229257
element.AssignOwnership(annotation, textualRepresentation);
230258

231259
Assert.That(element.ComputeTextualRepresentation(), Is.EquivalentTo([textualRepresentation]));
232260
}
261+
262+
[Test]
263+
public void VerifyComputePathOperation()
264+
{
265+
Assert.That(() => ((IElement)null).ComputePathOperation(), Throws.TypeOf<ArgumentNullException>());
266+
267+
var element = new Definition();
268+
269+
Assert.That(element.ComputePathOperation, Is.Empty);
270+
element.DeclaredName = "name";
271+
272+
Assert.That(element.ComputePathOperation, Is.Empty);
273+
274+
var membership = new OwningMembership();
275+
var secondMembership = new OwningMembership();
276+
var namespaceElement = new Namespace();
277+
278+
namespaceElement.AssignOwnership(membership, element);
279+
280+
Assert.That(element.ComputePathOperation, Is.EqualTo("name"));
281+
282+
var secondElement = new Definition()
283+
{
284+
DeclaredName = "name"
285+
};
286+
287+
namespaceElement.AssignOwnership(secondMembership, secondElement);
288+
289+
using (Assert.EnterMultipleScope())
290+
{
291+
Assert.That(element.ComputePathOperation, Is.EqualTo("name"));
292+
Assert.That(secondElement.ComputePathOperation, Throws.TypeOf<NotSupportedException>());
293+
}
294+
}
295+
296+
[Test]
297+
public void VerifyComputeEscapedNameOperation()
298+
{
299+
Assert.That(() => ((IElement)null).ComputeEscapedNameOperation(), Throws.TypeOf<ArgumentNullException>());
300+
301+
var element = new Definition()
302+
{
303+
DeclaredName = "basic"
304+
};
305+
306+
Assert.That(element.ComputeEscapedNameOperation, Is.EqualTo("basic"));
307+
308+
element.DeclaredName = "non basic";
309+
Assert.That(element.ComputeEscapedNameOperation, Is.EqualTo("\'non basic\'"));
310+
}
233311
}
234312
}

SysML2.NET.Tests/Extend/MembershipExtensionsTestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void ComputeMemberElementId_ThrowsNotSupportedException()
3838
[Test]
3939
public void ComputeMembershipOwningNamespace_ThrowsNotSupportedException()
4040
{
41-
Assert.That(() => ((IMembership)null).ComputeMembershipOwningNamespace(), Throws.TypeOf<NotSupportedException>());
41+
Assert.That(() => ((IMembership)null).ComputeMembershipOwningNamespace(), Throws.TypeOf<ArgumentNullException>());
4242
}
4343
}
4444
}

SysML2.NET.Tests/Extend/OwningMembershipExtensionsTestFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class OwningMembershipExtensionsTestFixture
3232
[Test]
3333
public void ComputeOwnedMemberElement_ThrowsNotSupportedException()
3434
{
35-
Assert.That(() => ((IOwningMembership)null).ComputeOwnedMemberElement(), Throws.TypeOf<NotSupportedException>());
35+
Assert.That(() => ((IOwningMembership)null).ComputeOwnedMemberElement(), Throws.TypeOf<ArgumentNullException>());
3636
}
3737

3838
[Test]
@@ -44,7 +44,7 @@ public void ComputeOwnedMemberElementId_ThrowsNotSupportedException()
4444
[Test]
4545
public void ComputeOwnedMemberName_ThrowsNotSupportedException()
4646
{
47-
Assert.That(() => ((IOwningMembership)null).ComputeOwnedMemberName(), Throws.TypeOf<NotSupportedException>());
47+
Assert.That(() => ((IOwningMembership)null).ComputeOwnedMemberName(), Throws.TypeOf<ArgumentNullException>());
4848
}
4949

5050
[Test]

SysML2.NET/Extend/ElementExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ internal static string ComputePathOperation(this IElement elementSubject)
336336
var ownedRelatedElementsIndex = elementSubject.OwningRelationship.OwnedRelatedElement.ToList().IndexOf(elementSubject) +1;
337337
var parentPath = elementSubject.OwningRelationship.Path();
338338

339-
return string.IsNullOrWhiteSpace(parentPath) ? $"/{ownedRelatedElementsIndex}": $"{parentPath}/{ownedRelatedElementsIndex}";
339+
return $"{parentPath}/{ownedRelatedElementsIndex}";
340340
}
341341
}
342342
}

SysML2.NET/Extend/NamespaceExtensions.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace SysML2.NET.Core.POCO.Root.Namespaces
2222
{
2323
using System;
24+
using System.Collections.Frozen;
2425
using System.Collections.Generic;
2526
using System.Linq;
2627

@@ -46,7 +47,7 @@ internal static class NamespaceExtensions
4647
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
4748
internal static List<IMembership> ComputeImportedMembership(this INamespace namespaceSubject)
4849
{
49-
return namespaceSubject == null ? throw new ArgumentNullException(nameof(namespaceSubject)) : namespaceSubject.ImportedMemberships((INamespace)null);
50+
return namespaceSubject == null ? throw new ArgumentNullException(nameof(namespaceSubject)) : namespaceSubject.ImportedMemberships([]);
5051
}
5152

5253
/// <summary>
@@ -88,10 +89,9 @@ internal static List<IMembership> ComputeMembership(this INamespace namespaceSub
8889
/// <returns>
8990
/// the computed result
9091
/// </returns>
91-
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
9292
internal static List<IImport> ComputeOwnedImport(this INamespace namespaceSubject)
9393
{
94-
throw new NotSupportedException("Create a GitHub issue when this method is required");
94+
return namespaceSubject == null ? throw new ArgumentNullException(nameof(namespaceSubject)) : [..namespaceSubject.OwnedRelationship.OfType<IImport>()];
9595
}
9696

9797
/// <summary>
@@ -204,10 +204,30 @@ internal static List<IMembership> ComputeVisibleMembershipsOperation(this INames
204204
/// <returns>
205205
/// The expected collection of <see cref="IMembership" />
206206
/// </returns>
207-
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
208207
internal static List<IMembership> ComputeImportedMembershipsOperation(this INamespace namespaceSubject, List<INamespace> excluded)
209208
{
210-
throw new NotSupportedException("Create a GitHub issue when this method is required");
209+
var importedMemberships = namespaceSubject.ownedImport.Where(x => !excluded.Contains(x.importOwningNamespace))
210+
.SelectMany(x => x.ImportedMemberships(excluded))
211+
.Distinct()
212+
.ToList();
213+
214+
var ownedMembershipNames = namespaceSubject.ownedMembership
215+
.Select(m => m.MemberName)
216+
.Where(name => name != null)
217+
.ToHashSet(StringComparer.Ordinal);
218+
219+
var importedMembershipsNameFrequency = importedMemberships
220+
.GroupBy(x => x.MemberName, StringComparer.Ordinal)
221+
.ToFrozenDictionary(g => g.Key, g => g.Count(), StringComparer.Ordinal);
222+
223+
var nonCollidingImportedMemberships = importedMemberships.Where(x =>
224+
{
225+
var name = x.MemberName;
226+
227+
return !string.IsNullOrWhiteSpace(name) && !ownedMembershipNames.Contains(name) && (!importedMembershipsNameFrequency.TryGetValue(name, out var frequency) || frequency <= 1);
228+
}).ToList();
229+
230+
return nonCollidingImportedMemberships;
211231
}
212232

213233
/// <summary>

SysML2.NET/Extend/OwningMembershipExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ internal static class OwningMembershipExtensions
4343
/// <returns>
4444
/// the computed result
4545
/// </returns>
46-
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
4746
internal static IElement ComputeOwnedMemberElement(this IOwningMembership owningMembershipSubject)
4847
{
49-
return owningMembershipSubject == null ? throw new ArgumentNullException(nameof(owningMembershipSubject)) : owningMembershipSubject.OwnedRelatedElement.SingleOrDefault();
48+
return owningMembershipSubject == null ? throw new ArgumentNullException(nameof(owningMembershipSubject)) : owningMembershipSubject.OwnedRelatedElement.Single();
5049
}
5150

5251
/// <summary>
@@ -73,10 +72,9 @@ internal static string ComputeOwnedMemberElementId(this IOwningMembership owning
7372
/// <returns>
7473
/// the computed result
7574
/// </returns>
76-
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
7775
internal static string ComputeOwnedMemberName(this IOwningMembership owningMembershipSubject)
7876
{
79-
throw new NotSupportedException("Create a GitHub issue when this method is required");
77+
return owningMembershipSubject == null ? throw new ArgumentNullException(nameof(owningMembershipSubject)) : owningMembershipSubject.ownedMemberElement.name;
8078
}
8179

8280
/// <summary>

0 commit comments

Comments
 (0)