From c4857541ed0ee2055e582fc2afa18a7f5ddd8b4e Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 8 Apr 2021 19:22:23 +0200 Subject: [PATCH] Make TestSqlLoggerFactory recognize quoting/curlies Follow-up to #24595 --- .../TestUtilities/TestSqlLoggerFactory.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test/EFCore.Relational.Specification.Tests/TestUtilities/TestSqlLoggerFactory.cs b/test/EFCore.Relational.Specification.Tests/TestUtilities/TestSqlLoggerFactory.cs index 83097dabea1..04780870805 100644 --- a/test/EFCore.Relational.Specification.Tests/TestUtilities/TestSqlLoggerFactory.cs +++ b/test/EFCore.Relational.Specification.Tests/TestUtilities/TestSqlLoggerFactory.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Logging; using Xunit; @@ -121,6 +122,8 @@ public TestSqlLogger(bool shouldLogCommands) public List SqlStatements { get; } = new(); public List Parameters { get; } = new(); + private StringBuilder _stringBuilder = new(); + protected override void UnsafeClear() { base.UnsafeClear(); @@ -157,7 +160,37 @@ protected override void UnsafeLog( if (!string.IsNullOrWhiteSpace(parameters)) { Parameters.Add(parameters); - parameters = parameters.Replace(", ", _eol) + _eol + _eol; + + _stringBuilder.Clear(); + + var inQuotes = false; + var inCurlies = false; + for (var i = 0; i < parameters.Length; i++) + { + var c = parameters[i]; + switch (c) + { + case '\'': + inQuotes = !inQuotes; + goto default; + case '{': + inCurlies = true; + goto default; + case '}': + inCurlies = false; + goto default; + case ',' when parameters[i + 1] == ' ' && !inQuotes && !inCurlies: + _stringBuilder.Append(_eol); + i++; + continue; + default: + _stringBuilder.Append(c); + continue; + } + } + + _stringBuilder.Append(_eol).Append(_eol); + parameters = _stringBuilder.ToString(); } SqlStatements.Add(parameters + commandText);