Skip to content

Commit f9a1e88

Browse files
committed
Written some tests to keep the blood flowing.
1 parent e566695 commit f9a1e88

File tree

5 files changed

+212
-1
lines changed

5 files changed

+212
-1
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using NUnit.Framework;
3+
using StreamSource.Framework;
4+
5+
namespace StreamSource
6+
{
7+
[TestFixture]
8+
public class MetadatumTests
9+
{
10+
[Test]
11+
public void NameCanNotBeNull()
12+
{
13+
Assert.Throws<ArgumentNullException>(() => new Metadatum(null, ""));
14+
}
15+
16+
[Test]
17+
public void ValueCanNotBeNull()
18+
{
19+
Assert.Throws<ArgumentNullException>(() => new Metadatum("", null));
20+
}
21+
22+
[Test]
23+
public void NameReturnsExpectedValue()
24+
{
25+
var name = Guid.NewGuid().ToString();
26+
var sut = new Metadatum(name, "");
27+
var result = sut.Name;
28+
Assert.That(result, Is.EqualTo(name));
29+
}
30+
31+
[Test]
32+
public void ValueReturnsExpectedValue()
33+
{
34+
var value = Guid.NewGuid().ToString();
35+
var sut = new Metadatum("", value);
36+
var result = sut.Value;
37+
Assert.That(result, Is.EqualTo(value));
38+
}
39+
40+
[Test]
41+
public void VerifyEquality()
42+
{
43+
var name1 = Guid.NewGuid().ToString();
44+
var name2 = Guid.NewGuid().ToString();
45+
var value1 = Guid.NewGuid().ToString();
46+
var value2 = Guid.NewGuid().ToString();
47+
new EqualityAssertion(new Metadatum("", "")).
48+
VerifyEqual(new Metadatum("", ""), new Metadatum("", "")).
49+
VerifyEqual(new Metadatum(name1, value1), new Metadatum(name1, value1)).
50+
VerifyNotEqual(new Metadatum(name1, value1), new Metadatum(name2, value1)).
51+
VerifyNotEqual(new Metadatum(name1, value1), new Metadatum(name1, value2)).
52+
Assert();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using NUnit.Framework;
3+
using StreamSource.Framework;
4+
5+
namespace StreamSource
6+
{
7+
[TestFixture]
8+
public class StreamChangeTests
9+
{
10+
[Test]
11+
public void MessageCanNotBeNull()
12+
{
13+
Assert.Throws<ArgumentNullException>(() =>
14+
new StreamChange(Guid.Empty, null, new Metadatum[0]));
15+
}
16+
17+
[Test]
18+
public void MetadataCanNotBeNull()
19+
{
20+
Assert.Throws<ArgumentNullException>(() =>
21+
new StreamChange(Guid.Empty, new object(), null));
22+
}
23+
24+
[Test]
25+
public void MetadataCanBeEmpty()
26+
{
27+
Assert.DoesNotThrow(() =>
28+
new StreamChange(Guid.Empty, new object(), new Metadatum[0]));
29+
}
30+
31+
[Test]
32+
public void MessageIdReturnsExpectedResult()
33+
{
34+
var messageId = Guid.NewGuid();
35+
var sut = new StreamChange(messageId, new object(), new Metadatum[0]);
36+
var result = sut.MessageId;
37+
Assert.That(result, Is.EqualTo(messageId));
38+
}
39+
40+
[Test]
41+
public void MessageReturnsExpectedResult()
42+
{
43+
var message = new object();
44+
var sut = new StreamChange(Guid.Empty, message, new Metadatum[0]);
45+
var result = sut.Message;
46+
Assert.That(result, Is.EqualTo(message));
47+
}
48+
49+
[Test]
50+
public void MetadataReturnsExpectedResult()
51+
{
52+
var metadata = new[]
53+
{
54+
new Metadatum("name1", "value1"),
55+
new Metadatum("name2", "value2"),
56+
new Metadatum("name1", "value2")
57+
};
58+
var sut = new StreamChange(Guid.Empty, new object(), metadata);
59+
var result = sut.Metadata;
60+
Assert.That(result, Is.EquivalentTo(metadata));
61+
}
62+
63+
[Test]
64+
public void VerifyEquality()
65+
{
66+
var messageId1 = Guid.NewGuid();
67+
var messageId2 = Guid.NewGuid();
68+
var message1 = new object();
69+
var message2 = new object();
70+
var metadata1 = new[]
71+
{
72+
new Metadatum("name1", "value1"),
73+
new Metadatum("name2", "value2"),
74+
new Metadatum("name1", "value2")
75+
};
76+
77+
var metadata2 = new[]
78+
{
79+
new Metadatum("name3", "value3"),
80+
new Metadatum("name4", "value4"),
81+
new Metadatum("name3", "value4")
82+
};
83+
new EqualityAssertion(new StreamChange(messageId1, message1, metadata1)).
84+
VerifyEqual(new StreamChange(messageId1, message1, metadata1), new StreamChange(messageId1, message1, metadata1)).
85+
VerifyNotEqual(new StreamChange(messageId1, message1, metadata1), new StreamChange(messageId2, message1, metadata1)).
86+
VerifyNotEqual(new StreamChange(messageId1, message1, metadata1), new StreamChange(messageId1, message2, metadata1)).
87+
VerifyNotEqual(new StreamChange(messageId1, message1, metadata1), new StreamChange(messageId1, message1, metadata2)).
88+
Assert();
89+
}
90+
}
91+
}

src/StreamSource.Tests/StreamSource.Tests.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
<ItemGroup>
4444
<Compile Include="Framework\EqualityAssertion.cs" />
4545
<Compile Include="Framework\EqualityAssertionOptions.cs" />
46+
<Compile Include="MetadatumTests.cs" />
4647
<Compile Include="Naming\StreamNameComponentTests.cs" />
4748
<Compile Include="Naming\StreamNameParseTests.cs" />
4849
<Compile Include="Naming\StreamNameTests.cs" />
4950
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="StreamChangeTests.cs" />
5052
</ItemGroup>
5153
<ItemGroup>
5254
<None Include="packages.config" />
@@ -57,7 +59,9 @@
5759
<Name>StreamSource</Name>
5860
</ProjectReference>
5961
</ItemGroup>
60-
<ItemGroup />
62+
<ItemGroup>
63+
<Folder Include="Serialization\" />
64+
</ItemGroup>
6165
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6266
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6367
Other similar extension points exist, see Microsoft.Common.targets.

src/StreamSource/Metadatum.cs

+28
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,33 @@ public Metadatum(string name, string value)
1414
Name = name;
1515
Value = value;
1616
}
17+
18+
public bool Equals(Metadatum other)
19+
{
20+
if (ReferenceEquals(other, null)) return false;
21+
return string.Equals(Name, other.Name) && string.Equals(Value, other.Value);
22+
}
23+
24+
public override bool Equals(object obj)
25+
{
26+
if (ReferenceEquals(null, obj)) return false;
27+
if (ReferenceEquals(this, obj)) return true;
28+
return obj.GetType() == GetType() && Equals((Metadatum)obj);
29+
}
30+
31+
public override int GetHashCode()
32+
{
33+
return Name.GetHashCode() ^ Value.GetHashCode();
34+
}
35+
36+
public static bool operator ==(Metadatum left, Metadatum right)
37+
{
38+
return Equals(left, right);
39+
}
40+
41+
public static bool operator !=(Metadatum left, Metadatum right)
42+
{
43+
return !Equals(left, right);
44+
}
1745
}
1846
}

src/StreamSource/StreamChange.cs

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace StreamSource
45
{
@@ -16,5 +17,37 @@ public StreamChange(Guid messageId, object message, Metadatum[] metadata)
1617
Message = message;
1718
Metadata = metadata;
1819
}
20+
21+
public bool Equals(StreamChange other)
22+
{
23+
if (ReferenceEquals(other, null)) return false;
24+
return MessageId == other.MessageId &&
25+
Equals(Message, other.Message) &&
26+
Metadata.SequenceEqual(other.Metadata);
27+
}
28+
29+
public override bool Equals(object other)
30+
{
31+
if (ReferenceEquals(other, null)) return false;
32+
if (ReferenceEquals(other, this)) return true;
33+
return other is StreamChange && Equals((StreamChange)other);
34+
}
35+
36+
public override int GetHashCode()
37+
{
38+
return Metadata.Aggregate(
39+
MessageId.GetHashCode() ^ Message.GetHashCode(),
40+
(current, next) => current ^ next.GetHashCode());
41+
}
42+
43+
public static bool operator ==(StreamChange left, StreamChange right)
44+
{
45+
return Equals(left, right);
46+
}
47+
48+
public static bool operator !=(StreamChange left, StreamChange right)
49+
{
50+
return !Equals(left, right);
51+
}
1952
}
2053
}

0 commit comments

Comments
 (0)