summaryrefslogtreecommitdiff
path: root/product/DataAccess/Transactions/TrackerEntrySpecs.cs
blob: c1c13099501e76978a7ee387295ab364508f6fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using developwithpassion.bdd.contexts;
using Gorilla.Commons.Testing;
using Gorilla.Commons.Utility.Core;
using Gorilla.Commons.Utility.Extensions;

namespace Gorilla.Commons.Infrastructure.Transactions
{
    public class TrackerEntrySpecs
    {
    }

    public abstract class behaves_like_tracker_entry : concerns_for<ITrackerEntry<Pillow>>
    {
    }

    [Concern(typeof (ITrackerEntry<>))]
    public class when_comparing_the_current_instance_of_a_component_with_its_original_and_it_has_changes :
        behaves_like_tracker_entry
    {
        it should_indicate_that_there_are_changes = () => result.should_be_true();

        because b = () => { result = sut.has_changes(); };

        public override ITrackerEntry<Pillow> create_sut()
        {
            return new TrackerEntry<Pillow>(new Pillow("pink"), new Pillow("yellow"));
        }

        static bool result;
    }

    [Concern(typeof (ITrackerEntry<>))]
    public class when_the_original_instance_has_a_null_field_that_is_now_not_null :
        behaves_like_tracker_entry
    {
        it should_indicate_that_there_are_changes = () => result.should_be_true();

        because b = () => { result = sut.has_changes(); };

        public override ITrackerEntry<Pillow> create_sut()
        {
            return new TrackerEntry<Pillow>(new Pillow(null), new Pillow("yellow"));
        }

        static bool result;
    }

    [Concern(typeof (ITrackerEntry<>))]
    public class when_the_original_instance_had_a_non_null_field_and_the_current_instance_has_a_null_field :
        behaves_like_tracker_entry
    {
        it should_indicate_that_there_are_changes = () => result.should_be_true();

        because b = () => { result = sut.has_changes(); };

        context c = () =>
                        {
                            var id = Guid.NewGuid();
                            original = new Pillow("green", id);
                            current = new Pillow(null, id);
                        };

        public override ITrackerEntry<Pillow> create_sut()
        {
            return new TrackerEntry<Pillow>(original, current);
        }

        static bool result;
        static Pillow original;
        static Pillow current;
    }

    [Concern(typeof (ITrackerEntry<>))]
    public class when_the_original_instance_has_the_same_value_as_the_current_instance :
        behaves_like_tracker_entry
    {
        it should_indicate_that_there_are_no_changes = () => result.should_be_false();

        because b = () => { result = sut.has_changes(); };

        context c = () =>
                        {
                            var id = Guid.NewGuid();
                            original = new Pillow("green", id);
                            current = new Pillow("green", id);
                        };

        public override ITrackerEntry<Pillow> create_sut()
        {
            return new TrackerEntry<Pillow>(original, current);
        }

        static bool result;
        static Pillow original;
        static Pillow current;
    }

    public class Pillow : IIdentifiable<Guid>
    {
        readonly string color;

        public Pillow(string color) : this(color, Guid.NewGuid())
        {
        }

        public Pillow(string color, Guid id)
        {
            this.color = color;
            this.id = id;
        }

        public Id<Guid> id { get; set; }

        public bool Equals(Pillow other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.id.Equals(id);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Pillow)) return false;
            return Equals((Pillow) obj);
        }

        public override int GetHashCode()
        {
            return id.GetHashCode();
        }

        public override string ToString()
        {
            return "{0} id: {1}".formatted_using(base.ToString(), id);
        }
    }
}