PageRenderTime 30ms CodeModel.GetById 9ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeModelTestBase.cs

https://github.com/ajadex/SharpDevelop
C# | 101 lines | 71 code | 13 blank | 17 comment | 0 complexity | 791ca60c7acebd98b08e304e1dc88480 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using ICSharpCode.NRefactory.TypeSystem;
 21using ICSharpCode.NRefactory.TypeSystem.Implementation;
 22using ICSharpCode.PackageManagement;
 23using ICSharpCode.PackageManagement.EnvDTE;
 24using ICSharpCode.SharpDevelop;
 25using ICSharpCode.SharpDevelop.Dom;
 26using ICSharpCode.SharpDevelop.Project;
 27using ICSharpCode.SharpDevelop.Refactoring;
 28using PackageManagement.Tests.Helpers;
 29using Rhino.Mocks;
 30
 31namespace PackageManagement.Tests.EnvDTE
 32{
 33	public class CodeModelTestBase : CSharpModelTestBase
 34	{
 35		protected CodeGenerator codeGenerator;
 36		protected CodeModelContext codeModelContext;
 37		protected string projectLanguage = "C#";
 38		protected ILanguageBinding languageBinding;
 39		
 40		public override void SetUp()
 41		{
 42			base.SetUp();
 43			project.Stub(p => p.Language).Return(null).WhenCalled(mi => mi.ReturnValue = projectLanguage);
 44			
 45			codeGenerator = MockRepository.GenerateMock<CodeGenerator>();
 46			languageBinding = MockRepository.GenerateMock<ILanguageBinding>();
 47			languageBinding.Stub(binding => binding.CodeGenerator).Return(codeGenerator);
 48			project.Stub(p => p.LanguageBinding).Return(languageBinding);
 49			
 50			codeModelContext = new CodeModelContext {
 51				CurrentProject = project
 52			};
 53		}
 54		
 55		protected CodeModel codeModel;
 56		protected Project dteProject;
 57		protected IPackageManagementProjectService projectService;
 58		protected IPackageManagementFileService fileService;
 59		protected TestableProject msbuildProject;
 60		
 61		protected void CreateCodeModel()
 62		{
 63			ISolution solution = ProjectHelper.CreateSolutionWithoutInitializingServicesForUnitTests();
 64			msbuildProject = ProjectHelper.CreateTestProject(solution, "MyProject");
 65			
 66			projectService = MockRepository.GenerateStub<IPackageManagementProjectService>();
 67			fileService = MockRepository.GenerateStub<IPackageManagementFileService>();
 68			dteProject = new Project(msbuildProject, projectService, fileService);
 69			codeModelContext.DteProject = dteProject;
 70			
 71			codeModel = new CodeModel(codeModelContext, dteProject);
 72			
 73			msbuildProject.SetAssemblyModel(assemblyModel);
 74			project.Stub(p => p.AssemblyModel).Return(assemblyModel);
 75			
 76			fileService
 77				.Stub(fs => fs.GetCompilationUnit(msbuildProject))
 78				.WhenCalled(compilation => compilation.ReturnValue = CreateCompilation());
 79		}
 80		
 81		ICompilation CreateCompilation()
 82		{
 83			var solutionSnapshot = new TestableSolutionSnapshot(msbuildProject);
 84			msbuildProject.SetProjectContent(projectContent);
 85			ICompilation compilation = new SimpleCompilation(solutionSnapshot, projectContent, projectContent.AssemblyReferences);
 86			solutionSnapshot.AddCompilation(projectContent, compilation);
 87			return compilation;
 88		}
 89		
 90		protected void CreateCompilationForUpdatedCodeFile(string fileName, string code)
 91		{
 92			fileService
 93				.Stub(fs => fs.GetCompilationUnit(fileName))
 94				.WhenCalled(compilation => {
 95					UpdateCodeFile(fileName, code);
 96					compilation.ReturnValue = CreateCompilation();
 97				})
 98				.Return(null); // HACK: Not returning null here but Rhino Mocks fails to work otherwise.
 99		}
100	}
101}