Skip to content

Commit 5d59283

Browse files
committed
added ValidateGroups to SharedParameterFile, so it can be used in constructors
1 parent 1c84586 commit 5d59283

2 files changed

Lines changed: 52 additions & 24 deletions

File tree

src/Parameters/Shared/SharedParameterFile.cs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public sealed partial class SharedParameterFile : ICloneable, IEquatable<SharedP
2424

2525
private IReadOnlyList<Group> _groups;
2626

27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="SharedParameterFile"/> class.
29+
/// </summary>
2730
internal SharedParameterFile()
2831
{
2932
Metadata = new MetaData(2, 1);
@@ -42,10 +45,19 @@ public SharedParameterFile(IEnumerable<Group> groups = null, IEnumerable<Paramet
4245
Metadata = metadata ?? new MetaData(2, 1);
4346
Parameters = new ParameterCollection(this, parameters ?? new List<Parameter>());
4447
_groups = groups != null ? new List<Group>(groups) : new List<Group>();
48+
49+
var groupValidation = ValidateGroups(_groups?.ToList())?.ToArray();
50+
if (groupValidation != null && groupValidation.Any())
51+
{
52+
throw new ArgumentException(
53+
nameof(groups),
54+
$"Groups you have supplied are invalid. Here is what is wrong: {string.Join(", ", groupValidation.Select(v => v.ErrorMessage))}"
55+
);
56+
}
4557
}
4658

4759
/// <summary>
48-
/// Initializes a new instance of the <see cref="T:CodeCave.Revit.Toolkit.Parameters.Shared.SharedParameterFile" /> class.
60+
/// Initializes a new instance of the <see cref="SharedParameterFile" /> class.
4961
/// </summary>
5062
/// <param name="groups">The list of groups.</param>
5163
/// <param name="parameters">The list of parameters.</param>
@@ -163,30 +175,12 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
163175
results.Add(new ValidationResult($"Data in {nameof(Sections.META)} section is invalid",
164176
new[] {nameof(Metadata)}));
165177

166-
var groups = Groups.ToArray();
167-
if (!groups.Any())
168-
results.Add(new ValidationResult("The list of groups is empty", new[] {nameof(Groups)}));
169-
170178
if (!Parameters.Any())
171179
results.Add(new ValidationResult("The list of parameters is empty", new[] {nameof(Parameters)}));
172180

173-
// Check for group duplicates by ID
174-
var groupIds = groups.GroupBy(p => p.Id).Where(g => g.Count() > 1).Select(p => p.Key);
175-
results.AddRange(groupIds.Select(groupId =>
176-
new ValidationResult($"The following group {nameof(Group.Id)} has duplicates: {groupId}",
177-
new[] {nameof(Groups)})));
178-
179-
// Check for group duplicates by name
180-
var groupNames = groups.GroupBy(p => p.Name).Where(g => g.Count() > 1).Select(p => p.Key);
181-
results.AddRange(groupNames.Select(group =>
182-
new ValidationResult($"The following group {nameof(Group.Name)} has duplicates: {group}",
183-
new[] {nameof(Groups)})));
184-
185-
// Check for unused
186-
var unusedGroups = groups.Where(g => !Parameters.Any(p => g.Id.Equals(p.Group?.Id)));
187-
results.AddRange(unusedGroups.Select(g =>
188-
new ValidationResult($"The following group is unused (not assigned to any parameter): {g.Id}={g.Name}",
189-
new[] {nameof(Groups)})));
181+
// Validate groups
182+
var groups = Groups.ToArray();
183+
results.AddRange(ValidateGroups(groups));
190184

191185
// Check for parameter duplicates by Guid
192186
var paramGuidDuplicates = Parameters.GroupBy(p => p.Guid).Where(g => g.Count() > 1).Select(p => p.Key);
@@ -200,15 +194,49 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
200194
new ValidationResult($"The following parameter {nameof(Parameter.Name)} has duplicates: {name}",
201195
new[] {nameof(Parameters)})));
202196

197+
// Check for unused
198+
var unusedGroups = groups.Where(g => !Parameters.Any(p => g.Id.Equals(p.Group?.Id)));
199+
results.AddRange(unusedGroups.Select(g =>
200+
new ValidationResult($"The following group is unused (not assigned to any parameter): {g.Id}={g.Name}",
201+
new[] { nameof(Groups) })));
202+
203203
// Check for orphan parameters by groups
204204
var paramGroupOrphans = Parameters.Where(p => !groups.Any(g => g.Id.Equals(p.Group?.Id)));
205205
results.AddRange(paramGroupOrphans.Select(p =>
206206
new ValidationResult($"The following parameter is assigned to an unknown group ({p.Group?.Id}): {p.Name}",
207-
new[] {nameof(Parameters)})));
207+
new[] { nameof(Parameters) })));
208+
209+
return results;
210+
}
211+
212+
/// <summary>
213+
/// Validates a list of groups.
214+
/// </summary>
215+
/// <param name="groups">The groups to validate.</param>
216+
/// <returns></returns>
217+
internal IEnumerable<ValidationResult> ValidateGroups(ICollection<Group> groups)
218+
{
219+
var results = new List<ValidationResult>();
220+
221+
if (!groups.Any())
222+
results.Add(new ValidationResult("The list of groups is empty", new[] { nameof(Groups) }));
223+
224+
// Check for group duplicates by ID
225+
var groupIds = groups.GroupBy(p => p.Id).Where(g => g.Count() > 1).Select(p => p.Key);
226+
results.AddRange(groupIds.Select(groupId =>
227+
new ValidationResult($"The following group {nameof(Group.Id)} has duplicates: {groupId}",
228+
new[] { nameof(Groups) })));
229+
230+
// Check for group duplicates by name
231+
var groupNames = groups.GroupBy(p => p.Name).Where(g => g.Count() > 1).Select(p => p.Key);
232+
results.AddRange(groupNames.Select(group =>
233+
new ValidationResult($"The following group {nameof(Group.Name)} has duplicates: {group}",
234+
new[] { nameof(Groups) })));
208235

209236
return results;
210237
}
211238

239+
212240
#endregion Validation
213241

214242
#region Equals and GetHashCode

tests/SharedParameterFileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void GroupsAreParsedCorrectly()
184184
[Fact]
185185
public void ClonedAndRandomizedFilesAreEqual()
186186
{
187-
Assert.All(SharedParameterFiles,
187+
Assert.All(SharedParameterFiles.Where(f => f.StartsWith(PathToValidFiles)),
188188
sharedParamFilePath =>
189189
{
190190
var sharedParamFile1 = new SharedParameterFile(sharedParamFilePath);

0 commit comments

Comments
 (0)