chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using NUnit.Framework;
using System.Linq;
using System.Reflection;
using System.IO;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class PluginSearcherTests
    {
        [Test]
        public void PluginSearcherBasics()
        {
            PluginSearcher searcher = new PluginSearcher();
 
            searcher.Search(new string[] { "OpenTap.UnitTests.dll", "OpenTap.dll" });
            CollectionAssert.AllItemsAreInstancesOfType(searcher.PluginTypes.ToList(), typeof(TypeData));
            CollectionAssert.AllItemsAreNotNull(searcher.PluginTypes.ToList());
            CollectionAssert.AllItemsAreUnique(searcher.PluginTypes.ToList());
 
            var instrType = searcher.PluginTypes.FirstOrDefault(st => st.Name == "OpenTap.IInstrument");
            Assert.IsNotNull(instrType);
            // Test of SearchType.PluginTypes (should be the type itself, as this is IInstrument is a plugin type - it directly implements ITapPlugin)
            Assert.AreEqual(instrType, instrType.PluginTypes.First());
            // Test of SearchType.Display
            Assert.IsNotNull(instrType.Display);
            Assert.AreEqual("Instrument", instrType.Display.Name);
            // Test of SearchType.Assembly
            Assert.AreEqual("OpenTap", instrType.Assembly.Name);
 
            var instrImplType = searcher.PluginTypes.FirstOrDefault(st => st.Name == "OpenTap.Engine.UnitTests.InstrumentTest");
            Assert.IsNotNull(instrImplType);
            // Test of SearchType.PluginTypes
            Assert.AreEqual(1, instrImplType.PluginTypes.Count());
            Assert.AreEqual(instrType, instrImplType.PluginTypes.First());
            // Test of SearchType.DerivedTypes
            CollectionAssert.Contains(instrType.DerivedTypes.ToArray(), instrImplType);
            // Test of SearchAssembly.References
            CollectionAssert.Contains(instrImplType.Assembly.References.ToList(), instrType.Assembly);
            // Test of SearchAssembly.Load()
            Assert.AreEqual(Assembly.GetExecutingAssembly(),instrImplType.Assembly.Load());
            // Test of SearchType.Load()
            Assert.AreEqual(typeof(OpenTap.Engine.UnitTests.InstrumentTest), instrImplType.Load());
            
            // Test of nested class
            var stepType = searcher.PluginTypes.FirstOrDefault(st => st.Name == "OpenTap.Engine.UnitTests.TestPlanTestFixture1+TestPlanTestStep");
            Assert.IsNotNull(stepType);
 
            CollectionAssert.IsSubsetOf(searcher.PluginTypes.ToList(), searcher.AllTypes.Values);
        }
 
        [Test]
        public void SameAssemblyTwice()
        {
            PluginSearcher searcher = new PluginSearcher();
            try
            {
                Directory.CreateDirectory("SameAssemblyTwiceTestDir");
                File.Copy("OpenTap.UnitTests.dll", "SameAssemblyTwiceTestDir/OpenTap.UnitTests.dll",true);
                searcher.Search(new string[] { "OpenTap.UnitTests.dll", "OpenTap.dll", "SameAssemblyTwiceTestDir/OpenTap.UnitTests.dll" });
                Assert.AreEqual(2, searcher.Assemblies.Count());
            }
            finally
            {
                Directory.Delete("SameAssemblyTwiceTestDir",true);
            }
        }
 
        [Test]
        public void SearchTwice()
        {
            PluginSearcher searcher = new PluginSearcher();
            try
            {
                Directory.CreateDirectory("SameAssemblyTwiceTestDir");
                File.Copy("OpenTap.UnitTests.dll", "SameAssemblyTwiceTestDir/OpenTap.UnitTests.dll", true);
                searcher.Search(new string[] { "OpenTap.UnitTests.dll" });
                Assert.AreEqual(1, searcher.Assemblies.Count());
                searcher.Search(new string[] { "OpenTap.dll" });
                Assert.AreEqual(2, searcher.Assemblies.Count());
            }
            finally
            {
                Directory.Delete("SameAssemblyTwiceTestDir", true);
            }
        }
        
        [Test]
        public void LocateSystemNetIPAddressTest()
        {
            // the plugin searcher does not always locate .NET assemblies if they are part of the framework.
            // However if they are ever mentioned in respect to a Type, then we must make sure it is correctly loaded. 
            var type1 = TypeData.FromType(typeof(System.Net.IPAddress));
            var type2 = TypeData.GetTypeData("System.Net.IPAddress");
            var type3 = TypeData.GetTypeData(new System.Net.IPAddress(new byte[]{127, 0, 0, 1}));
            
            Assert.IsNotNull(type1);
            Assert.IsNotNull(type2);
            Assert.IsNotNull(type3);
            Assert.AreEqual(type1, type2);
            Assert.AreEqual(type3, type2);
        }
    }
}