0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 11:51:17 +03:00
ParkanPlayground/X86DisassemblerTests/InstructionTests/InstructionHandlerFactoryTests.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2025-04-13 01:30:58 +03:00
using System.Reflection;
using X86Disassembler.X86;
using X86Disassembler.X86.Handlers;
namespace X86DisassemblerTests.InstructionTests;
public class InstructionHandlerFactoryTests
{
2025-04-13 01:30:58 +03:00
[Fact]
public void Factory_ShouldNotContainDuplicates()
{
byte[] code = new byte[] {0xCC, 0xCC, 0xCC};
2025-04-17 20:47:51 +03:00
var sut = new InstructionHandlerFactory(new InstructionDecoder(code, code.Length));
2025-04-13 01:30:58 +03:00
var handlers = (List<IInstructionHandler>) sut.GetType()
.GetField("_handlers", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(sut)!;
var distinctHandlersCount = handlers.Distinct()
.Count();
Assert.Equal(distinctHandlersCount, handlers.Count);
}
[Fact]
public void Factory_ShouldContainAllKnownHandlers()
{
byte[] code = new byte[] {0xCC, 0xCC, 0xCC};
2025-04-17 20:47:51 +03:00
var sut = new InstructionHandlerFactory(new InstructionDecoder(code, code.Length));
2025-04-13 01:30:58 +03:00
var handlers = (List<IInstructionHandler>) sut.GetType()
.GetField("_handlers", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(sut)!;
var handlerTypes = typeof(InstructionHandler).Assembly.GetExportedTypes()
.Where(x => x.IsAssignableTo(typeof(InstructionHandler)) && x is {IsAbstract: false, IsInterface: false})
.ToList();
foreach (var handlerType in handlerTypes)
{
2025-04-18 02:37:19 +03:00
if (handlers.All(x => x.GetType() != handlerType))
{
Assert.Fail($"{handlerType.Name} not found");
}
2025-04-13 01:30:58 +03:00
}
2025-04-18 01:02:14 +03:00
var uniqueRegisteredHandlers = new HashSet<string>();
var duplicates = new List<string>();
foreach (var handler in handlers)
{
if (!uniqueRegisteredHandlers.Add(handler.GetType().Name))
{
duplicates.Add(handler.GetType().Name);
}
}
if (duplicates.Count != 0)
{
Assert.Fail($"The following handlers are registered more than 1 time:\n" +
$"{string.Join("\n", duplicates)}");
}
2025-04-13 01:30:58 +03:00
}
}