Skip to content

Commit bf5a5f7

Browse files
committed
add parse and tryparse
1 parent 6d36325 commit bf5a5f7

File tree

818 files changed

+12475
-699
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

818 files changed

+12475
-699
lines changed

src/StronglyTypedIds/Templates/Guid/Guid_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ public override bool Equals(object obj)
2222
public override string ToString() => Value.ToString();
2323
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2424
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
25+
26+
public static TESTID Parse(string value) => new TESTID(System.Guid.Parse(value));
27+
public static bool TryParse(string value, out TESTID result)
28+
{
29+
if (System.Guid.TryParse(value, out System.Guid parseResult))
30+
{
31+
result = new TESTID(parseResult);
32+
return true;
33+
}
34+
result = default;
35+
return false;
36+
}

src/StronglyTypedIds/Templates/Int/Int_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ public override bool Equals(object obj)
2121
public override string ToString() => Value.ToString();
2222
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2323
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
24+
25+
public static TESTID Parse(string value) => new TESTID(int.Parse(value));
26+
public static bool TryParse(string value, out TESTID result)
27+
{
28+
if (int.TryParse(value, out int parseResult))
29+
{
30+
result = new TESTID(parseResult);
31+
return true;
32+
}
33+
result = default;
34+
return false;
35+
}

src/StronglyTypedIds/Templates/Long/Long_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ public override bool Equals(object obj)
2121
public override string ToString() => Value.ToString();
2222
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2323
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
24+
25+
public static TESTID Parse(string value) => new TESTID(long.Parse(value));
26+
public static bool TryParse(string value, out TESTID result)
27+
{
28+
if (long.TryParse(value, out long parseResult))
29+
{
30+
result = new TESTID(parseResult);
31+
return true;
32+
}
33+
result = default;
34+
return false;
35+
}

test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ public void CantCreateEmptyGeneratedId1()
7474
Assert.NotEqual((object)bar, (object)foo);
7575
}
7676

77+
[Fact]
78+
public void CanParseString()
79+
{
80+
var value = Guid.NewGuid();
81+
var foo = GuidId1.Parse(value.ToString());
82+
var bar = new GuidId1(value);
83+
84+
Assert.Equal(bar, foo);
85+
}
86+
87+
[Fact]
88+
public void ThrowWhenInvalidParseString()
89+
{
90+
Assert.Throws<FormatException>(() => GuidId1.Parse(""));
91+
}
92+
93+
[Fact]
94+
public void CanFailTryParse()
95+
{
96+
var result = GuidId1.TryParse("", out _);
97+
Assert.False(result);
98+
}
99+
100+
101+
[Fact]
102+
public void CanTryParseSuccessfully()
103+
{
104+
var value = Guid.NewGuid();
105+
var result = GuidId1.TryParse(value.ToString(), out GuidId1 foo);
106+
var bar = new GuidId1(value);
107+
108+
Assert.True(result);
109+
Assert.Equal(bar, foo);
110+
}
111+
112+
77113
[Fact]
78114
public void CanSerializeToGuid_WithTypeConverter()
79115
{

test/StronglyTypedIds.IntegrationTests/IntIdTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ public void DifferentTypesAreUnequal()
6464
Assert.NotEqual((object)bar, (object)foo);
6565
}
6666

67+
[Fact]
68+
public void CanParseString()
69+
{
70+
var value = 1;
71+
var foo = IntId.Parse(value.ToString());
72+
var bar = new IntId(value);
73+
74+
Assert.Equal(bar, foo);
75+
}
76+
77+
[Fact]
78+
public void ThrowWhenInvalidParseString()
79+
{
80+
Assert.Throws<FormatException>(() => IntId.Parse(""));
81+
}
82+
83+
[Fact]
84+
public void CanFailTryParse()
85+
{
86+
var result = IntId.TryParse("", out _);
87+
Assert.False(result);
88+
}
89+
90+
91+
[Fact]
92+
public void CanTryParseSuccessfully()
93+
{
94+
var value = 2;
95+
var result = IntId.TryParse(value.ToString(), out IntId foo);
96+
var bar = new IntId(value);
97+
98+
Assert.True(result);
99+
Assert.Equal(bar, foo);
100+
}
101+
102+
103+
67104
[Fact]
68105
public void CanSerializeToInt_WithNewtonsoftJsonProvider()
69106
{

test/StronglyTypedIds.IntegrationTests/LongIdTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,42 @@ public void DifferentTypesAreUnequal()
6464
Assert.NotEqual((object)bar, (object)foo);
6565
}
6666

67+
[Fact]
68+
public void CanParseString()
69+
{
70+
var value = 1L;
71+
var foo = LongId.Parse(value.ToString());
72+
var bar = new LongId(value);
73+
74+
Assert.Equal(bar, foo);
75+
}
76+
77+
[Fact]
78+
public void ThrowWhenInvalidParseString()
79+
{
80+
Assert.Throws<FormatException>(() => LongId.Parse(""));
81+
}
82+
83+
[Fact]
84+
public void CanFailTryParse()
85+
{
86+
var result = LongId.TryParse("", out _);
87+
Assert.False(result);
88+
}
89+
90+
91+
[Fact]
92+
public void CanTryParseSuccessfully()
93+
{
94+
var value = 2L;
95+
var result = LongId.TryParse(value.ToString(), out LongId foo);
96+
var bar = new LongId(value);
97+
98+
Assert.True(result);
99+
Assert.Equal(bar, foo);
100+
}
101+
102+
67103
[Fact]
68104
public void CanSerializeToLong_WithNewtonsoftJsonProvider()
69105
{

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=0.verified.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//------------------------------------------------------------------------------
1+
//------------------------------------------------------------------------------
22
// <auto-generated>
33
// This code was generated by the StronglyTypedId source generator
44
//
@@ -35,5 +35,17 @@ namespace Some.Namespace
3535
public override string ToString() => Value.ToString();
3636
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3737
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
38+
39+
public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
if (System.Guid.TryParse(value, out System.Guid parseResult))
43+
{
44+
result = new MyTestId(parseResult);
45+
return true;
46+
}
47+
result = default;
48+
return false;
49+
}
3850
}
3951
}

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=2.verified.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//------------------------------------------------------------------------------
1+
//------------------------------------------------------------------------------
22
// <auto-generated>
33
// This code was generated by the StronglyTypedId source generator
44
//
@@ -35,5 +35,17 @@ namespace Some.Namespace
3535
public override string ToString() => Value.ToString();
3636
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3737
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
38+
39+
public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
if (System.Guid.TryParse(value, out System.Guid parseResult))
43+
{
44+
result = new MyTestId(parseResult);
45+
return true;
46+
}
47+
result = default;
48+
return false;
49+
}
3850
}
3951
}

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=4.verified.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//------------------------------------------------------------------------------
1+
//------------------------------------------------------------------------------
22
// <auto-generated>
33
// This code was generated by the StronglyTypedId source generator
44
//
@@ -35,6 +35,18 @@ namespace Some.Namespace
3535
public override string ToString() => Value.ToString();
3636
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3737
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
38+
39+
public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
if (System.Guid.TryParse(value, out System.Guid parseResult))
43+
{
44+
result = new MyTestId(parseResult);
45+
return true;
46+
}
47+
result = default;
48+
return false;
49+
}
3850
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
3951
}
4052
}

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=6.verified.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ namespace Some.Namespace
3535
public override string ToString() => Value.ToString();
3636
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3737
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
38+
39+
public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
if (System.Guid.TryParse(value, out System.Guid parseResult))
43+
{
44+
result = new MyTestId(parseResult);
45+
return true;
46+
}
47+
result = default;
48+
return false;
49+
}
3850
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
3951
}
4052
}

0 commit comments

Comments
 (0)