-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from Cysharp/hadashiA/default-value
Use default value from member declaration
- Loading branch information
Showing
5 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using MemoryPack.Tests.Models; | ||
|
||
namespace MemoryPack.Tests; | ||
|
||
public class DefaultValueTest | ||
{ | ||
[Fact] | ||
public void FieldDefaultValue() | ||
{ | ||
var bin = MemoryPackSerializer.Serialize(new DefaultValuePlaceholder { X = 1 }); | ||
var expected = new FieldDefaultValue(); | ||
var deserializedValue = MemoryPackSerializer.Deserialize<FieldDefaultValue>(bin)!; | ||
deserializedValue.Y.Should().Be(expected.Y); | ||
deserializedValue.Z.Should().Be(expected.Z); | ||
deserializedValue.S.Should().Be(expected.S); | ||
deserializedValue.B.Should().Be(expected.B); | ||
} | ||
|
||
[Fact] | ||
public void PropertyDefaultValue() | ||
{ | ||
var bin = MemoryPackSerializer.Serialize(new DefaultValuePlaceholder { X = 1 }); | ||
var expected = new PropertyDefaultValue(); | ||
var deserializedValue = MemoryPackSerializer.Deserialize<PropertyDefaultValue>(bin)!; | ||
deserializedValue.Y.Should().Be(expected.Y); | ||
deserializedValue.Z.Should().Be(expected.Z); | ||
deserializedValue.S.Should().Be(expected.S); | ||
deserializedValue.B.Should().Be(expected.B); | ||
} | ||
|
||
[Fact] | ||
public void CtorParamDefaultValue() | ||
{ | ||
var bin = MemoryPackSerializer.Serialize(new DefaultValuePlaceholder { X = 1 }); | ||
var expected = new CtorParamDefaultValue(1); | ||
var deserializedValue = MemoryPackSerializer.Deserialize<CtorParamDefaultValue>(bin)!; | ||
deserializedValue.Y.Should().Be(expected.Y); | ||
deserializedValue.Z.Should().Be(expected.Z); | ||
deserializedValue.S.Should().Be(expected.S); | ||
deserializedValue.B.Should().Be(expected.B); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace MemoryPack.Tests.Models; | ||
|
||
[MemoryPackable] | ||
partial class DefaultValuePlaceholder | ||
{ | ||
public int X { get; set; } | ||
} | ||
|
||
[MemoryPackable] | ||
partial class FieldDefaultValue | ||
{ | ||
public int X; | ||
public int Y = 12345; | ||
public float Z = 678.9f; | ||
public string S = "aaaaaaaaa"; | ||
public bool B = true; | ||
} | ||
|
||
[MemoryPackable] | ||
partial class PropertyDefaultValue | ||
{ | ||
public int X { get; set; } | ||
public int Y { get; set; } = 12345; | ||
public float Z { get; set; } = 678.9f; | ||
public string S { get; set; } = "aaaaaaaaa"; | ||
public bool B { get; set; } = true; | ||
} | ||
|
||
[MemoryPackable] | ||
partial class CtorParamDefaultValue | ||
{ | ||
public int X; | ||
public int Y; | ||
public float Z; | ||
public string S; | ||
public bool B; | ||
|
||
[MemoryPackConstructor] | ||
public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true) | ||
{ | ||
X = x; | ||
Y = y; | ||
Z = z; | ||
S = s; | ||
B = b; | ||
} | ||
} |