/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. using System;
  19. using ICSharpCode.NRefactory.TypeSystem;
  20. using ICSharpCode.NRefactory.TypeSystem.Implementation;
  21. using ICSharpCode.PackageManagement;
  22. using ICSharpCode.PackageManagement.EnvDTE;
  23. using ICSharpCode.SharpDevelop;
  24. using ICSharpCode.SharpDevelop.Dom;
  25. using ICSharpCode.SharpDevelop.Project;
  26. using ICSharpCode.SharpDevelop.Refactoring;
  27. using PackageManagement.Tests.Helpers;
  28. using Rhino.Mocks;
  29. namespace PackageManagement.Tests.EnvDTE
  30. {
  31. public class CodeModelTestBase : CSharpModelTestBase
  32. {
  33. protected CodeGenerator codeGenerator;
  34. protected CodeModelContext codeModelContext;
  35. protected string projectLanguage = "C#";
  36. protected ILanguageBinding languageBinding;
  37. public override void SetUp()
  38. {
  39. base.SetUp();
  40. project.Stub(p => p.Language).Return(null).WhenCalled(mi => mi.ReturnValue = projectLanguage);
  41. codeGenerator = MockRepository.GenerateMock<CodeGenerator>();
  42. languageBinding = MockRepository.GenerateMock<ILanguageBinding>();
  43. languageBinding.Stub(binding => binding.CodeGenerator).Return(codeGenerator);
  44. project.Stub(p => p.LanguageBinding).Return(languageBinding);
  45. codeModelContext = new CodeModelContext {
  46. CurrentProject = project
  47. };
  48. }
  49. protected CodeModel codeModel;
  50. protected Project dteProject;
  51. protected IPackageManagementProjectService projectService;
  52. protected IPackageManagementFileService fileService;
  53. protected TestableProject msbuildProject;
  54. protected void CreateCodeModel()
  55. {
  56. ISolution solution = ProjectHelper.CreateSolutionWithoutInitializingServicesForUnitTests();
  57. msbuildProject = ProjectHelper.CreateTestProject(solution, "MyProject");
  58. projectService = MockRepository.GenerateStub<IPackageManagementProjectService>();
  59. fileService = MockRepository.GenerateStub<IPackageManagementFileService>();
  60. dteProject = new Project(msbuildProject, projectService, fileService);
  61. codeModelContext.DteProject = dteProject;
  62. codeModel = new CodeModel(codeModelContext, dteProject);
  63. msbuildProject.SetAssemblyModel(assemblyModel);
  64. project.Stub(p => p.AssemblyModel).Return(assemblyModel);
  65. fileService
  66. .Stub(fs => fs.GetCompilationUnit(msbuildProject))
  67. .WhenCalled(compilation => compilation.ReturnValue = CreateCompilation());
  68. }
  69. ICompilation CreateCompilation()
  70. {
  71. var solutionSnapshot = new TestableSolutionSnapshot(msbuildProject);
  72. msbuildProject.SetProjectContent(projectContent);
  73. ICompilation compilation = new SimpleCompilation(solutionSnapshot, projectContent, projectContent.AssemblyReferences);
  74. solutionSnapshot.AddCompilation(projectContent, compilation);
  75. return compilation;
  76. }
  77. protected void CreateCompilationForUpdatedCodeFile(string fileName, string code)
  78. {
  79. fileService
  80. .Stub(fs => fs.GetCompilationUnit(fileName))
  81. .WhenCalled(compilation => {
  82. UpdateCodeFile(fileName, code);
  83. compilation.ReturnValue = CreateCompilation();
  84. })
  85. .Return(null); // HACK: Not returning null here but Rhino Mocks fails to work otherwise.
  86. }
  87. }
  88. }