main
  1using gorilla.utility;
  2using Machine.Specifications;
  3using solidware.financials.service.domain.hierarchy;
  4
  5namespace specs.unit.service.domain.hierarchy
  6{
  7    public class HierarchySpecs
  8    {
  9        public abstract class concern 
 10        {
 11            Establish context = () =>
 12            {
 13                sut = new Hierarchy();
 14            };
 15
 16            static protected Hierarchy sut;
 17        }
 18
 19        [Concern(typeof (Hierarchy))]
 20        public class when_visiting_item_in_a_hierarchy : concern
 21        {
 22            Establish c = () =>
 23            {
 24                visitor = Create.an<Visitor<Hierarchy>>();
 25                middle = new Hierarchy();
 26                bottom = new Hierarchy();
 27            };
 28
 29            Because b = () =>
 30            {
 31                top = sut;
 32                top.add(middle);
 33                middle.add(bottom);
 34                top.accept(visitor);
 35            };
 36
 37            It should_visit_everyone = () =>
 38            {
 39                visitor.received(x => x.visit(top));
 40                visitor.received(x => x.visit(top));
 41                visitor.received(x => x.visit(middle));
 42                visitor.received(x => x.visit(bottom));
 43            };
 44
 45            static Visitor<Hierarchy> visitor;
 46            static Hierarchy top;
 47            static Hierarchy middle;
 48            static Hierarchy bottom;
 49        }
 50
 51        [Concern(typeof (Hierarchy))]
 52        public class when_moving_a_sub_tree_from_one_tree_to_another : concern
 53        {
 54            Establish c = () =>
 55            {
 56                old_tree = new Hierarchy();
 57                new_tree = new Hierarchy();
 58                child = new Hierarchy();
 59            };
 60
 61            Because b = () =>
 62            {
 63                old_tree.add(child);
 64                child.move_to(new_tree);
 65            };
 66
 67            It should_remove_the_sub_tree_from_the_old_tree = () =>
 68            {
 69                old_tree.contains(child).should_be_false();
 70                child.belongs_to(old_tree).should_be_false();
 71            };
 72
 73            It should_add_the_sub_tree_to_the_new_one = () =>
 74            {
 75                new_tree.contains(child).should_be_true();
 76                child.belongs_to(new_tree).should_be_true();
 77            };
 78
 79            static Hierarchy old_tree;
 80            static Hierarchy new_tree;
 81            static Hierarchy child;
 82        }
 83
 84        [Concern(typeof (Hierarchy))]
 85        public class when_removing_a_descendant_from_a_tree_that_it_does_not_belong_to : concern
 86        {
 87            Establish c = () =>
 88            {
 89                orphan = new Hierarchy();
 90            };
 91
 92            Because b = () =>
 93            {
 94                sut.remove(orphan);
 95            };
 96
 97            It should_ignore_the_descdendant = () =>
 98            {
 99                sut.contains(orphan).should_be_false();
100                orphan.belongs_to(sut).should_be_false();
101            };
102
103            static Hierarchy orphan;
104        }
105
106        [Concern(typeof (Hierarchy))]
107        public class when_moving_a_child_to_another_tree_but_does_not_already_belong_to_a_tree : concern
108        {
109            Establish c = () =>
110            {
111                orphan = new Hierarchy();
112            };
113
114            Because b = () =>
115            {
116                orphan.move_to(sut);
117            };
118
119            It should_move_the_orphan_to_the_new_family = () =>
120            {
121                sut.contains(orphan).should_be_true();
122                orphan.belongs_to(sut).should_be_true();
123            };
124
125            static Hierarchy orphan;
126        }
127    }
128}