Skip to content

Commit

Permalink
Merge pull request #83 from testit-tms/hotfix/TMS-28931_and_TMS-28908
Browse files Browse the repository at this point in the history
TMS-28931 and TMS-28908: Fixed the parsing of format characters for Q…
  • Loading branch information
PavelButuzov authored Oct 28, 2024
2 parents 97426c1 + 7438e58 commit beb3e8a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 53 deletions.
5 changes: 4 additions & 1 deletion Migrators/QaseExporter/Services/StepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ private static string ConvertingStepDescription(string description)
{
return
Utils.RemoveBackslashCharacters(
Utils.ConvertingAngleBracketsStr(
Utils.ConvertingToggleStrikethroughStr(
Utils.ConvertingCodeStr(
Utils.ConvertingBlockCodeStr(
Utils.ConvertingToggleStrongStr(
Utils.ConvertingHyperlinks(
Utils.ConvertingFormatCharactersWithBlockCodeStr(description))))));
Utils.ConvertingFormatCharacters(
Utils.ConvertingFormatCharactersWithBlockCodeStr(description)))))))));
}
}
10 changes: 9 additions & 1 deletion Migrators/QaseExporter/Services/TestCaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private async Task<List<TestCase>> ConvertTestCases(List<QaseTestCase> qaseTestC
new TestCase
{
Id = testCaseId,
Description = qaseTestCase.Description,
Description = ConvertingDescription(qaseTestCase.Description),
State = ConvertStatus(qaseTestCase.Status),
Priority = ConvertPriority(qaseTestCase.Priority),
Steps = steps,
Expand Down Expand Up @@ -329,4 +329,12 @@ private static StateType ConvertStatus(int status)
_ => StateType.NotReady
};
}

private static string ConvertingDescription(string description)
{
return
Utils.RemoveBackslashCharacters(
Utils.RemoveToggleStrongCharacters(
Utils.RemoveToggleStrikethroughCharacters(description)));
}
}
134 changes: 83 additions & 51 deletions Migrators/QaseExporter/Services/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,26 @@ public static class Utils
private const string HyperlinkPattern = @"\[[^\[\]]*\]\([^()\s]*\)";
private const string titlePattern = @"\[([^\[\]]+)\]";
private const string ToggleStrongStrPattern = @"\*\*(.*?)\*\*"; // Match: "**{anything}**"
private const string ToggleStrikethroughStrPattern = @"\~\~(.*?)\~\~"; // Match: "~~{anything}~~"
private const string BlockCodeStrPattern = @"\`{3}([\s\S]*?)\`{3}"; // Match: "```{anything}```"
private const string CodeStrPattern = @"(?<!`)\`(.*?)\`(?!`)"; // Match: "`{anything}`", No match: "```{anything}```"
private const string BackslashCharacterPattern = @"(?<!\\)\\(?!\\)"; // Match: "\\", No match: "\\\\"
private const string ToggleStrongCharacterPattern = @"\*\*"; // Match: "**"
private const string ToggleStrikethroughCharacterPattern = @"\~\~"; // Match: "~~"
private const string CodeCharacterPattern = @"(?<!\\)\`"; // Match: "`", No match: "\\`"
private const string AngleBracketCharacterPattern = @"\\<"; // Match: "\<", No match: "<"
private const string FormatTabCharacter = "\t";
private const string FormatNewLineCharacter = "\n";

public static QaseDescriptionData ExtractAttachments(string? description)
{
if (string.IsNullOrEmpty(description))
{
return new QaseDescriptionData
{
Description = string.Empty,
Attachments = new List<QaseAttachment>()
};
}

var data = new QaseDescriptionData
{
Description = description,
Attachments = new List<QaseAttachment>()
};

var imgRegex = new Regex(ImgPattern);

var matches = imgRegex.Matches(description);
var matches = GetAllMatchesByPattern(description, ImgPattern);

if (matches.Count == 0)
{
Expand Down Expand Up @@ -65,14 +59,7 @@ public static QaseDescriptionData ExtractAttachments(string? description)

public static string ConvertingHyperlinks(string? description)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var hyperlinkRegex = new Regex(HyperlinkPattern);

var matches = hyperlinkRegex.Matches(description);
var matches = GetAllMatchesByPattern(description, HyperlinkPattern);

if (matches.Count == 0)
{
Expand Down Expand Up @@ -100,14 +87,7 @@ public static string ConvertingHyperlinks(string? description)

public static string ConvertingToggleStrongStr(string? description)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var toggleStrongStrRegex = new Regex(ToggleStrongStrPattern);

var matches = toggleStrongStrRegex.Matches(description);
var matches = GetAllMatchesByPattern(description, ToggleStrongStrPattern);

if (matches.Count == 0)
{
Expand All @@ -116,23 +96,34 @@ public static string ConvertingToggleStrongStr(string? description)

foreach (Match match in matches)
{
var matchWithoutToggleStrongFormat = match.Value.Replace("**", "");
var matchWithoutToggleStrongFormat = RemoveToggleStrongCharacters(match.Value);
description = description.Replace(match.Value, $"<strong>{matchWithoutToggleStrongFormat}</strong>");
}

return description;
}

public static string ConvertingBlockCodeStr(string? description)
public static string ConvertingToggleStrikethroughStr(string? description)
{
if (string.IsNullOrEmpty(description))
var matches = GetAllMatchesByPattern(description, ToggleStrikethroughStrPattern);

if (matches.Count == 0)
{
return string.Empty;
return description;
}

var blockCodeStrRegex = new Regex(BlockCodeStrPattern);
foreach (Match match in matches)
{
var matchWithoutToggleStrikethroughFormat = RemoveToggleStrikethroughCharacters(match.Value);
description = description.Replace(match.Value, $"<s>{matchWithoutToggleStrikethroughFormat}</s>");
}

var matches = blockCodeStrRegex.Matches(description);
return description;
}

public static string ConvertingBlockCodeStr(string? description)
{
var matches = GetAllMatchesByPattern(description, BlockCodeStrPattern);

if (matches.Count == 0)
{
Expand All @@ -141,7 +132,7 @@ public static string ConvertingBlockCodeStr(string? description)

foreach (Match match in matches)
{
var matchWithoutBlockCodeFormat = match.Value.Replace("```", "");
var matchWithoutBlockCodeFormat = RemoveCodeCharacters(match.Value);
description = description.Replace(match.Value, $"<pre class=\"tiptap-code-block\"><code>{matchWithoutBlockCodeFormat}</code></pre>");
}

Expand All @@ -150,14 +141,7 @@ public static string ConvertingBlockCodeStr(string? description)

public static string ConvertingCodeStr(string? description)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var codeStrRegex = new Regex(CodeStrPattern);

var matches = codeStrRegex.Matches(description);
var matches = GetAllMatchesByPattern(description, CodeStrPattern);

if (matches.Count == 0)
{
Expand All @@ -166,7 +150,7 @@ public static string ConvertingCodeStr(string? description)

foreach (Match match in matches)
{
var matchWithoutCodeFormat = match.Value[1..^1];
var matchWithoutCodeFormat = RemoveCodeCharacters(match.Value);
description = description.Replace(match.Value, $"<code class=\"tiptap-inline-code\">{matchWithoutCodeFormat}</code>");
}

Expand All @@ -175,14 +159,13 @@ public static string ConvertingCodeStr(string? description)

public static string ConvertingFormatCharactersWithBlockCodeStr(string? description)
{
if (string.IsNullOrEmpty(description))
var matches = GetAllMatchesByPattern(description, BlockCodeStrPattern);

if (matches.Count == 0)
{
return string.Empty;
return description;
}

var blockCodeStrRegex = new Regex(BlockCodeStrPattern);

var matches = blockCodeStrRegex.Matches(description);
var remainingDescription = description;
var convertedDescription = string.Empty;

Expand Down Expand Up @@ -215,15 +198,64 @@ public static string ConvertingFormatCharacters(string? description)
return description;
}

public static string ConvertingAngleBracketsStr(string? description)
{
var matches = GetAllMatchesByPattern(description, AngleBracketCharacterPattern);

if (matches.Count == 0)
{
return description;
}

foreach (Match match in matches)
{
description = description.Replace(match.Value, "&lt;");
}

return description;
}

private static List<Match> GetAllMatchesByPattern(string? description, string pattern)
{
if (string.IsNullOrEmpty(description))
{
return new List<Match>();
}

var regex = new Regex(pattern);

return regex.Matches(description).ToList();
}

public static string RemoveBackslashCharacters(string? description)
{
return RemoveCharactersFromDescriptionByPattern(description, BackslashCharacterPattern);
}

public static string RemoveToggleStrongCharacters(string? description)
{
return RemoveCharactersFromDescriptionByPattern(description, ToggleStrongCharacterPattern);
}

public static string RemoveToggleStrikethroughCharacters(string? description)
{
return RemoveCharactersFromDescriptionByPattern(description, ToggleStrikethroughCharacterPattern);
}

public static string RemoveCodeCharacters(string? description)
{
return RemoveCharactersFromDescriptionByPattern(description, CodeCharacterPattern);
}

private static string RemoveCharactersFromDescriptionByPattern(string? description, string pattern)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var backslashCharacterRegex = new Regex(BackslashCharacterPattern);
var regex = new Regex(pattern);

return backslashCharacterRegex.Replace(description, "");
return regex.Replace(description, "");
}
}

0 comments on commit beb3e8a

Please sign in to comment.