Skip to content

Commit

Permalink
Adds FeatureListValidator and some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Richardson committed Dec 13, 2017
1 parent 6e564da commit febeb2c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
40 changes: 40 additions & 0 deletions Sharpasonne.Models.Tests/FeatureListValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Optional.Unsafe;
using Sharpasonne.Models.Features;
using Xunit;

namespace Sharpasonne.Models.Tests
{
public class FeatureListValidatorTests
{
[Fact]
void Given_EmptyTile_When_CheckingForOverlaps_Then_IsValid()
{
var featureList = Enumerable.Empty<IFeature>().ToImmutableList();
var isValid = new FeatureListValidator().CheckFeaturesDontOverlap(featureList);

Assert.True(isValid);
}

[Fact]
void Given_OverlappingFeaturesTile_When_CheckingForOverlaps_Then_IsNotValid()
{
var segments = Enumerable.Repeat(Segment.Left, 2)
.ToImmutableHashSet();
var overlappingFeatures = Enumerable.Repeat(new Field(segments) as IFeature, 2)
.ToImmutableList();

var isValid = new FeatureListValidator().CheckFeaturesDontOverlap(overlappingFeatures);

Assert.False(isValid);
}

[Fact]
void Given_EmptyTile_When_CheckingForFields_Then_AllFields()
{
throw new NotImplementedException();
}
}
}
28 changes: 28 additions & 0 deletions Sharpasonne.Models/FeatureListValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;

namespace Sharpasonne.Models
{
public class FeatureListValidator
{
public bool IsValid([NotNull] IImmutableList<IFeature> featureList)
{
var isValid = CheckFeaturesDontOverlap(featureList);

return isValid;
}

public bool CheckFeaturesDontOverlap(
[NotNull] [ItemNotNull] IImmutableList<IFeature> featureList)
{
var segments = featureList
.SelectMany(f => f.Connections)
.ToList();

var distinctSegments = segments.Distinct();

return segments.Count() == distinctSegments.Count();
}
}
}
7 changes: 6 additions & 1 deletion Sharpasonne.Models/Features/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Sharpasonne.Models.Features
{
class Field : IFeature
public class Field : IFeature
{
public Field(IImmutableSet<Segment> connections)
{
Connections = connections;
}

public IImmutableSet<Segment> Connections { get; }
}
}
11 changes: 2 additions & 9 deletions Sharpasonne.Models/TileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@
using System.Linq;
using JetBrains.Annotations;
using Optional;
using Sharpasonne.Models;

namespace Sharpasonne
namespace Sharpasonne.Models
{
public class TileBuilder
{
public Option<Tile> CreateTile([NotNull] IEnumerable<IFeature> features)
{
var featureList = features as IImmutableList<IFeature> ?? features.ToImmutableList();

if (IsValid(featureList))
if (new FeatureListValidator().IsValid(featureList))
{
var tile = new Tile(featureList);
return Option.Some(tile);
}

return Option.None<Tile>();
}

private bool IsValid([NotNull] IImmutableList<IFeature> featureList)
{
// TODO: It.
return true;
}
}
}

0 comments on commit febeb2c

Please sign in to comment.