Skip to content

Commit 980dcae

Browse files
committed
Add tests for struct with operators
1 parent 5b02c44 commit 980dcae

4 files changed

Lines changed: 83 additions & 15 deletions

File tree

src/ImmutableObjectGraph.Generation.Tests/TestSources/ImmutableWithComplexStructField.Tests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,55 @@
1010
public class ImmutableWithComplexStructFieldTests
1111
{
1212
[Fact]
13-
public void AssignVariousValues()
13+
public void StructWithoutOperatorsAlwaysRecreatesObjectWithChangedValue()
1414
{
1515
var originalStruct = new SomeStructWithMultipleFields(5, 0);
1616
var structWithModifiedSecondField = new SomeStructWithMultipleFields(5, 1);
1717
var v1 = ImmutableWithComplexStructField.Create(someStructField: originalStruct);
1818
var v2 = v1.With(someStructField: structWithModifiedSecondField);
1919
Assert.Equal(structWithModifiedSecondField.Field2, v2.SomeStructField.Field2);
2020
}
21+
22+
[Fact]
23+
public void StructWithoutOperatorsAlwaysRecreatesObjectWithSameValue()
24+
{
25+
var s1 = new SomeStructWithMultipleFields(1, 2);
26+
var v1 = ImmutableWithComplexStructField.Create(someStructField: s1);
27+
var v2 = v1.With(someStructField: s1);
28+
29+
// The object should have been recreated since equality between the two struct values
30+
// cannot be determined without their operator defined.
31+
Assert.NotSame(v1, v2);
32+
Assert.Equal(s1.Field1, v2.SomeStructField.Field1);
33+
}
34+
35+
[Fact]
36+
public void StructWithoutOperatorsAlwaysRecreatesObjectWithoutValue()
37+
{
38+
var s1 = new SomeStructWithMultipleFields(1, 2);
39+
var v1 = ImmutableWithComplexStructField.Create(someStructField: s1);
40+
var v2 = v1.With(); // omit the struct value altogether
41+
Assert.Same(v1, v2);
42+
}
43+
44+
[Fact]
45+
public void StructWithOperatorsRecreatesObjectWithChangedValue()
46+
{
47+
var s12 = new SomeStructWithMultipleFieldsAndOperator(1, 2);
48+
var s13 = new SomeStructWithMultipleFieldsAndOperator(1, 3);
49+
var v1 = ImmutableWithComplexStructField.Create(someStructFieldWithOperator: s12);
50+
var v2 = v1.With(someStructFieldWithOperator: s13);
51+
Assert.NotSame(v1, v2);
52+
Assert.Equal(s13.Field2, v2.SomeStructFieldWithOperator.Field2);
53+
}
54+
55+
[Fact]
56+
public void StructWithOperatorsRecyclesObjectWithSameValue()
57+
{
58+
var s12 = new SomeStructWithMultipleFieldsAndOperator(1, 2);
59+
var v1 = ImmutableWithComplexStructField.Create(someStructFieldWithOperator: s12);
60+
var v2 = v1.With(someStructFieldWithOperator: s12);
61+
Assert.Same(v1, v2);
62+
}
2163
}
2264
}
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
namespace ImmutableObjectGraph.Generation.Tests.TestSources
22
{
3+
using System;
4+
35
[GenerateImmutable]
46
partial class ImmutableWithComplexStructField
57
{
68
readonly SomeStructWithMultipleFields someStructField;
9+
10+
readonly SomeStructWithMultipleFieldsAndOperator someStructFieldWithOperator;
711
}
812

913
struct SomeStructWithMultipleFields
@@ -17,15 +21,38 @@ internal SomeStructWithMultipleFields(int field1, int field2)
1721
internal int Field1 { get; }
1822

1923
internal int Field2 { get; }
24+
}
25+
26+
struct SomeStructWithMultipleFieldsAndOperator
27+
{
28+
internal SomeStructWithMultipleFieldsAndOperator(int field1, int field2)
29+
{
30+
this.Field1 = field1;
31+
this.Field2 = field2;
32+
}
2033

21-
////public static bool operator ==(SomeStructWithMultipleFields one, SomeStructWithMultipleFields two)
22-
////{
23-
//// return one.Field1 == two.Field1 && one.Field2 == two.Field2;
24-
////}
34+
internal int Field1 { get; }
35+
36+
internal int Field2 { get; }
2537

26-
////public static bool operator !=(SomeStructWithMultipleFields one, SomeStructWithMultipleFields two)
27-
////{
28-
//// return !(one == two);
29-
////}
38+
public static bool operator ==(SomeStructWithMultipleFieldsAndOperator one, SomeStructWithMultipleFieldsAndOperator two)
39+
{
40+
return one.Field1 == two.Field1 && one.Field2 == two.Field2;
41+
}
42+
43+
public static bool operator !=(SomeStructWithMultipleFieldsAndOperator one, SomeStructWithMultipleFieldsAndOperator two)
44+
{
45+
return !(one == two);
46+
}
47+
48+
public override bool Equals(object obj)
49+
{
50+
throw new NotImplementedException();
51+
}
52+
53+
public override int GetHashCode()
54+
{
55+
throw new NotImplementedException();
56+
}
3057
}
3158
}

src/ImmutableObjectGraph/Optional.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
namespace ImmutableObjectGraph {
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
namespace ImmutableObjectGraph
2+
{
3+
using System.Diagnostics;
74

85
public static class Optional {
6+
[DebuggerStepThrough]
97
public static Optional<T> For<T>(T value) {
108
return value;
119
}

src/ImmutableObjectGraph/Optional`1.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static implicit operator Optional<T>(T value) {
5757
/// </summary>
5858
/// <param name="defaultValue">The default value to use if a value was not specified.</param>
5959
/// <returns>The value.</returns>
60+
[DebuggerStepThrough]
6061
public T GetValueOrDefault(T defaultValue) {
6162
return this.IsDefined ? this.value : defaultValue;
6263
}

0 commit comments

Comments
 (0)