Skip to content

Commit

Permalink
Refactoring and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Dec 11, 2024
1 parent a4ddc74 commit f88cf00
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class ExtensionAssemblyTests
{
private static readonly Assembly THIS_ASSEMBLY = Assembly.GetExecutingAssembly();
private static readonly string THIS_ASSEMBLY_PATH = THIS_ASSEMBLY.Location;
private static readonly string THIS_ASSEMBLY_FULL_NAME = THIS_ASSEMBLY.GetName().FullName;
private static readonly string THIS_ASSEMBLY_NAME = THIS_ASSEMBLY.GetName().Name;
private static readonly Version THIS_ASSEMBLY_VERSION = THIS_ASSEMBLY.GetName().Version;

Expand All @@ -23,18 +22,6 @@ public void CreateExtensionAssemblies()
_ea = new ExtensionAssembly(THIS_ASSEMBLY_PATH, false);
}

[Test]
public void AssemblyDefinition()
{
Assert.That(_ea.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_FULL_NAME));
}

[Test]
public void MainModule()
{
Assert.That(_ea.MainModule.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_FULL_NAME));
}

[Test]
public void AssemblyName()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.ComponentModel;
using System.Reflection;
using NUnit.Engine.Extensibility;
using NUnit.Framework;

namespace NUnit.Engine.Internal.Tests
{
public class ExtensionAssemblyTrackerTests
{
private static readonly Assembly THIS_ASSEMBLY = typeof(ExtensionAssemblyTrackerTests).Assembly;
private static readonly string THIS_ASSEMBLY_PATH = THIS_ASSEMBLY.Location;
private static readonly string THIS_ASSEMBLY_NAME = THIS_ASSEMBLY.GetName().Name;
private static readonly Version THIS_ASSEMBLY_VERSION = THIS_ASSEMBLY.GetName().Version;
private static readonly ExtensionAssembly TEST_EXTENSION_ASSEMBLY =
new ExtensionAssembly(THIS_ASSEMBLY_PATH, false, THIS_ASSEMBLY_NAME, THIS_ASSEMBLY_VERSION);

private ExtensionAssemblyTracker _tracker;

[SetUp]
public void CreateTracker()
{
_tracker = new ExtensionAssemblyTracker();
}

[Test]
public void AddToList()
{
_tracker.Add(TEST_EXTENSION_ASSEMBLY);

Assert.That(_tracker.Count, Is.EqualTo(1));
Assert.That(_tracker[0].FilePath, Is.EqualTo(THIS_ASSEMBLY_PATH));
Assert.That(_tracker[0].AssemblyName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_tracker[0].AssemblyVersion, Is.EqualTo(THIS_ASSEMBLY_VERSION));
}

[Test]
public void AddUpdatesNameIndex()
{
_tracker.Add(TEST_EXTENSION_ASSEMBLY);

Assert.That(_tracker.ByName.ContainsKey(THIS_ASSEMBLY_NAME));
Assert.That(_tracker.ByName[THIS_ASSEMBLY_NAME].AssemblyName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_tracker.ByName[THIS_ASSEMBLY_NAME].FilePath, Is.EqualTo(THIS_ASSEMBLY_PATH));
Assert.That(_tracker.ByName[THIS_ASSEMBLY_NAME].AssemblyVersion, Is.EqualTo(THIS_ASSEMBLY_VERSION));
}
[Test]
public void AddUpdatesPathIndex()
{
_tracker.Add(TEST_EXTENSION_ASSEMBLY);

Assert.That(_tracker.ByPath.ContainsKey(THIS_ASSEMBLY_PATH));
Assert.That(_tracker.ByPath[THIS_ASSEMBLY_PATH].AssemblyName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_tracker.ByPath[THIS_ASSEMBLY_PATH].FilePath, Is.EqualTo(THIS_ASSEMBLY_PATH));
Assert.That(_tracker.ByPath[THIS_ASSEMBLY_PATH].AssemblyVersion, Is.EqualTo(THIS_ASSEMBLY_VERSION));
}

[Test]
public void AddDuplicatePathThrowsArgumentException()
{
_tracker.Add(TEST_EXTENSION_ASSEMBLY);

Assert.That(() =>
_tracker.Add(TEST_EXTENSION_ASSEMBLY),
Throws.TypeOf<System.ArgumentException>());
}

[Test]
public void AddDuplicateAssemblyNameThrowsArgumentException()
{
_tracker.Add(TEST_EXTENSION_ASSEMBLY);

Assert.That(() => _tracker.Add(new ExtensionAssembly("Some/Other/Path", false, THIS_ASSEMBLY_NAME, THIS_ASSEMBLY_VERSION)),
Throws.TypeOf<System.ArgumentException>());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
using System.IO;
using TestCentric.Metadata;
using NUnit.Engine.Internal;
Expand All @@ -14,26 +15,26 @@ public ExtensionAssembly(string filePath, bool fromWildCard)
FilePath = filePath;
FromWildCard = fromWildCard;
Assembly = GetAssemblyDefinition();
AssemblyName = Assembly.Name.Name;
AssemblyVersion = Assembly.Name.Version;
}

// Internal constructor used for certain tests. AssemblyDefinition is not initialized.
internal ExtensionAssembly(string filePath, bool fromWildCard, string assemblyName, Version version)
{
FilePath = filePath;
FromWildCard = fromWildCard;
AssemblyName = assemblyName;
AssemblyVersion = version;
}

public string FilePath { get; }
public bool FromWildCard { get; }
public AssemblyDefinition Assembly { get; }

public string AssemblyName
{
get { return Assembly.Name.Name; }
}

public Version AssemblyVersion
{
get { return Assembly.Name.Version; }
}
public string AssemblyName { get; }

public ModuleDefinition MainModule
{
get { return Assembly.MainModule; }
}
public Version AssemblyVersion { get; }

#if NETFRAMEWORK
public RuntimeFramework TargetFramework
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using NUnit.Engine.Extensibility;
using System.Collections.Generic;

namespace NUnit.Engine.Internal
{
/// <summary>
/// This is a simple utility class used by the ExtensionManager to keep track of ExtensionAssemblies.
/// It is a List of ExtensionAssemblies and also provides indices by file path and assembly name.
/// It allows writing tests to show that no duplicate extension assemblies are loaded.
/// </summary>
internal class ExtensionAssemblyTracker : List<ExtensionAssembly>
{
public Dictionary<string, ExtensionAssembly> ByPath = new Dictionary<string, ExtensionAssembly>();
public Dictionary<string, ExtensionAssembly> ByName = new Dictionary<string, ExtensionAssembly>();

public new void Add(ExtensionAssembly assembly)
{
base.Add(assembly);
ByPath.Add(assembly.FilePath, assembly);
ByName.Add(assembly.AssemblyName, assembly);
}
}
}
Loading

0 comments on commit f88cf00

Please sign in to comment.