public void ShouldResolveToVBTemplateWhenProjectIsVB()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			MyArtifactLink link = CreateLink(myModelElement, EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);

			StringAssert.Contains(result[link.ItemPath], @"Generated from VB");
		}
		public void TestMyTemplateGetsCalled()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			MyArtifactLink link = CreateLink(myModelElement);
			
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);

			Assert.AreEqual<string>(message, result[link.ItemPath]);
		}
		public void ShouldSelectAnyTextTemplateIfLanguageSpecificNotAvailable()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			ProjectNode project = GetTestProjectNode(EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB);
			MyLanguageIndependentArtifactLink link = new MyLanguageIndependentArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);

			Assert.AreEqual<string>("NoTemplate.any.tt", result[link.ItemPath]);
		}
		public void TestValidationWithOneMel()
		{
			Store store = new Store(new Type[] { typeof(MockDomainModel) });
			Partition partition = new Partition(store);

			using(Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement mockModelElement = new ExtensibleMockModelElement(partition, "Foo");

				ValidationController controller = new ValidationController();

				bool Isvalid = ModelValidator.ValidateModelElement(mockModelElement, controller);

				Assert.AreEqual(true, Isvalid);

				t.Rollback();
			}
		}
		public void InvokeHostWithModel()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition,string.Empty);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel,null);
				string templateContent = GetStandardTemplateHeader() + @"
					<#= this.Model.DomainModelInfo.Id #>";

				string transformResult = engine.ProcessTemplate(
					templateContent,
					host);
				Assert.AreEqual(domainModel.DomainModelInfo.Id, new Guid(transformResult));
				t.Rollback();
			}
		}
		public void ShouldAddElementThatMatchType()
		{
			List<ModelElement> elementList = new List<ModelElement>();

			Store store = new Store(new Type[] { typeof(MockDomainModel) });
			Partition partition = new Partition(store);
			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, "TestMessage");

				DepthFirstElementWalker elementWalker = new DepthFirstElementWalker(
					new TypeMatchingElementVisitor<IExtensibleObject>(elementList),
					new EmbeddingVisitorFilter()
					);

				elementWalker.DoTraverse(myModelElement);
				t.Rollback();

				Assert.AreEqual<int>(1, elementList.Count);
			}
		}
		public void TestWalkingWithOneMel()
		{
			List<ModelElement> elementList = new List<ModelElement>();
			Store store = new Store(new Type[] { typeof(MockDomainModel) });
			Partition partition = new Partition(store);

			using(Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement mockModelElement = new ExtensibleMockModelElement(partition, "Foo");

				FullDepthElementWalker elementWalker =
					new FullDepthElementWalker(
						new ModelElementVisitor(elementList),
						new EmbeddingReferenceVisitorFilter(),
						false);

				elementWalker.DoTraverse(mockModelElement);

				Assert.AreEqual(1, elementList.Count);

				t.Rollback();
			}
		}
		public void ShouldGetAssemblyReferences()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			ProjectNode project = GetTestProjectNode();
			AssemblyReferencesArtifactLink link = new AssemblyReferencesArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			strategy.Generate(link);

			Assert.AreEqual<int>(2, strategy.AssemblyReferences.Count);
		}
			public ErrorArtifactLink(ExtensibleMockModelElement modelElement)
				: base(modelElement)
			{
				message = modelElement.Message;
			}
			public VbOnlyArtifactLink(ExtensibleMockModelElement modelElement)
			{
				this.modelElement = modelElement;
			}
		public void TestExceptionsGetLogged()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			ProjectNode project = GetTestProjectNode();
			ErrorArtifactLink link = new ErrorArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);
			
			Assert.IsFalse(result.Values.Contains(typeof(InvalidOperationException).Name));
			Assert.IsTrue(result.Values.Contains(message));
			Assert.AreEqual<int>(2, strategy.Errors.Count);
			Assert.IsTrue(strategy.Errors[1].Message.Contains(message));
		}
		public void TestCancelOutputGetsCalled()
		{
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, "");
			ProjectNode project = GetTestProjectNode();
			CancelOutputArtifactLink link = new CancelOutputArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);

			Assert.AreEqual<int>(0, result.Count);
		}
		public void TestCompileErrorsGetLogged()
		{
			string message = "Foo";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);
			ProjectNode project = GetTestProjectNode();
			CompileErrorArtifactLink link = new CompileErrorArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);
			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			IDictionary<string, string> result = strategy.Generate(link);
			
			Assert.AreEqual<int>(1, strategy.Errors.Count);
			Assert.IsTrue(strategy.Errors[0].Message.Contains(message));
		}
		public void HostReturnsEmptyContentOnCancelOutput()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null);
				string templateContent = GetStandardTemplateHeader() + @"<# CancelOutput(); #>";

				string transformResult = engine.ProcessTemplate(templateContent, host);

				Assert.AreEqual<int>(0, host.CompilerErrors.Count);
				Assert.IsFalse(host.GenerateOutput);

				t.Rollback();
			}
		}
		public void HostReturnsErrorsInCollection()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null);
				string transformResult = engine.ProcessTemplate(
					GetStandardTemplateHeader() + @"
					<# throw new global::System.Exception(""TestException""); #>",
					host);

				Assert.AreEqual<int>(2, host.CompilerErrors.Count);
				Assert.IsTrue(host.CompilerErrors[1].ErrorText.Contains("TestException"),"Could not find expected exception in compiler errors."); 

				t.Rollback();
			}
		}
		public void ShouldGenerateErrorIfNoApplicableTemplate()
		{
			string message = "Hello World";
			ExtensibleMockModelElement myModelElement = new ExtensibleMockModelElement(partition, message);

			// CSharp language project should look for C# template then any, but link only has VB.
			ProjectNode project = GetTestProjectNode(EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp);

			VbOnlyArtifactLink link = new VbOnlyArtifactLink(myModelElement);
			Utility.SetData<IServiceProvider>(link, new MockServiceProvider());
			Utility.SetData<ProjectNode>(link, project);

			TextTemplateCodeGenerationStrategy strategy = new TextTemplateCodeGenerationStrategy();
			strategy.ResourceResolver = link;
			strategy.Generate(link);
		}
		private MyArtifactLink CreateLink(ExtensibleMockModelElement element, string lang)
		{
			MyArtifactLink link = new MyArtifactLink(element);
			link.Data.Add(typeof(ProjectNode).FullName, GetTestProjectNode(lang));
			link.Data.Add(typeof(IServiceProvider).FullName, new MockServiceProvider());
			return link;
		}
		private MyArtifactLink CreateLink(ExtensibleMockModelElement element)
		{
			return CreateLink(element, EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp);
		}
		public void CanGetCSharpTypeOutput()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null);
				string transformResult = engine.ProcessTemplate(
					GetStandardTemplateHeader() + @"
					<#= Utility.GetCSharpTypeOutput(""System.String"") #>",
					host);
				Assert.IsTrue(transformResult.Contains("string"));
				t.Rollback();
			}
		}
			public CancelOutputArtifactLink(ExtensibleMockModelElement modelElement)
				: base(modelElement)
			{
			}
		public void HostReturnsWarningsFromLogCall()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			const string LogMessage = "Message1";
			const string LogTitle = "Title1";

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null);
				string transformResult = engine.ProcessTemplate(
					GetStandardTemplateHeader() + @"
					<# LogWarning(""" + LogMessage + @""",""" + LogTitle + @"""); #>",
					host);
				Assert.AreEqual<int>(1, host.CompilerErrors.Count);
				Assert.IsTrue(host.CompilerErrors[0].IsWarning);
				Assert.IsTrue(host.CompilerErrors[0].ErrorNumber.Contains(LogMessage), "Could not find expected error in compiler errors.");
				Assert.IsTrue(host.CompilerErrors[0].ErrorText.Contains(LogTitle), "Could not find expected error in compiler errors.");

				t.Rollback();
			}
		}
			public CompileErrorArtifactLink(ExtensibleMockModelElement modelElement)
				: base(modelElement)
			{
				funcName = modelElement.Message;
			}
		private string IsValid(bool expectedValue)
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty);
				MockCodeGenerationService cgs = new MockCodeGenerationService(expectedValue);
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel, cgs);
				string transformResult = engine.ProcessTemplate(
					GetStandardTemplateHeader().Replace("/n","") + @"<#= this.IsValid(CurrentElement.InvalidArtifactLink).ToString()#>",
					host);
				t.Rollback();
				return transformResult.Trim();
			}
		}
			public AssemblyReferencesArtifactLink(ExtensibleMockModelElement modelElement)
				: base(modelElement)
			{
			}
		public void CanAddProjectReference()
		{
			Engine engine = new Engine();
			Store store = new Store(typeof(MockDomainModel));
			MockDomainModel domainModel = GetModel(store);

			using (Transaction t = store.TransactionManager.BeginTransaction())
			{
				ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, "test");
				serviceModel.ObjectExtender = new MockObjectExtender();
				TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel);
				host.StandardAssemblyReferences.Add(typeof(Microsoft.Practices.Modeling.ExtensionProvider.Extension.ObjectExtender<ExtensibleMockModelElement>).Assembly.FullName);
				
				string transformResult = engine.ProcessTemplate(
					GetStandardTemplateHeader() + @"
					<# AddProjectReference(CurrentExtender.ArtifactLink); #>",
					host);
				Assert.AreEqual(1, host.ProjectReferences.Count);
				t.Rollback();
			}
		}
			public MyLanguageIndependentArtifactLink(ExtensibleMockModelElement modelElement)
			{
				this.modelElement = modelElement;
			}