Skip to content

Instantly share code, notes, and snippets.

@tathamoddie
Created June 23, 2010 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tathamoddie/449572 to your computer and use it in GitHub Desktop.
Save tathamoddie/449572 to your computer and use it in GitHub Desktop.
namespace TechList
{
[TestFixture]
public class Luke
{
class DemoObject
{
public string GroupId { get; set; }
public int GroupCount { get; set; }
}
static bool AreThereAnyErrors(IEnumerable<DemoObject> input)
{
return input
.GroupBy(obj => obj.GroupId)
.Where(group => group
.Any(groupItem => groupItem.GroupCount != group.Count()))
.Any();
}
[Test]
public void AreThereAnyErrors_ReturnsTrueWhenAnEntireGroupHasWrongCount()
{
// Arrange
var input = new[] {
new DemoObject { GroupId = "A", GroupCount = 2 },
new DemoObject { GroupId = "B", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 2 },
new DemoObject { GroupId = "C", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 2 }
};
// Act
var result = AreThereAnyErrors(input);
// Assert
Assert.IsTrue(result);
}
[Test]
public void AreThereAnyErrors_ReturnsTrueWhenOneElementOfAGroupHasWrongCount()
{
// Arrange
var input = new[] {
new DemoObject { GroupId = "A", GroupCount = 3 },
new DemoObject { GroupId = "B", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 2 },
new DemoObject { GroupId = "C", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 3 }
};
// Act
var result = AreThereAnyErrors(input);
// Assert
Assert.IsTrue(result);
}
[Test]
public void AreThereAnyErrors_ReturnsFalseWhenAllGroupsHaveCorrectCount()
{
// Arrange
var input = new[] {
new DemoObject { GroupId = "A", GroupCount = 3 },
new DemoObject { GroupId = "B", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 3 },
new DemoObject { GroupId = "C", GroupCount = 1 },
new DemoObject { GroupId = "A", GroupCount = 3 }
};
// Act
var result = AreThereAnyErrors(input);
// Assert
Assert.IsFalse(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment