Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions UnitsNet.Modular/UnitsNet.Modular.Generator/QuantityEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace UnitsNet.Modular.Generator;

internal static class QuantityEmitter
{
private const string NumericFormatStringSyntaxAttribute =
"global::System.Diagnostics.CodeAnalysis.StringSyntax(" +
"global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.NumericFormat)";

public static string Emit(
QuantitySelection selection,
IReadOnlyList<EmittedQuantityRelation> relationships,
Expand Down Expand Up @@ -222,8 +226,13 @@ public static string Emit(
writer.AppendLine(" public override int GetHashCode() => BaseValue.GetHashCode();");
writer.AppendLine(" public override string ToString() => ToString(null, null);");
writer.AppendLine(" public string ToString(global::System.IFormatProvider? formatProvider) => ToString(null, formatProvider);");
writer.AppendLine(" public string ToString(string? format) => ToString(format, null);");
writer.AppendLine(" public string ToString(string? format, global::System.IFormatProvider? formatProvider) =>");
writer.AppendLine(" public string ToString(");
writer.Append(" [").Append(NumericFormatStringSyntaxAttribute).AppendLine("]");
writer.AppendLine(" string? format) => ToString(format, null);");
writer.AppendLine(" public string ToString(");
writer.Append(" [").Append(NumericFormatStringSyntaxAttribute).AppendLine("]");
writer.AppendLine(" string? format,");
writer.AppendLine(" global::System.IFormatProvider? formatProvider) =>");
writer.AppendLine(" global::UnitsNet.Modular.SourceGen.QuantityOperations.Format(_value, Unit, format, formatProvider, Metadata);");
writer.AppendLine();
if (quantity.IsLogarithmic)
Expand Down
19 changes: 19 additions & 0 deletions UnitsNet.Modular/UnitsNet.Modular.Tests/GeneratedQuantityTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
using Fictional.Measurements;
using UnitsNet;
Expand All @@ -10,6 +12,23 @@ namespace UnitsNet.Modular.Tests;

public sealed class GeneratedQuantityTests
{
[Fact]
public void FormattingApis_IdentifyNumericFormatSyntax()
{
MethodInfo quantityToString = typeof(Length).GetMethod(nameof(Length.ToString), [typeof(string)])!;
StringSyntaxAttribute? quantitySyntax = quantityToString
.GetParameters()[0]
.GetCustomAttribute<StringSyntaxAttribute>();

MethodInfo descriptorFormat = typeof(IQuantityDescriptor).GetMethod(nameof(IQuantityDescriptor.Format))!;
StringSyntaxAttribute? descriptorSyntax = descriptorFormat
.GetParameters()[1]
.GetCustomAttribute<StringSyntaxAttribute>();

Assert.Equal(StringSyntaxAttribute.NumericFormat, quantitySyntax?.Syntax);
Assert.Equal(StringSyntaxAttribute.NumericFormat, descriptorSyntax?.Syntax);
}

[Fact]
public void GeneratedRegistry_SystemTextJsonRoundTripsBuiltInAndCustomQuantities()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ bool TryParse(
/// <summary>Formats a generated quantity after validating its concrete type.</summary>
string Format(
IQuantity<double> quantity,
[System.Diagnostics.CodeAnalysis.StringSyntax(System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.NumericFormat)]
string? format = null,
IFormatProvider? formatProvider = null);
}
Expand Down
Loading