summaryrefslogtreecommitdiff
path: root/spec/unit/infrastructure/cloning/BinarySerializerSpecs.cs
blob: 448236efa8f322089dbdd5807f723865acfc1a58 (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
using System;
using System.IO;
using gorilla.infrastructure.cloning;
using Machine.Specifications;

namespace specs.unit.infrastructure.cloning
{
    [Subject(typeof (BinarySerializer<TestItem>))]
    public abstract class when_a_file_is_specified_to_serialize_an_item_to
    {
        static Serializer<TestItem> create_sut()
        {
            return new BinarySerializer<TestItem>(file_name);
        }

        Establish c = () =>
        {
            file_name = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serialized.dat");

            sut = create_sut();
        };

        Cleanup aeo = () =>
        {
            if (File.Exists(file_name)) File.Delete(file_name);
        };

        static protected string file_name;
        static protected Serializer<TestItem> sut;
    }

    [Subject(typeof (BinarySerializer<TestItem>))]
    public class when_serializing_an_item : when_a_file_is_specified_to_serialize_an_item_to
    {
        It should_serialize_the_item_to_a_file = () => File.Exists(file_name).should_be_true();

        Because b = () => sut.serialize(new TestItem(string.Empty));
    }

    [Subject(typeof (BinarySerializer<TestItem>))]
    public class when_deserializing_an_item : when_a_file_is_specified_to_serialize_an_item_to
    {
        It should_be_able_to_deserialize_from_a_serialized_file = () => result.should_be_equal_to(original);

        Establish c = () =>
        {
            original = new TestItem("hello world");
        };

        Because b = () =>
        {
            sut.serialize(original);
            result = sut.deserialize();
        };

        static TestItem original;
        static TestItem result;
    }

    [Serializable]
    public class TestItem : IEquatable<TestItem>
    {
        public TestItem(string text)
        {
            Text = text;
        }

        public string Text { get; set; }

        public bool Equals(TestItem testItem)
        {
            return testItem != null;
        }

        public override bool Equals(object obj)
        {
            return ReferenceEquals(this, obj) || Equals(obj as TestItem);
        }

        public override int GetHashCode()
        {
            return 0;
        }
    }
}