Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Created January 21, 2014 22:09
Show Gist options
  • Save beyond-code-github/8549434 to your computer and use it in GitHub Desktop.
Save beyond-code-github/8549434 to your computer and use it in GitHub Desktop.
Efficient iterator specifications
public class ItemA
{
public string Identifier { get; set; }
}
public class ItemB
{
public string Name { get; set; }
}
public abstract class EfficientIteratorTests
{
protected static EfficientIterator<ItemA, ItemB> subject;
protected static List<ItemA> listOne;
protected static List<ItemB> listTwo;
protected static int count;
protected static List<string> listOneOrphans;
protected static List<string> listTwoOrphans;
protected static List<string> intersections;
private Establish context = () =>
{
count = 0;
listOneOrphans = new List<string>();
listTwoOrphans = new List<string>();
intersections = new List<string>();
subject = new EfficientIterator<ItemA, ItemB>((a, b) => String.CompareOrdinal(a.Identifier, b.Name));
subject.Always = (a, b) => count++;
subject.OneOnly = a => listOneOrphans.Add(a.Identifier);
subject.TwoOnly = b => listTwoOrphans.Add(b.Name);
subject.Match = (a, b) => intersections.Add(a.Identifier);
};
}
public class Efficient_iterator_when_both_lists_have_elements_and_list_one_leads : EfficientIteratorTests
{
private Establish context = () =>
{
listOne = new List<ItemA> { new ItemA { Identifier = "A" }, new ItemA { Identifier = "B" } };
listTwo = new List<ItemB> { new ItemB { Name = "B" }, new ItemB { Name = "C" } };
};
private Because of = () => subject.RunToEnd(listOne, listTwo);
private It should_identify_A_as_a_list_one_orphan = () => listOneOrphans.ShouldContainOnly("A");
private It should_identify_C_as_a_list_two_orphan = () => listTwoOrphans.ShouldContainOnly("C");
private It should_identify_B_as_an_intersection = () => intersections.ShouldContainOnly("B");
private It should_increment_the_count_once_per_distinct_item = () => count.ShouldEqual(3);
}
public class Efficient_iterator_when_both_lists_have_elements_and_list_two_leads : EfficientIteratorTests
{
private Establish context = () =>
{
listOne = new List<ItemA> { new ItemA { Identifier = "B" }, new ItemA { Identifier = "C" } };
listTwo = new List<ItemB> { new ItemB { Name = "A" }, new ItemB { Name = "B" } };
};
private Because of = () => subject.RunToEnd(listOne, listTwo);
private It should_identify_A_as_a_list_one_orphan = () => listOneOrphans.ShouldContainOnly("C");
private It should_identify_C_as_a_list_two_orphan = () => listTwoOrphans.ShouldContainOnly("A");
private It should_identify_B_as_an_intersection = () => intersections.ShouldContainOnly("B");
private It should_increment_the_count_once_per_distinct_item = () => count.ShouldEqual(3);
}
public class Efficient_iterator_when_only_list_one_has_elements : EfficientIteratorTests
{
private Establish context = () =>
{
listOne = new List<ItemA> { new ItemA { Identifier = "A" }, new ItemA { Identifier = "B" } };
listTwo = new List<ItemB>();
};
private Because of = () => subject.RunToEnd(listOne, listTwo);
private It should_identify_A_as_a_list_one_orphan = () => listOneOrphans.ShouldContain("A");
private It should_identify_B_as_a_list_one_orphan = () => listOneOrphans.ShouldContain("B");
private It should_identify_no_intersections = () => intersections.ShouldBeEmpty();
private It should_increment_the_count_once_per_distinct_item = () => count.ShouldEqual(2);
}
public class Efficient_iterator_when_only_list_two_has_elements : EfficientIteratorTests
{
private Establish context = () =>
{
listOne = new List<ItemA>();
listTwo = new List<ItemB> { new ItemB { Name = "B" }, new ItemB { Name = "C" } };
};
private Because of = () => subject.RunToEnd(listOne, listTwo);
private It should_identify_B_as_a_list_two_orphan = () => listTwoOrphans.ShouldContain("B");
private It should_identify_C_as_a_list_two_orphan = () => listTwoOrphans.ShouldContain("C");
private It should_identify_no_intersections = () => intersections.ShouldBeEmpty();
private It should_increment_the_count_once_per_distinct_item = () => count.ShouldEqual(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment