Skip to content

Commit

Permalink
feat(validation): validate that active runway rows are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTWF committed Jan 29, 2021
1 parent 34ac130 commit c46d211
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/Compiler/Model/ActiveRunway.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Compiler.Model
using System;

namespace Compiler.Model
{
public class ActiveRunway : AbstractCompilableElement
{
Expand All @@ -25,6 +27,22 @@ Comment inlineComment
*/
public int Mode { get; }

public override bool Equals(object obj)
{
return obj is ActiveRunway &&
Equals((ActiveRunway) obj);
}

protected bool Equals(ActiveRunway other)
{
return Identifier == other.Identifier && Airfield == other.Airfield && Mode == other.Mode;
}

public override int GetHashCode()
{
return HashCode.Combine(Identifier, Airfield, Mode);
}

public override string GetCompileData(SectorElementCollection elements)
{
return $"ACTIVE_RUNWAY:{this.Airfield}:{this.Identifier}:{this.Mode}";
Expand Down
36 changes: 36 additions & 0 deletions src/Compiler/Validate/AllActiveRunwaysMustBeUnique.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Linq;
using Compiler.Argument;
using Compiler.Error;
using Compiler.Event;
using Compiler.Model;

namespace Compiler.Validate
{
public class AllActiveRunwaysMustBeUnique: IValidationRule
{
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events)
{
var duplicates = sectorElements.ActiveRunways.GroupBy(
runway => runway
)
.Where(g => g.Count() > 1)
.ToList();

if (!duplicates.Any())
{
return;
}

foreach (var duplicate in duplicates)
{
ActiveRunway runway = duplicate.First();
events.AddEvent(
new ValidationRuleFailure(
$"Duplicate ACTIVE_RUNWAY {runway.Airfield}:{runway.Identifier}:{runway.Mode}",
runway
)
);
}
}
}
}
5 changes: 3 additions & 2 deletions src/Compiler/Validate/AllActiveRunwaysMustReferenceARunway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public void Validate(SectorElementCollection sectorElements, CompilerArguments a
var missingRunways = sectorElements.ActiveRunways
.Where(
activeRunway => !sectorElements.Runways.Exists(runway => IsSameRunway(activeRunway, runway))
);
)
.ToList();

if (missingRunways.Count() == 0)
if (!missingRunways.Any())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public void Validate(SectorElementCollection sectorElements, CompilerArguments a
{
var airports = sectorElements.Airports.Select(airport => airport.Icao).ToList();
var missingAirports = sectorElements.ActiveRunways
.Where(activeRunway => !airports.Contains(activeRunway.Airfield));
.Where(activeRunway => !airports.Contains(activeRunway.Airfield))
.ToList();

if (missingAirports.Count() == 0)
if (!missingAirports.Any())
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Validate/OutputValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class OutputValidator
new CentrelineColourIsDefined(),
new AllActiveRunwaysMustReferenceAnAirport(),
new AllActiveRunwaysMustReferenceARunway(),
new AllActiveRunwaysMustBeUnique(),
};

public static void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events)
Expand Down
37 changes: 37 additions & 0 deletions tests/CompilerTest/Validate/AllActiveRunwaysMustBeUniqueTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Compiler.Validate;
using CompilerTest.Bogus.Factory;
using Xunit;

namespace CompilerTest.Validate
{
public class AllActiveRunwaysMustBeUniqueTest: AbstractValidatorTestCase
{
[Fact]
public void TestItFailsIfDuplicatesExist()
{
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 0));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 1));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 1));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0));
AssertValidationErrors(3);
}

[Fact]
public void TestItPassesOnNoDuplicates()
{
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 0));
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0));
sectorElements.Add(ActiveRunwayFactory.Make("EGXY", "27L", 0));
AssertNoValidationErrors();
}

protected override IValidationRule GetValidationRule()
{
return new AllActiveRunwaysMustBeUnique();
}
}
}

0 comments on commit c46d211

Please sign in to comment.