Skip to content

Commit

Permalink
Add Tests for RetrieveFormatVersionFromInputFileName (#116)
Browse files Browse the repository at this point in the history
* Add Tests for `RetrieveFormatVersionFromInputFileName`

* remve BOM
  • Loading branch information
hf-kklein authored Jul 31, 2023
1 parent f377a03 commit 5f5a6c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
23 changes: 15 additions & 8 deletions EDILibrary/TemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace EDILibrary
public class TemplateHelper
{

private const string FormatVersionPattern = "^(?<format>[A-Z]{6,7})(?<version>[A-Z]?\\d+\\.\\d+[a-z]?)$";
private static readonly Regex FormatVersionRegex = new(@"^(?<format>[A-Z]{6,7})(?<version>[A-Z]?\d+\.\d+[a-z]?)$");

private static void ParseAPERAKString(string aperak, out string dataType, out int length,
out List<string> list)
Expand Down Expand Up @@ -138,7 +138,7 @@ private static void Recurse(XElement cur, JArray refObj, TreeElement tree)
{
meta.Add("sg", elem.Attribute("meta.sg").Value);
}
//check for parent sg
//check for parent sg
else if (cur.Attribute("ref") != null)
{
meta.Add("sg", cur.Attribute("ref").Value);
Expand Down Expand Up @@ -453,19 +453,26 @@ public string ConvertFilesToJSON(string inputFileName, string outputFileName, st

}

private static string RetrieveFormatVersionFromInputFileName(string inputFileName)
/// <summary>
/// Extract the Format version (e.g. "1.0a") from a given template file name (e.g. "COMDIS1.0a.template")
/// </summary>
/// <param name="inputFileName">path of name of the template file</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static string RetrieveFormatVersionFromInputFileName(string inputFileName)
{
var formatVersionRegex = new Regex(FormatVersionPattern);
var part = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(System.IO.Path.DirectorySeparatorChar).Last();
var match = formatVersionRegex.Match(part);
var actualFileName = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(Path.DirectorySeparatorChar).Last();
var match = FormatVersionRegex.Match(actualFileName);
if (match.Success)
{
return match.Groups["version"].Value;
}
else

if (inputFileName.Contains(".create.template"))
{
throw new ArgumentException($"Format version could not be determined: {part}", nameof(inputFileName));
throw new ArgumentException("Pass the name of the \".template\" file, not \".create.template\"", nameof(inputFileName));
}
throw new ArgumentException($"Format version could not be determined: {actualFileName}", nameof(inputFileName));
}

// the format version will be written to the json template file, thus needs to be passed
Expand Down
27 changes: 27 additions & 0 deletions EDILibraryTests/TemplateHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using EDILibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EDILibraryTests;

[TestClass]
public class TemplateHelperTests
{
[TestMethod]
[DataRow("COMDIS1.0c.template", "1.0c")]
[DataRow("UTILMDGG1.0a.template", "G1.0a")]
public void Test_RetrieveFormatVersionFromInputFileName(string filename, string expectedFormatVersion)
{
var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filename);
Assert.AreEqual(expectedFormatVersion, actualFormatVersion);
}

[TestMethod]
public void Test_RetrieveFormatVersionFromInputFileName_ForPaths()
{
var filepathAsString = $"Path{Path.DirectorySeparatorChar}To{Path.DirectorySeparatorChar}My{Path.DirectorySeparatorChar}Favourite{Path.DirectorySeparatorChar}Templates{Path.DirectorySeparatorChar}folder{Path.DirectorySeparatorChar}COMDIS1.0c.template";
var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filepathAsString);
Assert.AreEqual("1.0c", actualFormatVersion);
}

}

0 comments on commit 5f5a6c6

Please sign in to comment.