main
1using System;
2using System.IO;
3using jive;
4using Machine.Specifications;
5
6namespace specs.unit.cloning
7{
8 [Subject(typeof (BinarySerializer<TestItem>))]
9 public abstract class when_a_file_is_specified_to_serialize_an_item_to
10 {
11 static Serializer<TestItem> create_sut()
12 {
13 return new BinarySerializer<TestItem>(file_name);
14 }
15
16 Establish c = () =>
17 {
18 file_name = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serialized.dat");
19
20 sut = create_sut();
21 };
22
23 Cleanup aeo = () =>
24 {
25 if (File.Exists(file_name)) File.Delete(file_name);
26 };
27
28 static protected string file_name;
29 static protected Serializer<TestItem> sut;
30 }
31
32 [Subject(typeof (BinarySerializer<TestItem>))]
33 public class when_serializing_an_item : when_a_file_is_specified_to_serialize_an_item_to
34 {
35 It should_serialize_the_item_to_a_file = () => File.Exists(file_name).should_be_true();
36
37 Because b = () => sut.serialize(new TestItem(string.Empty));
38 }
39
40 [Subject(typeof (BinarySerializer<TestItem>))]
41 public class when_deserializing_an_item : when_a_file_is_specified_to_serialize_an_item_to
42 {
43 It should_be_able_to_deserialize_from_a_serialized_file = () => result.should_be_equal_to(original);
44
45 Establish c = () =>
46 {
47 original = new TestItem("hello world");
48 };
49
50 Because b = () =>
51 {
52 sut.serialize(original);
53 result = sut.deserialize();
54 };
55
56 static TestItem original;
57 static TestItem result;
58 }
59
60 [Serializable]
61 public class TestItem : IEquatable<TestItem>
62 {
63 public TestItem(string text)
64 {
65 Text = text;
66 }
67
68 public string Text { get; set; }
69
70 public bool Equals(TestItem testItem)
71 {
72 return testItem != null;
73 }
74
75 public override bool Equals(object obj)
76 {
77 return ReferenceEquals(this, obj) || Equals(obj as TestItem);
78 }
79
80 public override int GetHashCode()
81 {
82 return 0;
83 }
84 }
85}