Skip to content

Commit 5a455f0

Browse files
committed
add parse and tryparse for NewId
1 parent dbe2956 commit 5a455f0

File tree

258 files changed

+4147
-0
lines changed

Some content is hidden

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

258 files changed

+4147
-0
lines changed

src/StronglyTypedIds/Templates/NewId/NewId_Base.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ 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(new MassTransit.NewId(in value));
27+
public static bool TryParse(string value, out TESTID result)
28+
{
29+
try
30+
{
31+
result = new TESTID(new MassTransit.NewId(in value));
32+
return true;
33+
}
34+
catch
35+
{
36+
result = default;
37+
return false;
38+
}
39+
40+
}

test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs

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

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,21 @@ 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(new MassTransit.NewId(in value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
try
43+
{
44+
result = new MyTestId(new MassTransit.NewId(in value));
45+
return true;
46+
}
47+
catch
48+
{
49+
result = default;
50+
return false;
51+
}
52+
53+
}
3854
}
3955
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,21 @@ 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(new MassTransit.NewId(in value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
try
43+
{
44+
result = new MyTestId(new MassTransit.NewId(in value));
45+
return true;
46+
}
47+
catch
48+
{
49+
result = default;
50+
return false;
51+
}
52+
53+
}
3854
}
3955
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ 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(new MassTransit.NewId(in value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
try
43+
{
44+
result = new MyTestId(new MassTransit.NewId(in value));
45+
return true;
46+
}
47+
catch
48+
{
49+
result = default;
50+
return false;
51+
}
52+
53+
}
3854
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
3955
}
4056
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ 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(new MassTransit.NewId(in value));
40+
public static bool TryParse(string value, out MyTestId result)
41+
{
42+
try
43+
{
44+
result = new MyTestId(new MassTransit.NewId(in value));
45+
return true;
46+
}
47+
catch
48+
{
49+
result = default;
50+
return false;
51+
}
52+
53+
}
3854
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
3955
}
4056
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ namespace Some.Namespace
3838
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3939
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
4040

41+
public static MyTestId Parse(string value) => new MyTestId(new MassTransit.NewId(in value));
42+
public static bool TryParse(string value, out MyTestId result)
43+
{
44+
try
45+
{
46+
result = new MyTestId(new MassTransit.NewId(in value));
47+
return true;
48+
}
49+
catch
50+
{
51+
result = default;
52+
return false;
53+
}
54+
55+
}
56+
4157
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
4258
{
4359
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=2.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ namespace Some.Namespace
3838
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3939
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
4040

41+
public static MyTestId Parse(string value) => new MyTestId(new MassTransit.NewId(in value));
42+
public static bool TryParse(string value, out MyTestId result)
43+
{
44+
try
45+
{
46+
result = new MyTestId(new MassTransit.NewId(in value));
47+
return true;
48+
}
49+
catch
50+
{
51+
result = default;
52+
return false;
53+
}
54+
55+
}
56+
4157
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
4258
{
4359
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=4.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ namespace Some.Namespace
3737
public override string ToString() => Value.ToString();
3838
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3939
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
40+
41+
public static MyTestId Parse(string value) => new MyTestId(new MassTransit.NewId(in value));
42+
public static bool TryParse(string value, out MyTestId result)
43+
{
44+
try
45+
{
46+
result = new MyTestId(new MassTransit.NewId(in value));
47+
return true;
48+
}
49+
catch
50+
{
51+
result = default;
52+
return false;
53+
}
54+
55+
}
4056
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
4157

4258
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=6.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ namespace Some.Namespace
3737
public override string ToString() => Value.ToString();
3838
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
3939
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
40+
41+
public static MyTestId Parse(string value) => new MyTestId(new MassTransit.NewId(in value));
42+
public static bool TryParse(string value, out MyTestId result)
43+
{
44+
try
45+
{
46+
result = new MyTestId(new MassTransit.NewId(in value));
47+
return true;
48+
}
49+
catch
50+
{
51+
result = default;
52+
return false;
53+
}
54+
55+
}
4056
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
4157

4258
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter

0 commit comments

Comments
 (0)