Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed May 17, 2024
1 parent 3d5f69a commit 217f5d1
Show file tree
Hide file tree
Showing 7 changed files with 1,923 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable
namespace SomeNamespace1
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
#endif
#if NET7_0_OR_GREATER
global::System.IParsable<MyId>, global::System.ISpanParsable<MyId>,
#endif
#if NET8_0_OR_GREATER
global::System.IUtf8SpanFormattable,
#endif
global::System.IComparable<MyId>, global::System.IEquatable<MyId>, global::System.IFormattable
{
public global::System.Guid Value { get; }

public MyId(global::System.Guid value)
{
Value = value;
}

public static MyId New() => new MyId(global::System.Guid.NewGuid());
public static readonly MyId Empty = new MyId(global::System.Guid.Empty);

/// <inheritdoc cref="global::System.IEquatable{T}"/>
public bool Equals(MyId other) => this.Value.Equals(other.Value);
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is MyId other && Equals(other);
}

public override int GetHashCode() => Value.GetHashCode();

public override string ToString() => Value.ToString();

public static bool operator ==(MyId a, MyId b) => a.Equals(b);
public static bool operator !=(MyId a, MyId b) => !(a == b);
public static bool operator > (MyId a, MyId b) => a.CompareTo(b) > 0;
public static bool operator < (MyId a, MyId b) => a.CompareTo(b) < 0;
public static bool operator >= (MyId a, MyId b) => a.CompareTo(b) >= 0;
public static bool operator <= (MyId a, MyId b) => a.CompareTo(b) <= 0;

/// <inheritdoc cref="global::System.IComparable{TSelf}"/>
public int CompareTo(MyId other) => Value.CompareTo(other.Value);

public partial class MyIdTypeConverter : global::System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Type sourceType)
{
return sourceType == typeof(global::System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object? ConvertFrom(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Globalization.CultureInfo? culture, object value)
{
return value switch
{
global::System.Guid guidValue => new MyId(guidValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && global::System.Guid.TryParse(stringValue, out var result) => new MyId(result),
_ => base.ConvertFrom(context, culture, value),
};
}

public override bool CanConvertTo(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Type? sourceType)
{
return sourceType == typeof(global::System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
}

public override object? ConvertTo(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Globalization.CultureInfo? culture, object? value, global::System.Type destinationType)
{
if (value is MyId idValue)
{
if (destinationType == typeof(global::System.Guid))
{
return idValue.Value;
}

if (destinationType == typeof(string))
{
return idValue.Value.ToString();
}
}

return base.ConvertTo(context, culture, value, destinationType);
}
}

public partial class MyIdSystemTextJsonConverter : global::System.Text.Json.Serialization.JsonConverter<MyId>
{
public override bool CanConvert(global::System.Type typeToConvert)
=> typeToConvert == typeof(global::System.Guid) || typeToConvert == typeof(string) || base.CanConvert(typeToConvert);

public override MyId Read(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
=> new (reader.GetGuid());

public override void Write(global::System.Text.Json.Utf8JsonWriter writer, MyId value, global::System.Text.Json.JsonSerializerOptions options)
=> writer.WriteStringValue(value.Value);

#if NET6_0_OR_GREATER
public override MyId ReadAsPropertyName(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
=> new(global::System.Guid.Parse(reader.GetString() ?? throw new global::System.FormatException("The string for the MyId property was null")));

public override void WriteAsPropertyName(global::System.Text.Json.Utf8JsonWriter writer, MyId value, global::System.Text.Json.JsonSerializerOptions options)
=> writer.WritePropertyName(value.Value.ToString());
#endif
}

public static MyId Parse(string input)
=> new(global::System.Guid.Parse(input));

#if NET7_0_OR_GREATER
/// <inheritdoc cref="global::System.IParsable{TSelf}"/>
public static MyId Parse(string input, global::System.IFormatProvider? provider)
=> new(global::System.Guid.Parse(input, provider));

/// <inheritdoc cref="global::System.IParsable{TSelf}"/>
public static bool TryParse(
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? input,
global::System.IFormatProvider? provider,
out MyId result)
{
if (input is null)
{
result = default;
return false;
}

if (global::System.Guid.TryParse(input, provider, out var guid))
{
result = new(guid);
return true;
}
else
{
result = default;
return false;
}
}
#endif

/// <inheritdoc cref="global::System.IFormattable"/>
public string ToString(
#if NET7_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
#endif
string? format,
global::System.IFormatProvider? formatProvider)
=> Value.ToString(format, formatProvider);

#if NETCOREAPP2_1_OR_GREATER
public static MyId Parse(global::System.ReadOnlySpan<char> input)
=> new(global::System.Guid.Parse(input));
#endif

#if NET6_0_OR_GREATER
#if NET7_0_OR_GREATER
/// <inheritdoc cref="global::System.ISpanParsable{TSelf}"/>
#endif
public static MyId Parse(global::System.ReadOnlySpan<char> input, global::System.IFormatProvider? provider)
#if NET7_0_OR_GREATER
=> new(global::System.Guid.Parse(input, provider));
#else
=> new(global::System.Guid.Parse(input));
#endif

#if NET7_0_OR_GREATER
/// <inheritdoc cref="global::System.ISpanParsable{TSelf}"/>
#endif
public static bool TryParse(global::System.ReadOnlySpan<char> input, global::System.IFormatProvider? provider, out MyId result)
{
#if NET7_0_OR_GREATER
if (global::System.Guid.TryParse(input, provider, out var guid))
#else
if (global::System.Guid.TryParse(input, out var guid))
#endif
{
result = new(guid);
return true;
}
else
{
result = default;
return false;
}
}

/// <inheritdoc cref="global::System.ISpanFormattable"/>
public bool TryFormat(
global::System.Span<char> destination,
out int charsWritten,
#if NET7_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
#endif
global::System.ReadOnlySpan<char> format,
global::System.IFormatProvider? provider)
=> Value.TryFormat(destination, out charsWritten, format);

/// <inheritdoc cref="global::System.ISpanFormattable"/>
public bool TryFormat(
global::System.Span<char> destination,
out int charsWritten,
#if NET7_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
#endif
global::System.ReadOnlySpan<char> format = default)
=> Value.TryFormat(destination, out charsWritten, format);
#endif
#if NET8_0_OR_GREATER
/// <inheritdoc cref="global::System.IUtf8SpanFormattable.TryFormat" />
public bool TryFormat(
global::System.Span<byte> utf8Destination,
out int bytesWritten,
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
global::System.ReadOnlySpan<char> format,
global::System.IFormatProvider? provider)
=> Value.TryFormat(utf8Destination, out bytesWritten, format);
#endif
}
}

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable
namespace SomeNamespace2
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyIdConverters
{
public partial class DapperTypeHandler : global::Dapper.SqlMapper.TypeHandler<SomeNamespace1.MyId>
{
public override void SetValue(global::System.Data.IDbDataParameter parameter, SomeNamespace1.MyId value)
{
parameter.Value = value.Value;
}

public override SomeNamespace1.MyId Parse(object value)
{
return value switch
{
global::System.Guid guidValue => new SomeNamespace1.MyId(guidValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && global::System.Guid.TryParse(stringValue, out var result) => new SomeNamespace1.MyId(result),
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to SomeNamespace1.MyId"),
};
}
}
}
}
Loading

0 comments on commit 217f5d1

Please sign in to comment.