├── .gitattributes ├── .gitignore ├── README.md ├── RhinoCommonUnitTesting.sln └── Src ├── Rhino.Props ├── RhinoPlugin.Tests.Xunit ├── RhinoPlugin.Tests.Xunit.csproj ├── XunitExampleTests.cs └── XunitTestFixture.cs └── RhinoPlugin.Tests ├── ExampleTests.cs ├── RhinoPlugin.Tests.csproj └── TestInit.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | [Mm]igrationBackup/ 25 | [Tt]estResults 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # DNX 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | 50 | *_i.c 51 | *_p.c 52 | *_i.h 53 | *.ilk 54 | *.meta 55 | *.obj 56 | *.pch 57 | *.pdb 58 | *.pgc 59 | *.pgd 60 | *.rsp 61 | *.sbr 62 | *.tlb 63 | *.tli 64 | *.tlh 65 | *.tmp 66 | *.tmp_proj 67 | *.log 68 | *.vspscc 69 | *.vssscc 70 | .builds 71 | *.pidb 72 | *.svclog 73 | *.scc 74 | 75 | # Chutzpah Test files 76 | _Chutzpah* 77 | 78 | # Visual C++ cache files 79 | ipch/ 80 | *.aps 81 | *.ncb 82 | *.opendb 83 | *.opensdf 84 | *.sdf 85 | *.cachefile 86 | *.VC.db 87 | *.VC.VC.opendb 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | *.sap 94 | 95 | # TFS 2012 Local Workspace 96 | $tf/ 97 | 98 | # Guidance Automation Toolkit 99 | *.gpState 100 | 101 | # ReSharper is a .NET coding add-in 102 | _ReSharper*/ 103 | *.[Rr]e[Ss]harper 104 | *.DotSettings.user 105 | 106 | # JustCode is a .NET coding add-in 107 | .JustCode 108 | 109 | # TeamCity is a build add-in 110 | _TeamCity* 111 | 112 | # DotCover is a Code Coverage Tool 113 | *.dotCover 114 | 115 | # NCrunch 116 | _NCrunch_* 117 | .*crunch*.local.xml 118 | nCrunchTemp_* 119 | 120 | # MightyMoose 121 | *.mm.* 122 | AutoTest.Net/ 123 | 124 | # Web workbench (sass) 125 | .sass-cache/ 126 | 127 | # Installshield output folder 128 | [Ee]xpress/ 129 | 130 | # DocProject is a documentation generator add-in 131 | DocProject/buildhelp/ 132 | DocProject/Help/*.HxT 133 | DocProject/Help/*.HxC 134 | DocProject/Help/*.hhc 135 | DocProject/Help/*.hhk 136 | DocProject/Help/*.hhp 137 | DocProject/Help/Html2 138 | DocProject/Help/html 139 | 140 | # Click-Once directory 141 | publish/ 142 | 143 | # Publish Web Output 144 | *.[Pp]ublish.xml 145 | *.azurePubxml 146 | # TODO: Comment the next line if you want to checkin your web deploy settings 147 | # but database connection strings (with potential passwords) will be unencrypted 148 | #*.pubxml 149 | *.publishproj 150 | 151 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 152 | # checkin your Azure Web App publish settings, but sensitive information contained 153 | # in these scripts will be unencrypted 154 | PublishScripts/ 155 | 156 | # NuGet Packages 157 | *.nupkg 158 | # The packages folder can be ignored because of Package Restore 159 | **/packages/* 160 | # except build/, which is used as an MSBuild target. 161 | !**/packages/build/ 162 | # Uncomment if necessary however generally it will be regenerated when needed 163 | #!**/packages/repositories.config 164 | # NuGet v3's project.json files produces more ignoreable files 165 | *.nuget.props 166 | *.nuget.targets 167 | 168 | # Microsoft Azure Build Output 169 | csx/ 170 | *.build.csdef 171 | 172 | # Microsoft Azure Emulator 173 | ecf/ 174 | rcf/ 175 | 176 | # Windows Store app package directories and files 177 | AppPackages/ 178 | BundleArtifacts/ 179 | Package.StoreAssociation.xml 180 | _pkginfo.txt 181 | 182 | # Visual Studio cache files 183 | # files ending in .cache can be ignored 184 | *.[Cc]ache 185 | # but keep track of directories ending in .cache 186 | !*.[Cc]ache/ 187 | 188 | # Others 189 | ClientBin/ 190 | ~$* 191 | *~ 192 | *.dbmdl 193 | *.dbproj.schemaview 194 | *.jfm 195 | *.pfx 196 | *.publishsettings 197 | node_modules/ 198 | orleans.codegen.cs 199 | 200 | # Since there are multiple workflows, uncomment next line to ignore bower_components 201 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 202 | #bower_components/ 203 | 204 | # RIA/Silverlight projects 205 | Generated_Code/ 206 | 207 | # Backup & report files from converting an old project file 208 | # to a newer Visual Studio version. Backup files are not needed, 209 | # because we have git ;-) 210 | _UpgradeReport_Files/ 211 | Backup*/ 212 | UpgradeLog*.XML 213 | UpgradeLog*.htm 214 | 215 | # SQL Server files 216 | *.mdf 217 | *.ldf 218 | 219 | # Business Intelligence projects 220 | *.rdl.data 221 | *.bim.layout 222 | *.bim_*.settings 223 | 224 | # Microsoft Fakes 225 | FakesAssemblies/ 226 | 227 | # GhostDoc plugin setting file 228 | *.GhostDoc.xml 229 | 230 | # Node.js Tools for Visual Studio 231 | .ntvs_analysis.dat 232 | 233 | # Visual Studio 6 build log 234 | *.plg 235 | 236 | # Visual Studio 6 workspace options file 237 | *.opt 238 | 239 | # Visual Studio LightSwitch build output 240 | **/*.HTMLClient/GeneratedArtifacts 241 | **/*.DesktopClient/GeneratedArtifacts 242 | **/*.DesktopClient/ModelManifest.xml 243 | **/*.Server/GeneratedArtifacts 244 | **/*.Server/ModelManifest.xml 245 | _Pvt_Extensions 246 | 247 | # Paket dependency manager 248 | .paket/paket.exe 249 | paket-files/ 250 | 251 | # FAKE - F# Make 252 | .fake/ 253 | 254 | # JetBrains Rider 255 | .idea/ 256 | *.sln.iml 257 | 258 | # CodeRush 259 | .cr/ 260 | 261 | # Python Tools for Visual Studio (PTVS) 262 | __pycache__/ 263 | *.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RhinoCommon Unit Testing 2 | 3 | ## Intro 4 | Example of unit testing RhinoCommon from within the Visual Studio test runner on windows. 5 | 6 | ## Prerequisites 7 | Rhino 7 8 | https://www.rhino3d.com/download/Rhino/7.0 9 | 10 | Visual Studio 2019 11 | https://www.visualstudio.com/downloads/ 12 | 13 | ## Test Framework 14 | This project provides examples for XUnit and MS Test, 15 | but the principles would be easily transferable to other frameworks if needs. 16 | 17 | ## How to Run 18 | - Build the solution 19 | - Test should appear in Visual Studio Test Explorer 20 | - Set test enviroment to x64 21 | `Test > Test Settings > Default Processor Architecture > x64` 22 | - Click `Run All` to run the tests 23 | 24 | ## Further Reading 25 | For more info on using Rhino in a headless environment see the Rhino Compute project: 26 | https://github.com/mcneel/compute.rhino3d 27 | 28 | ## Troubleshooting 29 | - Note that when Rhino is in headless mode there is no document defined. 30 | The static property `RhinoDoc.ActiveDoc` will thus be null which may trip up your plugin code. 31 | 32 | - If you have any problems getting this to work or you have a more complex use case, 33 | then please get in touch via the issue tracker. 34 | -------------------------------------------------------------------------------- /RhinoCommonUnitTesting.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31112.23 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7BA61FAC-BC11-45AA-92BD-425EC3249CCE}" 7 | ProjectSection(SolutionItems) = preProject 8 | README.md = README.md 9 | EndProjectSection 10 | EndProject 11 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RhinoPlugin.Tests.Xunit", "Src\RhinoPlugin.Tests.Xunit\RhinoPlugin.Tests.Xunit.csproj", "{804B1957-C51A-4A75-85EA-3575CFD77902}" 12 | EndProject 13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RhinoPlugin.Tests", "Src\RhinoPlugin.Tests\RhinoPlugin.Tests.csproj", "{5F877072-8EA6-4CE3-9A6B-6CF201E3CB73}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Release|Any CPU = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {804B1957-C51A-4A75-85EA-3575CFD77902}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {804B1957-C51A-4A75-85EA-3575CFD77902}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {804B1957-C51A-4A75-85EA-3575CFD77902}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {804B1957-C51A-4A75-85EA-3575CFD77902}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {5F877072-8EA6-4CE3-9A6B-6CF201E3CB73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {5F877072-8EA6-4CE3-9A6B-6CF201E3CB73}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {5F877072-8EA6-4CE3-9A6B-6CF201E3CB73}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {5F877072-8EA6-4CE3-9A6B-6CF201E3CB73}.Release|Any CPU.Build.0 = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(SolutionProperties) = preSolution 31 | HideSolutionNode = FALSE 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {346AF59F-B441-42F3-BD15-687F0C64AD49} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /Src/Rhino.Props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | C:\Program Files\Rhino WIP\System\RhinoCommon.dll 11 | False 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests.Xunit/RhinoPlugin.Tests.Xunit.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net48 5 | 6 | 7 | 8 | x64 9 | false 10 | 11 | 12 | 13 | x64 14 | 15 | 16 | 17 | 18 | 19 | 20 | all 21 | runtime; build; native; contentfiles; analyzers; buildtransitive 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests.Xunit/XunitExampleTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | using Rhino.Geometry; 4 | using Grasshopper.Kernel.Types; 5 | 6 | using Xunit; 7 | 8 | 9 | namespace RhinoPlugin.Tests.Xunit 10 | { 11 | /// 12 | /// XUnit tests 13 | /// 14 | [Collection("RhinoTestingCollection")] 15 | public class XunitExampleTests 16 | { 17 | XunitTestFixture _fixture; 18 | 19 | public XunitExampleTests(XunitTestFixture fixture) 20 | { 21 | fixture = _fixture; 22 | } 23 | 24 | [Fact] 25 | public void Brep_Translation() 26 | { 27 | 28 | // Arrange 29 | var bb = new BoundingBox(new Point3d(0, 0, 0), new Point3d(100, 100, 100)); 30 | var brep = bb.ToBrep(); 31 | var t = Transform.Translation(new Vector3d(30, 40, 50)); 32 | 33 | // Act 34 | brep.Transform(t); 35 | 36 | // Assert 37 | Assert.Equal(brep.GetBoundingBox(true).Center, new Point3d(80, 90, 100)); 38 | 39 | } 40 | 41 | /// 42 | /// Xunit Test to Intersect sphere with a plane to generate a circle 43 | /// 44 | [Fact] 45 | public void Brep_Intersection() 46 | { 47 | // Arrange 48 | var radius = 4.0; 49 | var brep = Brep.CreateFromSphere(new Sphere(new Point3d(), radius)); 50 | var cuttingPlane = Plane.WorldXY; 51 | 52 | // Act 53 | Rhino.Geometry.Intersect.Intersection.BrepPlane(brep, cuttingPlane, 0.001, out var curves, out var points); 54 | 55 | // Assert 56 | Assert.Single(curves); 57 | Assert.Equal(2 * Math.PI * radius, curves[0].GetLength()); 58 | } 59 | 60 | /// 61 | /// Xunit Test to ensure Centroid of GH_Box outputs a GH_Point 62 | /// 63 | [Fact] 64 | public void GHBox_Centroid_ReturnsGHPoint() 65 | { 66 | // Arrange 67 | var myBox = new GH_Box(new Box()); 68 | 69 | // Act 70 | var result = myBox.Boundingbox.Center; 71 | 72 | // Assert 73 | Assert.IsType(result); 74 | } 75 | 76 | } 77 | } -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests.Xunit/XunitTestFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Win32; 2 | using System; 3 | using System.IO; 4 | using System.Reflection; 5 | using Xunit; 6 | 7 | namespace RhinoPlugin.Tests.Xunit 8 | { 9 | 10 | /// 11 | /// Shared test context across unit tests that loads rhinocommon.dll and grasshopper.dll 12 | /// 13 | public class XunitTestFixture : IDisposable 14 | { 15 | private bool initialized = false; 16 | private static string rhinoDir; 17 | private Rhino.Runtime.InProcess.RhinoCore _rhinoCore; 18 | 19 | /// 20 | /// Empty Constuctor 21 | /// 22 | public XunitTestFixture() 23 | { 24 | //get the correct rhino 7 installation directory 25 | rhinoDir = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path", null) as string ?? string.Empty; 26 | Assert.True(Directory.Exists(rhinoDir), String.Format("Rhino system dir not found: {0}", rhinoDir)); 27 | 28 | // Make sure we are running the tests as 64x 29 | Assert.True(Environment.Is64BitProcess, "Tests must be run as x64"); 30 | 31 | if (initialized) 32 | { 33 | throw new InvalidOperationException("Initialize Rhino.Inside once"); 34 | } 35 | else 36 | { 37 | RhinoInside.Resolver.Initialize(); 38 | initialized = true; 39 | } 40 | 41 | // Set path to rhino system directory 42 | string envPath = Environment.GetEnvironmentVariable("path"); 43 | Environment.SetEnvironmentVariable("path", envPath + ";" + rhinoDir); 44 | 45 | // Start a headless rhino instance using Rhino.Inside 46 | StartRhino(); 47 | 48 | // We have to load grasshopper.dll on the current AppDomain manually for some reason 49 | AppDomain.CurrentDomain.AssemblyResolve += ResolveGrasshopper; 50 | 51 | } 52 | 53 | /// 54 | /// Starting Rhino - loading the relevant libraries 55 | /// 56 | [STAThread] 57 | public void StartRhino() 58 | { 59 | _rhinoCore = new Rhino.Runtime.InProcess.RhinoCore(null, Rhino.Runtime.InProcess.WindowStyle.NoWindow); 60 | } 61 | 62 | /// 63 | /// Add Grasshopper.dll to the current Appdomain 64 | /// 65 | private Assembly ResolveGrasshopper(object sender, ResolveEventArgs args) 66 | { 67 | var name = args.Name; 68 | 69 | if (!name.StartsWith("Grasshopper")) 70 | { 71 | return null; 72 | } 73 | 74 | var path = Path.Combine(Path.GetFullPath(Path.Combine(rhinoDir, @"..\")), "Plug-ins\\Grasshopper\\Grasshopper.dll"); 75 | return Assembly.LoadFrom(path); 76 | } 77 | 78 | /// 79 | /// Disposing the context after running all the tests 80 | /// 81 | public void Dispose() 82 | { 83 | // do nothing or... 84 | _rhinoCore?.Dispose(); 85 | _rhinoCore = null; 86 | } 87 | } 88 | 89 | 90 | /// 91 | /// Collection Fixture - shared context across test classes 92 | /// 93 | [CollectionDefinition("RhinoTestingCollection")] 94 | public class RhinoCollection : ICollectionFixture 95 | { 96 | // This class has no code, and is never created. Its purpose is simply 97 | // to be the place to apply [CollectionDefinition] and all the 98 | // ICollectionFixture<> interfaces. 99 | } 100 | 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests/ExampleTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using Rhino.Geometry; 4 | using Grasshopper.Kernel.Types; 5 | 6 | namespace RhinoPluginTests 7 | { 8 | /// 9 | /// Example test methods that invoke RhinoCommon and Grasshopper 10 | /// 11 | [TestClass] 12 | public class ExampleTests 13 | { 14 | /// 15 | /// Transform a brep using a translation 16 | /// 17 | [TestMethod] 18 | public void Brep_Translation() 19 | { 20 | // Arrange 21 | var bb = new BoundingBox(new Point3d(0, 0, 0), new Point3d(100, 100, 100)); 22 | var brep = bb.ToBrep(); 23 | var t = Transform.Translation(new Vector3d(30, 40, 50)); 24 | 25 | // Act 26 | brep.Transform(t); 27 | 28 | // Assert 29 | Assert.AreEqual(brep.GetBoundingBox(true).Center, new Point3d(80, 90, 100)); 30 | } 31 | 32 | /// 33 | /// Intersect sphere with a plane to generate a circle 34 | /// 35 | [TestMethod] 36 | public void Brep_Intersection() 37 | { 38 | // Arrange 39 | var radius = 4.0; 40 | var brep = Brep.CreateFromSphere(new Sphere(new Point3d(), radius)); 41 | var cuttingPlane = Plane.WorldXY; 42 | 43 | // Act 44 | Rhino.Geometry.Intersect.Intersection.BrepPlane(brep, cuttingPlane, 0.001, out var curves, out var points); 45 | 46 | // Assert 47 | Assert.AreEqual(1, curves.Length, "Wrong curve count"); 48 | Assert.AreEqual(2 * Math.PI * radius, curves[0].GetLength(), "Wrong curve length"); 49 | } 50 | 51 | /// 52 | /// Xunit Test to ensure Centroid of GH_Box outputs a GH_Point 53 | /// 54 | [TestMethod] 55 | public void GHBox_Centroid_ReturnsGHPoint() 56 | { 57 | // Arrange 58 | var myBox = new GH_Box(new Box()); 59 | 60 | // Act 61 | var result = myBox.Boundingbox.Center; 62 | 63 | // Assert 64 | Assert.IsInstanceOfType(result, typeof(Point3d)); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests/RhinoPlugin.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net48 5 | f9948ae1-17c8-453f-913f-300dbb089346 6 | 7 | 8 | 9 | x64 10 | 11 | 12 | 13 | x64 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Src/RhinoPlugin.Tests/TestInit.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | using System.IO; 4 | using System.Reflection; 5 | 6 | using Microsoft.Win32; 7 | 8 | namespace RhinoPluginTests 9 | { 10 | /// 11 | /// Shared VS test assembly class using Rhino.Inside 12 | /// 13 | [TestClass] 14 | public static class TestInit 15 | { 16 | private static bool initialized = false; 17 | private static string rhinoDir; 18 | 19 | 20 | [AssemblyInitialize] 21 | public static void AssemblyInitialize(TestContext context) 22 | { 23 | //get the correct rhino 7 installation directory 24 | rhinoDir = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path", null) as string ?? string.Empty; 25 | Assert.IsTrue(System.IO.Directory.Exists(rhinoDir), "Rhino system dir not found: {0}", rhinoDir); 26 | context.WriteLine(" The current Rhino 7 installation is " + rhinoDir); 27 | 28 | if (initialized) 29 | { 30 | throw new InvalidOperationException("Initialize Rhino.Inside once"); 31 | } 32 | else 33 | { 34 | RhinoInside.Resolver.Initialize(); 35 | initialized = true; 36 | context.WriteLine("Rhino.Inside init has started"); 37 | } 38 | 39 | // Ensure we are running the tests in x64 40 | Assert.IsTrue(Environment.Is64BitProcess, "Tests must be run as x64"); 41 | 42 | // Set path to rhino system directory 43 | string envPath = Environment.GetEnvironmentVariable("path"); 44 | Environment.SetEnvironmentVariable("path", envPath + ";" + rhinoDir); 45 | 46 | // Start a headless rhino instance using Rhino.Inside 47 | StartRhino(); 48 | 49 | // We have to load grasshopper.dll on the current AppDomain manually for some reason 50 | AppDomain.CurrentDomain.AssemblyResolve += ResolveGrasshopper; 51 | } 52 | 53 | /// 54 | /// Starting Rhino - loading the relevant libraries 55 | /// 56 | [STAThread] 57 | public static void StartRhino() 58 | { 59 | var _rhinoCore = new Rhino.Runtime.InProcess.RhinoCore(null, Rhino.Runtime.InProcess.WindowStyle.NoWindow); 60 | } 61 | 62 | 63 | /// 64 | /// Add Grasshopper.dll to the current Appdomain 65 | /// 66 | private static Assembly ResolveGrasshopper(object sender, ResolveEventArgs args) 67 | { 68 | var name = args.Name; 69 | 70 | if (!name.StartsWith("Grasshopper")) 71 | { 72 | return null; 73 | } 74 | 75 | var path = Path.Combine(Path.GetFullPath(Path.Combine(rhinoDir, @"..\")), "Plug-ins\\Grasshopper\\Grasshopper.dll"); 76 | return Assembly.LoadFrom(path); 77 | } 78 | } 79 | } 80 | 81 | --------------------------------------------------------------------------------