Skip to content

Commit 9579448

Browse files
test: raise Humanizer coverage above 99 percent
1 parent 30d00a7 commit 9579448

8 files changed

Lines changed: 2918 additions & 55 deletions

File tree

src/Humanizer.SourceGenerators/Generators/ProfileCatalogs/WordsToNumberEngineContractFactory.cs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,54 @@ static string CreateVigesimalCompoundExpression(JsonElement root) =>
152152
/// token stripping, multiplier behavior, and ordinal handling, and the generated output
153153
/// becomes a single immutable rules object consumed by <c>TokenMapWordsToNumberConverter</c>.
154154
/// </summary>
155-
static string CreateTokenMapRulesExpression(JsonElement root) =>
156-
"new() { " +
157-
"CardinalMap = " + CreateStringLongFrozenDictionaryExpression(EngineContractUtilities.GetRequiredElement(root, "cardinalMap")) + ", " +
158-
"ExactOrdinalMap = " + CreateOptionalStringLongFrozenDictionaryExpression(root, "ordinalMap") + ", " +
159-
"OrdinalScaleMap = " + (root.TryGetProperty("ordinalScaleMap", out var ordinalScaleMap) ? CreateStringLongFrozenDictionaryExpression(ordinalScaleMap) : "null") + ", " +
160-
"GluedOrdinalScaleSuffixes = " + (root.TryGetProperty("gluedOrdinalScaleSuffixes", out var gluedOrdinalScaleSuffixes) ? CreateStringLongFrozenDictionaryExpression(gluedOrdinalScaleSuffixes) : "null") + ", " +
161-
"CompositeScaleMap = " + (root.TryGetProperty("compositeScaleMap", out var compositeScaleMap) ? CreateStringLongFrozenDictionaryExpression(compositeScaleMap) : "null") + ", " +
162-
"NormalizationProfile = TokenMapNormalizationProfile." + ToEnumMemberName(GetRequiredString(root, "normalizationProfile")) + ", " +
163-
"NegativePrefixes = " + CreateOptionalStringArrayExpression(root, "negativePrefixes") + ", " +
164-
"NegativeSuffixes = " + CreateOptionalStringArrayExpression(root, "negativeSuffixes") + ", " +
165-
"OrdinalPrefixes = " + CreateOptionalStringArrayExpression(root, "ordinalPrefixes") + ", " +
166-
"IgnoredTokens = " + CreateOptionalStringArrayExpression(root, "ignoredTokens") + ", " +
167-
"LeadingTokenPrefixesToTrim = " + CreateOptionalStringArrayExpression(root, "leadingTokenPrefixesToTrim") + ", " +
168-
"MultiplierTokens = " + CreateOptionalStringArrayExpression(root, "multiplierTokens") + ", " +
169-
"TokenSuffixesToStrip = " + CreateOptionalStringArrayExpression(root, "tokenSuffixesToStrip") + ", " +
170-
"OrdinalAbbreviationSuffixes = " + CreateOptionalStringArrayExpression(root, "ordinalAbbreviationSuffixes") + ", " +
171-
"TeenSuffixTokens = " + CreateOptionalStringArrayExpression(root, "teenSuffixTokens") + ", " +
172-
"HundredSuffixTokens = " + CreateOptionalStringArrayExpression(root, "hundredSuffixTokens") + ", " +
173-
"AllowTerminalOrdinalToken = " + (GetBoolean(root, "allowTerminalOrdinalToken") ? "true" : "false") + ", " +
174-
"UseHundredMultiplier = " + (GetBoolean(root, "useHundredMultiplier") ? "true" : "false") + ", " +
175-
"AllowInvariantIntegerInput = " + (GetBoolean(root, "allowInvariantIntegerInput") ? "true" : "false") + ", " +
176-
"TeenBaseValue = " + (GetOptionalInt64(root, "teenBaseValue")?.ToString(CultureInfo.InvariantCulture) ?? "10") + ", " +
177-
"HundredSuffixValue = " + (GetOptionalInt64(root, "hundredSuffixValue")?.ToString(CultureInfo.InvariantCulture) ?? "100") + ", " +
178-
"UnitTokenMinValue = " + (GetOptionalInt64(root, "unitTokenMinValue")?.ToString(CultureInfo.InvariantCulture) ?? "1") + ", " +
179-
"UnitTokenMaxValue = " + (GetOptionalInt64(root, "unitTokenMaxValue")?.ToString(CultureInfo.InvariantCulture) ?? "9") + ", " +
180-
"HundredSuffixMinValue = " + (GetOptionalInt64(root, "hundredSuffixMinValue")?.ToString(CultureInfo.InvariantCulture) ?? "long.MaxValue") + ", " +
181-
"HundredSuffixMaxValue = " + (GetOptionalInt64(root, "hundredSuffixMaxValue")?.ToString(CultureInfo.InvariantCulture) ?? "long.MinValue") + ", " +
182-
"ScaleThreshold = " + (GetOptionalInt64(root, "scaleThreshold")?.ToString(CultureInfo.InvariantCulture) ?? "1000") +
183-
" }";
155+
static string CreateTokenMapRulesExpression(JsonElement root)
156+
{
157+
var assignments = new[]
158+
{
159+
RuleAssignment("CardinalMap", CreateStringLongFrozenDictionaryExpression(EngineContractUtilities.GetRequiredElement(root, "cardinalMap"))),
160+
RuleAssignment("ExactOrdinalMap", CreateOptionalStringLongFrozenDictionaryExpression(root, "ordinalMap")),
161+
RuleAssignment("OrdinalScaleMap", CreateOptionalStringLongMapOrNull(root, "ordinalScaleMap")),
162+
RuleAssignment("GluedOrdinalScaleSuffixes", CreateOptionalStringLongMapOrNull(root, "gluedOrdinalScaleSuffixes")),
163+
RuleAssignment("CompositeScaleMap", CreateOptionalStringLongMapOrNull(root, "compositeScaleMap")),
164+
RuleAssignment("NormalizationProfile", "TokenMapNormalizationProfile." + ToEnumMemberName(GetRequiredString(root, "normalizationProfile"))),
165+
RuleAssignment("NegativePrefixes", CreateOptionalStringArrayExpression(root, "negativePrefixes")),
166+
RuleAssignment("NegativeSuffixes", CreateOptionalStringArrayExpression(root, "negativeSuffixes")),
167+
RuleAssignment("OrdinalPrefixes", CreateOptionalStringArrayExpression(root, "ordinalPrefixes")),
168+
RuleAssignment("IgnoredTokens", CreateOptionalStringArrayExpression(root, "ignoredTokens")),
169+
RuleAssignment("LeadingTokenPrefixesToTrim", CreateOptionalStringArrayExpression(root, "leadingTokenPrefixesToTrim")),
170+
RuleAssignment("MultiplierTokens", CreateOptionalStringArrayExpression(root, "multiplierTokens")),
171+
RuleAssignment("TokenSuffixesToStrip", CreateOptionalStringArrayExpression(root, "tokenSuffixesToStrip")),
172+
RuleAssignment("OrdinalAbbreviationSuffixes", CreateOptionalStringArrayExpression(root, "ordinalAbbreviationSuffixes")),
173+
RuleAssignment("TeenSuffixTokens", CreateOptionalStringArrayExpression(root, "teenSuffixTokens")),
174+
RuleAssignment("HundredSuffixTokens", CreateOptionalStringArrayExpression(root, "hundredSuffixTokens")),
175+
RuleAssignment("AllowTerminalOrdinalToken", CreateBooleanLiteral(GetBoolean(root, "allowTerminalOrdinalToken"))),
176+
RuleAssignment("UseHundredMultiplier", CreateBooleanLiteral(GetBoolean(root, "useHundredMultiplier"))),
177+
RuleAssignment("AllowInvariantIntegerInput", CreateBooleanLiteral(GetBoolean(root, "allowInvariantIntegerInput"))),
178+
RuleAssignment("TeenBaseValue", CreateOptionalInt64Literal(root, "teenBaseValue", "10")),
179+
RuleAssignment("HundredSuffixValue", CreateOptionalInt64Literal(root, "hundredSuffixValue", "100")),
180+
RuleAssignment("UnitTokenMinValue", CreateOptionalInt64Literal(root, "unitTokenMinValue", "1")),
181+
RuleAssignment("UnitTokenMaxValue", CreateOptionalInt64Literal(root, "unitTokenMaxValue", "9")),
182+
RuleAssignment("HundredSuffixMinValue", CreateOptionalInt64Literal(root, "hundredSuffixMinValue", "long.MaxValue")),
183+
RuleAssignment("HundredSuffixMaxValue", CreateOptionalInt64Literal(root, "hundredSuffixMaxValue", "long.MinValue")),
184+
RuleAssignment("ScaleThreshold", CreateOptionalInt64Literal(root, "scaleThreshold", "1000"))
185+
};
186+
187+
return "new() { " + string.Join(", ", assignments) + " }";
188+
}
189+
190+
static string RuleAssignment(string propertyName, string expression) =>
191+
propertyName + " = " + expression;
192+
193+
static string CreateOptionalStringLongMapOrNull(JsonElement root, string propertyName) =>
194+
root.TryGetProperty(propertyName, out var property)
195+
? CreateStringLongFrozenDictionaryExpression(property)
196+
: "null";
197+
198+
static string CreateBooleanLiteral(bool value) =>
199+
value ? "true" : "false";
200+
201+
static string CreateOptionalInt64Literal(JsonElement root, string propertyName, string defaultExpression) =>
202+
GetOptionalInt64(root, propertyName)?.ToString(CultureInfo.InvariantCulture) ?? defaultExpression;
184203

185204
static string CreateGreedyCompoundOrdinalMapExpression(WordsToNumberProfileDefinition profile)
186205
{

src/Humanizer.SourceGenerators/Generators/TokenMapWordsToNumberInput.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,33 +212,17 @@ public void Emit(SourceProductionContext context)
212212
AppendStringArray(builder, " ", "TeenSuffixTokens", locale.TeenSuffixTokens);
213213
AppendStringArray(builder, " ", "HundredSuffixTokens", locale.HundredSuffixTokens);
214214

215-
if (locale.AllowTerminalOrdinalToken)
216-
{
217-
builder.AppendLine(" AllowTerminalOrdinalToken = true,");
218-
}
219-
220-
if (locale.UseHundredMultiplier)
221-
{
222-
builder.AppendLine(" UseHundredMultiplier = true,");
223-
}
224-
225-
if (locale.AllowInvariantIntegerInput)
226-
{
227-
builder.AppendLine(" AllowInvariantIntegerInput = true,");
228-
}
215+
AppendTrueBooleanValue(builder, " ", "AllowTerminalOrdinalToken", locale.AllowTerminalOrdinalToken);
216+
AppendTrueBooleanValue(builder, " ", "UseHundredMultiplier", locale.UseHundredMultiplier);
217+
AppendTrueBooleanValue(builder, " ", "AllowInvariantIntegerInput", locale.AllowInvariantIntegerInput);
229218

230219
AppendLongValue(builder, " ", "TeenBaseValue", locale.TeenBaseValue, defaultValue: 10);
231220
AppendLongValue(builder, " ", "HundredSuffixValue", locale.HundredSuffixValue, defaultValue: 100);
232221
AppendLongValue(builder, " ", "UnitTokenMinValue", locale.UnitTokenMinValue, defaultValue: 1);
233222
AppendLongValue(builder, " ", "UnitTokenMaxValue", locale.UnitTokenMaxValue, defaultValue: 9);
234223
AppendLongValue(builder, " ", "HundredSuffixMinValue", locale.HundredSuffixMinValue, defaultValue: long.MaxValue);
235224
AppendLongValue(builder, " ", "HundredSuffixMaxValue", locale.HundredSuffixMaxValue, defaultValue: long.MinValue);
236-
if (locale.ScaleThreshold.HasValue)
237-
{
238-
builder.Append(" ScaleThreshold = ");
239-
builder.Append(locale.ScaleThreshold.Value.ToString(CultureInfo.InvariantCulture));
240-
builder.AppendLine(",");
241-
}
225+
AppendLongValue(builder, " ", "ScaleThreshold", locale.ScaleThreshold, defaultValue: 1000);
242226

243227
builder.AppendLine(" });");
244228
builder.AppendLine(" }");
@@ -317,6 +301,18 @@ static void AppendLongValue(StringBuilder builder, string indent, string propert
317301
builder.AppendLine(",");
318302
}
319303

304+
static void AppendTrueBooleanValue(StringBuilder builder, string indent, string propertyName, bool value)
305+
{
306+
if (!value)
307+
{
308+
return;
309+
}
310+
311+
builder.Append(indent);
312+
builder.Append(propertyName);
313+
builder.AppendLine(" = true,");
314+
}
315+
320316
static string? ReadRequiredEnumString(
321317
string localeCode,
322318
JsonElement element,

tests/Humanizer.SourceGenerators.Tests/SourceGenerators/HumanizerSourceGeneratorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Humanizer.SourceGenerators.Tests;
1212

1313
public partial class HumanizerSourceGeneratorTests
1414
{
15-
static readonly Lazy<ImmutableDictionary<string, string>> generatedSources = new(GenerateSources);
15+
static readonly Lazy<ImmutableDictionary<string, string>> GeneratedSources = new(GenerateSources);
1616

1717
[Fact]
1818
public void FormatterRegistryRegistrationsUseGeneratedProfilesForSharedFormatters()
@@ -251,7 +251,7 @@ public void WordsToNumberRegistrationsUseLexiconConvertersForKurdishAndVietnames
251251
Assert.DoesNotContain("case \"en\":", profileCatalogSource);
252252
Assert.DoesNotContain("case \"kurdish\":", profileCatalogSource);
253253
Assert.DoesNotContain("case \"vietnamese\":", profileCatalogSource);
254-
Assert.DoesNotContain(generatedSources.Value.Keys, static hintName => hintName == "TokenMapWordsToNumberConverters.Index.g.cs");
254+
Assert.DoesNotContain(GeneratedSources.Value.Keys, static hintName => hintName == "TokenMapWordsToNumberConverters.Index.g.cs");
255255
}
256256

257257
[Fact]
@@ -1361,7 +1361,7 @@ public void WordsToNumberProfilesAcceptOmittedEmptyIgnoredToken()
13611361
}
13621362

13631363
static string GetGeneratedSource(string hintName) =>
1364-
generatedSources.Value.TryGetValue(hintName, out var source)
1364+
GeneratedSources.Value.TryGetValue(hintName, out var source)
13651365
? source
13661366
: throw new Xunit.Sdk.XunitException($"Generated source '{hintName}' was not found.");
13671367

tests/Humanizer.Tests/CollectionHumanizeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ public void HumanizeTrimsItemsByDefault() =>
131131
/// Use the dummy formatter to ensure tests are testing formatter output rather than input
132132
/// </summary>
133133
static readonly Func<string, string> DummyFormatter = input => input;
134-
}
134+
}

0 commit comments

Comments
 (0)