156 lines
7.0 KiB
C#

using Adaptation.Shared;
using Adaptation.Shared.Methods;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
namespace _Tests.Helpers;
public class Metrology
{
internal static Tuple<string, string[], string[]> GetLogisticsColumnsAndBody(string fileFullName)
{
Tuple<string, string[], string[]> results;
results = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(fileFullName);
Assert.IsFalse(string.IsNullOrEmpty(results.Item1));
Assert.IsTrue(results.Item2.Length > 0, "Column check");
Assert.IsTrue(results.Item3.Length > 0, "Body check");
return results;
}
internal static Tuple<string, string[], string[]> GetLogisticsColumnsAndBody(string searchDirectory, string searchPattern)
{
Tuple<string, string[], string[]> results;
if (searchPattern.Length > 3 && !searchPattern.Contains('*') && File.Exists(searchPattern))
results = GetLogisticsColumnsAndBody(searchPattern);
else
{
string[] pdsfFiles;
pdsfFiles = Directory.GetFiles(searchDirectory, searchPattern, SearchOption.TopDirectoryOnly);
if (!pdsfFiles.Any())
_ = Process.Start("explorer.exe", searchDirectory);
Assert.IsTrue(pdsfFiles.Any(), "GetFiles check");
results = GetLogisticsColumnsAndBody(pdsfFiles[0]);
}
Assert.IsFalse(string.IsNullOrEmpty(results.Item1));
Assert.IsTrue(results.Item2.Length > 0, "Column check");
Assert.IsTrue(results.Item3.Length > 0, "Body check");
return results;
}
internal static Tuple<string, string[], string[]> GetLogisticsColumnsAndBody(IFileRead fileRead, Logistics logistics, Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResult, Tuple<string, string[], string[]> pdsf)
{
Tuple<string, string[], string[]> results;
string text = ProcessDataStandardFormat.GetPDSFText(fileRead, logistics, extractResult.Item3, logisticsText: pdsf.Item1);
string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
results = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(logistics.ReportFullPath, lines);
Assert.IsFalse(string.IsNullOrEmpty(results.Item1));
Assert.IsTrue(results.Item2.Length > 0, "Column check");
Assert.IsTrue(results.Item3.Length > 0, "Body check");
return results;
}
internal static string[] GetItem2(Tuple<string, string[], string[]> pdsf, Tuple<string, string[], string[]> pdsfNew)
{
JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true };
string jsonOld = JsonSerializer.Serialize(pdsf.Item2, pdsf.Item2.GetType(), jsonSerializerOptions);
string jsonNew = JsonSerializer.Serialize(pdsfNew.Item2, pdsfNew.Item2.GetType(), jsonSerializerOptions);
return new string[] { jsonOld, jsonNew };
}
internal static string[] GetItem3(Tuple<string, string[], string[]> pdsf, Tuple<string, string[], string[]> pdsfNew)
{
string joinOld = string.Join(Environment.NewLine, from l in pdsf.Item3 select string.Join('\t', from t in l.Split('\t') where !t.Contains(@"\\") select t));
string joinNew = string.Join(Environment.NewLine, from l in pdsfNew.Item3 select string.Join('\t', from t in l.Split('\t') where !t.Contains(@"\\") select t));
return new string[] { joinOld, joinNew };
}
internal static void UpdatePassDirectory(string searchDirectory)
{
DateTime dateTime = DateTime.Now;
try
{ Directory.SetLastWriteTime(searchDirectory, dateTime); }
catch (System.Exception) { }
string ticksDirectory = Path.GetDirectoryName(searchDirectory);
try
{ Directory.SetLastWriteTime(ticksDirectory, dateTime); }
catch (System.Exception) { }
string[] directories = Directory.GetDirectories(searchDirectory, "*", SearchOption.TopDirectoryOnly);
foreach (string directory in directories)
{
try
{ Directory.SetLastWriteTime(directory, dateTime); }
catch (System.Exception) { }
}
}
internal static string GetFileName(MethodBase methodBase)
{
string result;
string connectionName;
string seperator = "__";
string connectionNameAndTicks;
string[] segments = methodBase.Name.Split(new string[] { seperator }, StringSplitOptions.None);
string environment = segments[0];
string rawVersionName = segments[1];
string equipmentTypeDirectory = segments[2];
string ticks = DateTime.Now.Ticks.ToString();
string comment = segments[segments.Length - 1];
string versionName = segments[1].Replace('_', '.');
string before = string.Concat(environment, seperator, rawVersionName, seperator, equipmentTypeDirectory, seperator);
string after = methodBase.Name.Substring(before.Length);
if (after.Length < ticks.Length)
{
connectionName = after;
}
else
{
connectionNameAndTicks = after.Substring(0, after.Length - 2 - comment.Length);
connectionName = connectionNameAndTicks.Substring(0, connectionNameAndTicks.Length - ticks.Length);
ticks = connectionNameAndTicks.Substring(connectionName.Length);
}
result = Path.Combine(environment, equipmentTypeDirectory, versionName, $"{environment}__{rawVersionName}__{equipmentTypeDirectory}__{connectionName}", ticks, $"{connectionName.Replace('_', '-')}.json");
if (result.Contains('/'))
result = string.Concat('/', result);
else
result = string.Concat('\\', result);
return result;
}
internal static void CompareSaveTSV(string textFileDirectory, string[] join)
{
if (join[0] != join[1])
{
_ = Process.Start("explorer.exe", textFileDirectory);
File.WriteAllText(Path.Combine(textFileDirectory, "0.tsv"), join[0]);
File.WriteAllText(Path.Combine(textFileDirectory, "1.tsv"), join[1]);
}
}
internal static void CompareSaveJSON(string textFileDirectory, string[] json)
{
if (json[0] != json[1])
{
_ = Process.Start("explorer.exe", textFileDirectory);
File.WriteAllText(Path.Combine(textFileDirectory, "0.json"), json[0]);
File.WriteAllText(Path.Combine(textFileDirectory, "1.json"), json[1]);
}
}
internal static void CompareSave(string textFileDirectory, Tuple<string, string[], string[]> pdsf, Tuple<string, string[], string[]> pdsfNew)
{
if (pdsf.Item1 != pdsfNew.Item1)
{
_ = Process.Start("explorer.exe", textFileDirectory);
File.WriteAllText(Path.Combine(textFileDirectory, "0.dat"), pdsf.Item1);
File.WriteAllText(Path.Combine(textFileDirectory, "1.dat"), pdsfNew.Item1);
}
}
}