Skip to content

Commit

Permalink
Add tests and fix base copier
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Nov 2, 2024
1 parent 00f41d9 commit af16a0c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Orleans.Serialization/Codecs/CollectionCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public Collection<T> DeepCopy(Collection<T> input, CopyContext context)
/// <inheritdoc/>
public void DeepCopy(Collection<T> input, Collection<T> output, CopyContext context)
{
output.Clear();

foreach (var item in input)
{
output.Add(_copier.DeepCopy(item, context));
Expand Down
50 changes: 50 additions & 0 deletions test/Orleans.Serialization.UnitTests/BuiltInCodecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,56 @@ protected override Collection<int> CreateValue()
protected override Collection<int>[] TestValues => [null, new Collection<int>(), CreateValue(), CreateValue(), CreateValue()];
}

[GenerateSerializer]
public class TypeWithCollectionBase : Collection<int>
{
public TypeWithCollectionBase() : this(true) { }
public TypeWithCollectionBase(bool addDefaultValue)
{
if (addDefaultValue)
{
this.Add(42);
}
}

[Id(0)]
public int OtherProperty { get; set; }

public override string ToString() => $"[OtherProperty: {OtherProperty}, Values: [{string.Join(", ", this)}]]";
}

public class CollectionBaseCodecTests(ITestOutputHelper output) : FieldCodecTester<TypeWithCollectionBase, IFieldCodec<TypeWithCollectionBase>>(output)
{
private static TypeWithCollectionBase AddValues(TypeWithCollectionBase value)
{
value.Add(1);
value.Add(2);
value.Add(3);
return value;
}

protected override TypeWithCollectionBase[] TestValues => [null, new(), new(addDefaultValue: false), new() { 15 }, AddValues(new() { OtherProperty = 123 })];

protected override TypeWithCollectionBase CreateValue() => AddValues(new() { OtherProperty = Random.Next() });
protected override bool Equals(TypeWithCollectionBase left, TypeWithCollectionBase right) => ReferenceEquals(left, right) || left.SequenceEqual(right) && left.OtherProperty == right.OtherProperty;
}

public class CollectionBaseCopierTests(ITestOutputHelper output) : CopierTester<TypeWithCollectionBase, IDeepCopier<TypeWithCollectionBase>>(output)
{
private static TypeWithCollectionBase AddValues(TypeWithCollectionBase value)
{
value.Add(1);
value.Add(2);
value.Add(3);
return value;
}

protected override TypeWithCollectionBase[] TestValues => [null, new(), new(addDefaultValue: false), new() { 15 }, AddValues(new() { OtherProperty = 123 })];

protected override TypeWithCollectionBase CreateValue() => AddValues(new() { OtherProperty = Random.Next() });
protected override bool Equals(TypeWithCollectionBase left, TypeWithCollectionBase right) => ReferenceEquals(left, right) || left.SequenceEqual(right) && left.OtherProperty == right.OtherProperty;
}

public class ReadOnlyCollectionCodecTests(ITestOutputHelper output) : FieldCodecTester<ReadOnlyCollection<int>, ReadOnlyCollectionCodec<int>>(output)
{
protected override ReadOnlyCollection<int> CreateValue()
Expand Down

0 comments on commit af16a0c

Please sign in to comment.