Skip to content

Commit dbe2956

Browse files
committed
add parse and tryparse for string
1 parent bf5a5f7 commit dbe2956

File tree

516 files changed

+4938
-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.

516 files changed

+4938
-0
lines changed

src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ public override bool Equals(object? obj)
2929
public override string? ToString() => Value;
3030
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
3131
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
32+
33+
public static TESTID Parse(string? value) => new TESTID(value?.Trim());
34+
public static bool TryParse(string? value, out TESTID result)
35+
{
36+
result = new TESTID(value?.Trim());
37+
return true;
38+
}

src/StronglyTypedIds/Templates/String/String_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ public override bool Equals(object obj)
3030
public override string ToString() => Value;
3131
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
3232
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
33+
34+
public static TESTID Parse(string value) => new TESTID(value.Trim());
35+
public static bool TryParse(string value, out TESTID result)
36+
{
37+
if (string.IsNullOrWhiteSpace(value))
38+
{
39+
result = default;
40+
return false;
41+
}
42+
result = new TESTID(value.Trim());
43+
return true;
44+
}

test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs

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

67+
[Fact]
68+
public void CanParseSuccessfully()
69+
{
70+
var value = "123ABC";
71+
var foo = NullableStringId.Parse($" {value} ");
72+
var bar = new NullableStringId(value);
73+
74+
Assert.Equal(bar, foo);
75+
}
76+
77+
[Fact]
78+
public void CanTryParseSuccessfully()
79+
{
80+
var value = "123ABC";
81+
var result = NullableStringId.TryParse($" {value} ", out NullableStringId foo);
82+
var bar = new NullableStringId(value);
83+
84+
Assert.True(result);
85+
Assert.Equal(bar, foo);
86+
}
87+
6788
[Fact]
6889
public void CanSerializeToString_WithNewtonsoftJsonProvider()
6990
{

test/StronglyTypedIds.IntegrationTests/StringIdTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ public void DifferentTypesAreUnequal()
7070
Assert.NotEqual((object)bar, (object)foo);
7171
}
7272

73+
74+
[Fact]
75+
public void CanParseSuccessfully()
76+
{
77+
var value = "123ABC";
78+
var foo = StringId.Parse($" {value} ");
79+
var bar = new StringId(value);
80+
81+
Assert.Equal(bar, foo);
82+
}
83+
84+
85+
[Fact]
86+
public void CanTryParseSuccessfully()
87+
{
88+
var value = "123ABC";
89+
var result = StringId.TryParse($" {value} ", out StringId foo);
90+
var bar = new StringId(value);
91+
92+
Assert.True(result);
93+
Assert.Equal(bar, foo);
94+
}
95+
96+
97+
[Theory]
98+
[InlineData("")]
99+
[InlineData(" ")]
100+
[InlineData(null!)]
101+
public void CaTryParseFailOnInvalidStrings(string value)
102+
{
103+
var result = StringId.TryParse(value, out _);
104+
Assert.False(result);
105+
}
106+
73107
[Fact]
74108
public void CanSerializeToString_WithNewtonsoftJsonProvider()
75109
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,12 @@ namespace Some.Namespace
4343
public override string? ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string? value) => new MyTestId(value?.Trim());
48+
public static bool TryParse(string? value, out MyTestId result)
49+
{
50+
result = new MyTestId(value?.Trim());
51+
return true;
52+
}
4653
}
4754
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,12 @@ namespace Some.Namespace
4343
public override string? ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string? value) => new MyTestId(value?.Trim());
48+
public static bool TryParse(string? value, out MyTestId result)
49+
{
50+
result = new MyTestId(value?.Trim());
51+
return true;
52+
}
4653
}
4754
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ namespace Some.Namespace
4343
public override string? ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string? value) => new MyTestId(value?.Trim());
48+
public static bool TryParse(string? value, out MyTestId result)
49+
{
50+
result = new MyTestId(value?.Trim());
51+
return true;
52+
}
4653
public int CompareTo(MyTestId other)
4754
{
4855
return (Value, other.Value) switch

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ namespace Some.Namespace
4343
public override string? ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string? value) => new MyTestId(value?.Trim());
48+
public static bool TryParse(string? value, out MyTestId result)
49+
{
50+
result = new MyTestId(value?.Trim());
51+
return true;
52+
}
4653
public int CompareTo(MyTestId other)
4754
{
4855
return (Value, other.Value) switch

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,17 @@ namespace Some.Namespace
4343
public override string ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string value) => new MyTestId(value.Trim());
48+
public static bool TryParse(string value, out MyTestId result)
49+
{
50+
if (string.IsNullOrWhiteSpace(value))
51+
{
52+
result = default;
53+
return false;
54+
}
55+
result = new MyTestId(value.Trim());
56+
return true;
57+
}
4658
}
4759
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,17 @@ namespace Some.Namespace
4343
public override string ToString() => Value;
4444
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
4545
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
46+
47+
public static MyTestId Parse(string value) => new MyTestId(value.Trim());
48+
public static bool TryParse(string value, out MyTestId result)
49+
{
50+
if (string.IsNullOrWhiteSpace(value))
51+
{
52+
result = default;
53+
return false;
54+
}
55+
result = new MyTestId(value.Trim());
56+
return true;
57+
}
4658
}
4759
}

0 commit comments

Comments
 (0)