128 lines
4.6 KiB
C#
128 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using JetBrains.Annotations;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using Moq;
|
|
using Results;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Rig.Output.FileWriters.Enhanced;
|
|
using TBF.Rig.Sequences;
|
|
|
|
namespace TBFTests.Rig.Output.FileWriters.Enhanced
|
|
{
|
|
[TestClass]
|
|
[TestSubject(typeof(Writer))]
|
|
public class WriterTest
|
|
{
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
// Mock IBenchInfo and assign it to static field
|
|
var mockBenchInfo = new Mock<IBenchInfo>();
|
|
// Optional: Setup mock behavior if GetName or similar is used
|
|
mockBenchInfo.Setup(b => b.TestBenchName).Returns("TestBench");
|
|
mockBenchInfo.Setup(b => b.TestBenchId).Returns(1);
|
|
|
|
|
|
ProcessData.BenchInfo = mockBenchInfo.Object;
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
// Clean up the static reference
|
|
ProcessData.BenchInfo = null;
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetFilenameTest()
|
|
{
|
|
FactorySingle factory = new FactorySingle();
|
|
WriterCfg defaultConfig = factory.DefaultConfig() as WriterCfg;
|
|
string dateFormat = "yyMMdd-HHmm";
|
|
defaultConfig.FileNameFormat = $"{{5:{dateFormat}}}.txt";
|
|
Writer writer = new Writer( defaultConfig);
|
|
|
|
|
|
|
|
Results.Entities.Batch batch = new Results.Entities.Batch();
|
|
DateTime time = DateTime.Now;
|
|
batch.EndTime = time;
|
|
string filename = writer.GetFilename("C:\\TBF\\Results\\", batch, time);
|
|
|
|
|
|
// Assert
|
|
Assert.IsNotNull(filename, "Filename should not be null.");
|
|
Assert.IsTrue(filename.StartsWith("C:\\TBF\\Results\\"), "Filename should start with base path.");
|
|
|
|
//test extension
|
|
Assert.IsTrue(filename.EndsWith(".txt") , "Filename should have correct extension.");
|
|
|
|
//test path - Format path like \2025\7\28\
|
|
string path = $@"\{time.Year}\{time.Month}\{time.Day}\";
|
|
Assert.IsTrue( filename.Contains(path));
|
|
|
|
//test file name
|
|
string expected = string.Format($"{{0:{dateFormat}}}.txt", time);
|
|
Assert.IsTrue( filename.Contains(expected));
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
public void WriteRsltsExTest()
|
|
{
|
|
FactorySingle factory = new FactorySingle();
|
|
WriterCfg defaultConfig = factory.DefaultConfig() as WriterCfg;
|
|
string dateFormat = "yyMMdd-HHmm";
|
|
defaultConfig.FileNameFormat = $"{{5:{dateFormat}}}.txt";
|
|
defaultConfig.Header = "Header :)";
|
|
|
|
|
|
Writer writer = new Writer( defaultConfig);
|
|
var field = typeof(Writer).GetField("commonItems", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
var fieldHeader = typeof(Writer).GetField("header", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
fieldHeader?.SetValue(writer, defaultConfig.Header);
|
|
|
|
var mockList = new List<Results.WMeterRsltItemSpec>
|
|
{
|
|
new WMeterRsltItemSpec (ItemID.Batch_end_time, "End", null,Common.Quantity.DateTime, Common.ItemCategory.Other, null)
|
|
};
|
|
mockList[0].Caption = "Date and time";
|
|
string formatCommonTime = "yyyy.MM.dd HH:mm";
|
|
mockList[0].Format = $"{{0:{formatCommonTime}}}";
|
|
field?.SetValue(writer, mockList);
|
|
|
|
|
|
Results.Entities.Batch batch = new Results.Entities.Batch();
|
|
DateTime time = DateTime.Now;
|
|
batch.EndTime = time;
|
|
|
|
|
|
using (var memStream = new MemoryStream())
|
|
using (var writerStream = new StreamWriter(memStream))
|
|
{
|
|
writer.WriteRsltsEx(batch, writerStream, time);
|
|
writerStream.Flush();
|
|
|
|
// Get the actual content
|
|
memStream.Position = 0;
|
|
using (var reader = new StreamReader(memStream))
|
|
{
|
|
string content = reader.ReadToEnd();
|
|
// Assert on the actual content
|
|
Assert.IsNotNull(content);
|
|
|
|
Assert.IsTrue(content.Contains(defaultConfig.Header));
|
|
Assert.IsTrue(content.Contains("Date and time"));
|
|
string expectedCommonTime = string.Format($"{{0:{formatCommonTime}}}", time);
|
|
Assert.IsTrue(content.Contains(expectedCommonTime));
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
} |