main
  1using System;
  2using developwithpassion.bdd.contexts;
  3using Gorilla.Commons.Testing;
  4using gorilla.commons.utility;
  5
  6namespace momoney.database.transactions
  7{
  8    public class TrackerEntrySpecs {}
  9
 10    public abstract class behaves_like_tracker_entry : concerns_for<ITrackerEntry<Pillow>> {}
 11
 12    [Concern(typeof (ITrackerEntry<>))]
 13    public class when_comparing_the_current_instance_of_a_component_with_its_original_and_it_has_changes :
 14        behaves_like_tracker_entry
 15    {
 16        it should_indicate_that_there_are_changes = () => result.should_be_true();
 17
 18        because b = () =>
 19        {
 20            result = sut.has_changes();
 21        };
 22
 23        public override ITrackerEntry<Pillow> create_sut()
 24        {
 25            return new TrackerEntry<Pillow>(new Pillow("pink"), new Pillow("yellow"));
 26        }
 27
 28        static bool result;
 29    }
 30
 31    [Concern(typeof (ITrackerEntry<>))]
 32    public class when_the_original_instance_has_a_null_field_that_is_now_not_null :
 33        behaves_like_tracker_entry
 34    {
 35        it should_indicate_that_there_are_changes = () => result.should_be_true();
 36
 37        because b = () =>
 38        {
 39            result = sut.has_changes();
 40        };
 41
 42        public override ITrackerEntry<Pillow> create_sut()
 43        {
 44            return new TrackerEntry<Pillow>(new Pillow(null), new Pillow("yellow"));
 45        }
 46
 47        static bool result;
 48    }
 49
 50    [Concern(typeof (ITrackerEntry<>))]
 51    public class when_the_original_instance_had_a_non_null_field_and_the_current_instance_has_a_null_field :
 52        behaves_like_tracker_entry
 53    {
 54        it should_indicate_that_there_are_changes = () => result.should_be_true();
 55
 56        because b = () =>
 57        {
 58            result = sut.has_changes();
 59        };
 60
 61        context c = () =>
 62        {
 63            var id = Guid.NewGuid();
 64            original = new Pillow("green", id);
 65            current = new Pillow(null, id);
 66        };
 67
 68        public override ITrackerEntry<Pillow> create_sut()
 69        {
 70            return new TrackerEntry<Pillow>(original, current);
 71        }
 72
 73        static bool result;
 74        static Pillow original;
 75        static Pillow current;
 76    }
 77
 78    [Concern(typeof (ITrackerEntry<>))]
 79    public class when_the_original_instance_has_the_same_value_as_the_current_instance :
 80        behaves_like_tracker_entry
 81    {
 82        it should_indicate_that_there_are_no_changes = () => result.should_be_false();
 83
 84        because b = () =>
 85        {
 86            result = sut.has_changes();
 87        };
 88
 89        context c = () =>
 90        {
 91            var id = Guid.NewGuid();
 92            original = new Pillow("green", id);
 93            current = new Pillow("green", id);
 94        };
 95
 96        public override ITrackerEntry<Pillow> create_sut()
 97        {
 98            return new TrackerEntry<Pillow>(original, current);
 99        }
100
101        static bool result;
102        static Pillow original;
103        static Pillow current;
104    }
105
106    public class Pillow : Identifiable<Guid>
107    {
108        readonly string color;
109
110        public Pillow(string color) : this(color, Guid.NewGuid()) {}
111
112        public Pillow(string color, Guid id)
113        {
114            this.color = color;
115            this.id = id;
116        }
117
118        public Id<Guid> id { get; set; }
119
120        public bool Equals(Pillow other)
121        {
122            if (ReferenceEquals(null, other)) return false;
123            if (ReferenceEquals(this, other)) return true;
124            return other.id.Equals(id);
125        }
126
127        public override bool Equals(object obj)
128        {
129            if (ReferenceEquals(null, obj)) return false;
130            if (ReferenceEquals(this, obj)) return true;
131            if (obj.GetType() != typeof (Pillow)) return false;
132            return Equals((Pillow) obj);
133        }
134
135        public override int GetHashCode()
136        {
137            return id.GetHashCode();
138        }
139
140        public override string ToString()
141        {
142            return "{0} id: {1}".formatted_using(base.ToString(), id);
143        }
144    }
145}