Humanizr/Humanizer · error · InvalidOperationException

Test input must define durationCases.

Error message

Test input must define durationCases.

What it means

Thrown by ParseForTests when the provided test YAML text, after being wrapped and parsed, does not contain a 'durationCases' key at the root. ParseForTests prepends 'durationCases:\n' to the input, so this fires only if the SimpleYamlParser fails to find the key after wrapping — indicating the input text was malformed or empty.

Source

Thrown at src/Humanizer.SourceGenerators/Common/DurationCaseModels.cs:403

                localeCode,
                classification,
                "nominative",
                ["nominative", .. cases.Keys.OrderBy(static name => name, StringComparer.Ordinal)],
                EmptyRealizations(),
                EmptySources(),
                cases.ToImmutable().Add("nominative", CreateBaseDurationOverlay()));
        }

        internal static DurationCaseCatalog ParseForTests(
            string localeCode,
            string text,
            string? casePluralRule = null,
            string? phrasesText = null)
        {
            var root = SimpleYamlParser.Parse($"durationCases:\n{text}");
            if (!root.TryGetValue("durationCases", out var value))
            {
                throw new InvalidOperationException("Test input must define durationCases.");
            }

            SimpleYamlValue? phrases = null;
            if (phrasesText is not null)
            {
                var phrasesRoot = SimpleYamlParser.Parse($"phrases:\n{phrasesText}");
                _ = phrasesRoot.TryGetValue("phrases", out phrases);
            }

            var path = $"{localeCode}.durationCases";
            var mapping = ExpectMapping(value, path);
            return mapping.TryGetValue("inventory", out _) ||
                   mapping.TryGetValue("sources", out _) ||
                   mapping.TryGetValue("provenance", out _) ||
                   mapping.TryGetValue("reason", out _)
                ? Parse(localeCode, value, casePluralRule, phrases)
                : ParseLegacyForTests(localeCode, mapping, path);
        }

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Pass only the indented block content (what goes under durationCases:), not the full document. E.g. pass ' classification: distinct\n cases:\n ...' not 'durationCases:\n classification: distinct'.
  2. Ensure the text is non-empty and contains valid YAML block syntax.
  3. If testing with no duration data is intentional, use a different test helper or skip the ParseForTests call.

Example fix

// before (passing full document)
var catalog = DurationCaseNormalization.ParseForTests("xx",
    "durationCases:\n  classification: distinct");
// after (passing block content only)
var catalog = DurationCaseNormalization.ParseForTests("xx",
    "classification: distinct\n  cases:\n    genitive: { ... }");
Defensive patterns

Strategy: validation

Validate before calling

// Verify test input will produce a durationCases root after wrapping
static bool TestInputIsValid(string text)
{
    if (string.IsNullOrWhiteSpace(text)) return false;
    // Ensure the text doesn't itself declare durationCases at column 0
    return !text.Split('\n', StringSplitOptions.RemoveEmptyEntries)
        .Any(line => line.TrimStart().StartsWith("durationCases:", StringComparison.Ordinal));
}

Prevention

When it happens

Trigger: ParseForTests does SimpleYamlParser.Parse('durationCases:\n' + text) and then TryGetValue('durationCases'). If the parser produces a root without the key (e.g. the input text overrode it or was empty whitespace), the throw fires. For example, passing an empty string or a string that starts with a top-level key.

Common situations: A test passes string.Empty or whitespace as the durationCases text. Or the test text starts with its own 'durationCases:' key, causing a duplicate that the parser collapses unexpectedly. The helper expects raw block content (indented YAML), not a full document.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/0344823f656a2ad2. Report an issue: GitHub.