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