highlight test needs multiple projects loaded

I have a plugin with a highlighting that enumerates the projects within the solution - it looks for a matching class within another project based on the current class and the namespace of the project(s).

I have written some highlighting unit tests for the simple cases and taken a look the AgentMulder highlight unit tests (which aren't quite what I need) but can someone tell me how to load a solution or 2 projects into my resharper test to use so I can improve my test coverage?

Many thanks.

4 comments
Comment actions Permalink

As far as I can see, there isn't a good example of creating a solution with multiple projects, but it does look like it's supported. The BaseTestWithSolution abstract class gives you a solution that you can run your tests against. The derived (but still abstract) class BaseTestWithSingleProject uses the support in BaseTestWithSolution to create a solution with a single project. You can implement something similar, but create multiple projects.

The BaseTestWithSingleProject class has a method DoTestSolution, which is called from the main test entry points, such as DoOneTest and DoNamedTest (in other words, all tests go through this), and it calls CreateSolutionConfiguration, which in turn creates a new instance of TestSolutionConfiguration, passing in a single TestProjectConfiguration. You would need to create a class similar to BaseTestWithSingleProject, and instead of passing one instance of TestProjectConfiguration, you pass multiple, then you should get a solution with multiple projects. Passing this TestSolutionConfiguration to BaseTestWithSolution's DoTestSolution will cause the solution to be created, which in turn creates the child projects. Once the projects are created, it will try to wire up project-to-project references based on TestProjectConfiguration.ProjectNamesToRefer.

So, there isn't an off-the-shelf solution to this, but you can create one using the support in BaseTestWithSolution.

Thanks
Matt

0
Comment actions Permalink

Thank you Matt - I've got something working based on your advice. How do I post code into the forum?

0
Comment actions Permalink

Good stuff. As for adding code to the posts, copy and paste, attach the files or post a link to a gist...

0
Comment actions Permalink

    [System.ComponentModel.Category("CSharp")]
    [TestFileExtension(".cs")]
    [TestFixture]
    public abstract class CSharpHighlightingWithinSolutionTestBase : BaseTest     
    {
        private ISolution _loadedTestSolution;

        protected abstract string SolutionName { get; }

        [CanBeNull]
        protected PsiLanguageType CompilerIdsLanguage
        {
            get { return CSharpLanguage.Instance; }
        }

        private TestSolutionManager SolutionManager
        {
            get
            {
                return ShellInstance.GetComponent<TestSolutionManager>();
            }
        }
        
        public override void TestFixtureSetUp()
        {            
            base.TestFixtureSetUp();
            RunGuarded(() =>
                           {
                               using (Locks.UsingWriteLock())
                               {
                                   FileSystemPath solutionFilePath = GetTestDataFilePath2(SolutionName);                                   
                                   _loadedTestSolution =                            
                                       SolutionManager.OpenExistingSolution(solutionFilePath);
                                   Assert.IsNotNull(_loadedTestSolution, "Failed to load solution " + solutionFilePath.FullPath);
                               }
                           });

        }
        
        public override void TestFixtureTearDown()
        {
            Assert.IsNotNull(_loadedTestSolution, "_loadedTestSolution == null");
            RunGuarded(() =>
                           {
                               ShellInstance.GetComponent<TestSolutionManager>().CloseSolution(_loadedTestSolution);
                               _loadedTestSolution = null;
                           });
           
            base.TestFixtureTearDown();
        }

        protected virtual TestHighlightingDumper CreateHighlightDumper(IPsiSourceFile sourceFile, TextWriter writer)
        {
            return new TestHighlightingDumper(sourceFile, writer, GetActiveStages(sourceFile.GetSolution()), HighlightingPredicate, CompilerIdsLanguage);
        }

        protected virtual IList<IDaemonStage> GetActiveStages(ISolution solution)
        {
            return DaemonStageManager.GetInstance(solution).Stages;
        }
           
        protected virtual bool HighlightingPredicate(IHighlighting highlighting, IContextBoundSettingsStore settingsstore)
        {
            return true;
        }
         
        public void DoTestFiles(string fullProjectPathToTestFile)
        {
            Assert.IsNotNull(_loadedTestSolution, "Expected solution to be loaded");
            bool processedFile = false;

            var listOfProjectPaths = new List<string>();
            RunGuarded(() =>                                 {                                     foreach (var project in _loadedTestSolution.GetAllProjects())                                     {                                         List<IProjectFile> projectFiles = project.GetAllProjectFiles().ToList();                                         foreach (IProjectFile projectFile in                                                 projectFiles.Where(p => p.LanguageType.Is<CSharpProjectFileType>()))                                         {                                                                          listOfProjectPaths.Add(projectFile.GetPresentableProjectPath());
                                            if (fullProjectPathToTestFile != projectFile.GetPresentableProjectPath())                                                 continue;                                             IPsiSourceFile sourceFile = projectFile.ToSourceFile();                                             Assert.IsNotNull(sourceFile);                                             if (!sourceFile.Properties.IsNonUserFile)                                             {                                                 Assert.IsTrue(projectFile.Kind == ProjectItemKind.PHYSICAL_FILE);                                                 ExecuteWithGold(projectFile.Location.FullPath                                                                 , (writer =>                                                                        {                                                                            var highlightDumper =                                                                                CreateHighlightDumper(sourceFile, writer);                                                                            highlightDumper.DoHighlighting(                                                                                DaemonProcessKind.VISIBLE_DOCUMENT);                                                                            highlightDumper.Dump();                                                                        }));                                                 processedFile = true;                                             }                                         }                                     }                                 });          if (!processedFile)
            {
                listOfProjectPaths.ForEach(f => System.Diagnostics.Trace.WriteLine("Located Item:" + f));
                Assert.Fail("Failed to project file by project path " + fullProjectPathToTestFile);
            }         
         }     }
0

Please sign in to leave a comment.