Skip to content

[duplicate-code] 🔍 Duplicate Code Detected: C#-style String/Char Escaping Logic Duplicated Across TestFramework Assertions #11437

Closed

Description

@github-actions

Analysis of commit 916b4a4 (and the codebase state as of 0037ec5)

Assignee: @copilot

Summary

Commit 916b4a4 ("Escape control characters in data row display names") introduces a third, independent implementation of C#-style string/char escaping (quotes, backslash, standard escape sequences, control-character \uXXXX escaping, and unpaired-surrogate detection) inside TestDataSourceUtilities.cs. Two other near-identical implementations of the same algorithm already exist in Assert.StringDifference.Formatting.cs and AssertionValueRenderer.cs, all within the same TestFramework assembly.

Duplication Details

Pattern: C#-literal-style string/char escaping with unpaired-surrogate detection

  • Severity: Medium

  • Occurrences: 3

  • Locations:

    • src/TestFramework/TestFramework/Internal/TestDataSourceUtilities.cs (lines 162-253: AppendEscapedString, AppendEscapedChar, AppendEscapedCharacter, IsUnpairedSurrogate at lines 247-253)
    • src/TestFramework/TestFramework/Assertions/Assert.StringDifference.Formatting.cs (lines 8-70: GetEscapedCharacterLength, AppendEscapedToken, IsUnpairedSurrogate at lines 64-70)
    • src/TestFramework/TestFramework/Assertions/AssertionValueRenderer.cs (lines 193-253: RenderString, RenderChar, plus a third escaping switch inside IsBuiltInStringRendering at lines 40-99)
  • Code Sample (near-identical IsUnpairedSurrogate, byte-for-byte the same logic, duplicated verbatim):

    src/TestFramework/TestFramework/Internal/TestDataSourceUtilities.cs (lines 247-253):

    private static bool IsUnpairedSurrogate(string value, int index)
    {
        char character = value[index];
        return char.IsHighSurrogate(character)
            ? index + 1 >= value.Length || !char.IsLowSurrogate(value[index + 1])
            : char.IsLowSurrogate(character)
                && (index == 0 || !char.IsHighSurrogate(value[index - 1]));
    }

    src/TestFramework/TestFramework/Assertions/Assert.StringDifference.Formatting.cs (lines 64-70):

    private static bool IsUnpairedSurrogate(string value, int index)
    {
        char c = value[index];
        return char.IsHighSurrogate(c)
            ? index + 1 >= value.Length || !char.IsLowSurrogate(value[index + 1])
            : char.IsLowSurrogate(c)
                && (index == 0 || !char.IsHighSurrogate(value[index - 1]));
    }

    And the escaping switch itself is repeated with the same cases (", \\, \n, \r, \t, \0, then a char.IsControl fallback appending \uXXXX via ((int)c).ToString("X4", CultureInfo.InvariantCulture)) in AppendEscapedString/AppendEscapedCharacter (TestDataSourceUtilities.cs), AppendEscapedToken (Assert.StringDifference.Formatting.cs), and RenderString/RenderChar (AssertionValueRenderer.cs).

Impact Analysis

  • Maintainability: Any future fix to the escaping rules (e.g. supporting an additional escape sequence, changing the surrogate-pair handling, or adjusting the Unicode escape format) must now be applied in three separate places. It already happened once: AssertionValueRenderer.RenderString/RenderChar do not perform unpaired-surrogate detection, unlike the other two copies, showing the duplication has already caused behavioral drift.
  • Bug Risk: High — inconsistent fixes are likely since a developer touching one copy may not know the other two exist.
  • Code Bloat: ~100+ lines of near-duplicate logic across the assembly for what is conceptually a single utility function.

Refactoring Recommendations

  1. Extract a shared internal escaping utility

    • Extract common functionality to: src/TestFramework/TestFramework/Internal/StringEscaping.cs (or similar shared internal static class) exposing something like AppendEscapedChar(StringBuilder, char, ...), AppendEscapedString(StringBuilder, string), and IsUnpairedSurrogate(string, int).
    • Estimated effort: Small (2-4 hours), mostly mechanical replacement plus verifying unit tests for DataRowAttributeTests, AssertTests.AreEqualTests, and the AssertionValueRenderer/Assert.StringDifference test suites still pass.
    • Benefits: Single source of truth for escaping rules, consistent unpaired-surrogate handling across all three call sites (fixing the current drift in AssertionValueRenderer), smaller codebase, easier to extend.
  2. Align behavior before/while consolidating

    • Decide whether AssertionValueRenderer.RenderString/RenderChar should also perform unpaired-surrogate detection (for consistency with the other two copies) as part of the consolidation, since this is a behavioral gap uncovered by the duplication.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 3 (TestDataSourceUtilities.cs, Assert.StringDifference.Formatting.cs, AssertionValueRenderer.cs)
  • Detection Method: Semantic code analysis (grep + manual comparison of recent commit 916b4a4 against existing codebase)
  • Commit: 916b4a4
  • Analysis Date: 2026-09-22

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · copilot · auto · 51.3 AIC · ⌖ 6.74 AIC · ⊞ 11.7K · [◷]( · ◷)

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on Sep 24, 2026, 4:29 AM UTC

Metadata

Metadata

Labels

type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions