Created
October 19, 2009 15:49
-
-
Save jcbozonier/213473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using NUnit.Framework; | |
| namespace Katas.KataPotter2 | |
| { | |
| [TestFixture] | |
| public class When_grouping_three_books_where_two_are_the_same : potter_kata_context | |
| { | |
| [Test] | |
| public void There_should_be_two_sets() | |
| { | |
| Groups.Count().ShouldEqual(2); | |
| } | |
| [Test] | |
| public void Book_in_set_with_one_book_should_be_the_duplicate_book() | |
| { | |
| Groups | |
| .Where(x => x.Count == 1) | |
| .Single()[0].Id.ShouldEqual("1"); | |
| } | |
| [Test] | |
| public void One_set_should_have_one_book() | |
| { | |
| Groups.Any(x=>x.Count == 1).ShouldBeTrue(); | |
| } | |
| [Test] | |
| public void One_set_should_have_two_books() | |
| { | |
| Groups.Any(x => x.Count == 2).ShouldBeTrue(); | |
| } | |
| protected override void Because() | |
| { | |
| Groups = Grouper.Group(Books); | |
| } | |
| protected override void Context() | |
| { | |
| Books = new List<Book>() { new Book("0"), new Book("1"), new Book("1") }; | |
| } | |
| } | |
| [TestFixture] | |
| public class When_grouping_three_different_books : potter_kata_context | |
| { | |
| [Test] | |
| public void There_should_be_three_books_in_the_set() | |
| { | |
| Groups.First().Count.ShouldEqual(3); | |
| } | |
| [Test] | |
| public void There_should_be_only_one_set() | |
| { | |
| Groups.Count().ShouldEqual(1); | |
| } | |
| protected override void Because() | |
| { | |
| Groups = Grouper.Group(Books); | |
| } | |
| protected override void Context() | |
| { | |
| Books = new List<Book>() { new Book("0"), new Book("1"), new Book("2") }; | |
| } | |
| } | |
| [TestFixture] | |
| public class When_grouping_one_book : potter_kata_context | |
| { | |
| [Test] | |
| public void One_book_should_form_one_set() | |
| { | |
| Groups.Count().ShouldEqual(1); | |
| } | |
| [Test] | |
| public void There_should_be_one_book_in_set() | |
| { | |
| Groups.First().Count.ShouldEqual(1); | |
| } | |
| protected override void Because() | |
| { | |
| Groups = Grouper.Group(Books); | |
| } | |
| protected override void Context() | |
| { | |
| Books = new List<Book>() { new Book("0") }; | |
| } | |
| } | |
| public abstract class potter_kata_context | |
| { | |
| protected List<Book> Books; | |
| protected BookGrouper Grouper; | |
| protected IEnumerable<List<Book>> Groups; | |
| [SetUp] | |
| public void Setup() | |
| { | |
| Grouper = new BookGrouper(); | |
| Context(); | |
| Because(); | |
| } | |
| protected abstract void Because(); | |
| protected abstract void Context(); | |
| } | |
| public class BookGrouper | |
| { | |
| public IEnumerable<List<Book>> Group(List<Book> books) | |
| { | |
| var bookses = new List<List<Book>>(); | |
| var bookGroups = from book in books | |
| group book by book.Id | |
| into bookStack | |
| select new | |
| { | |
| BookId = bookStack.Key.ToString(), | |
| Books = bookStack | |
| }; | |
| foreach(var newGroup in bookGroups) | |
| { | |
| for(var i = 0; i< newGroup.Books.Count(); i++) | |
| { | |
| var bookList = new List<Book>(newGroup.Books); | |
| if(bookses.Count<=i) | |
| { | |
| bookses.Add(new List<Book>()); | |
| } | |
| bookses[i].Add(bookList[0]); | |
| } | |
| } | |
| return bookses; | |
| } | |
| } | |
| public class Book | |
| { | |
| public Book(string id) | |
| { | |
| Id = id; | |
| } | |
| public object Id { get; private set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment