Exemplo n.º 1
0
        public void TestBasicReversibleBooleanSwitches()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <BoolProperty Name=`GlobalOptimizations` Switch=`Og` ReverseSwitch=`Og-` />
                                       <BoolProperty Name=`IntrinsicFunctions` Switch=`Oi` ReverseSwitch=`Oi:NO` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.Equal(2, properties.Count);      // "Expected two properties but there were " + properties.Count
            Assert.NotNull(properties.First.Value); // "GlobalOptimizations switch should exist"
            Assert.Equal("GlobalOptimizations", properties.First.Value.Name);
            Assert.Equal("Og", properties.First.Value.SwitchName);
            Assert.Equal("Og-", properties.First.Value.ReverseSwitchName);
            Assert.Equal("true", properties.First.Value.Reversible); // "Switch should be marked as reversible"

            properties.RemoveFirst();

            Assert.NotNull(properties.First.Value); // "IntrinsicFunctions switch should exist"
            Assert.Equal("IntrinsicFunctions", properties.First.Value.Name);
            Assert.Equal("Oi", properties.First.Value.SwitchName);
            Assert.Equal("Oi:NO", properties.First.Value.ReverseSwitchName);
            Assert.Equal("true", properties.First.Value.Reversible); // "Switch should be marked as reversible"
            Assert.Equal(PropertyType.Boolean, properties.First.Value.Type);
        }
Exemplo n.º 2
0
        public void TestGenerateToFile()
        {
            string xml = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                             <Rule Name=`CL`>
                               <EnumProperty Name=`GeneratePreprocessedFile` Switch=`nologo`>
                                 <EnumValue Name=`Disabled` />
                                 <EnumValue Name=`Yes` Switch=`P` />
                                 <EnumValue Name=`NoLineNumbers` Switch=`EP` />
                               </EnumProperty>
                             </Rule>
                           </ProjectSchemaDefinitions>";

            TaskParser      tp            = XamlTestHelpers.LoadAndParse(xml, "CL");
            TaskGenerator   tg            = new TaskGenerator(tp);
            CodeCompileUnit compileUnit   = tg.GenerateCode();
            CodeDomProvider codeGenerator = CodeDomProvider.CreateProvider("CSharp");

            try
            {
                using (StreamWriter sw = new StreamWriter("XamlTaskFactory_Tests_TestGenerateToFile.cs"))
                {
                    CodeGeneratorOptions options = new CodeGeneratorOptions();
                    options.BlankLinesBetweenMembers = true;
                    options.BracingStyle             = "C";

                    codeGenerator.GenerateCodeFromCompileUnit(compileUnit, sw, options);
                }

                CSharpCodeProvider provider = new CSharpCodeProvider();
                // Build the parameters for source compilation.
                CompilerParameters cp = new CompilerParameters();

                // Add an assembly reference.
                cp.ReferencedAssemblies.Add("System.dll");
                cp.ReferencedAssemblies.Add("System.Xml.dll");
                cp.ReferencedAssemblies.Add(Path.Combine(XamlTestHelpers.PathToMSBuildBinaries, "Microsoft.Build.Utilities.Core.dll"));
                cp.ReferencedAssemblies.Add(Path.Combine(XamlTestHelpers.PathToMSBuildBinaries, "Microsoft.Build.Tasks.Core.dll"));
                cp.ReferencedAssemblies.Add(Path.Combine(XamlTestHelpers.PathToMSBuildBinaries, "Microsoft.Build.Framework.dll"));
                cp.ReferencedAssemblies.Add("System.Data.dll");

                // Generate an executable instead of
                // a class library.
                cp.GenerateExecutable = false;
                // Set the assembly file name to generate.
                cp.GenerateInMemory = true;
                // Invoke compilation
                CompilerResults cr = provider.CompileAssemblyFromFile(cp, "XamlTaskFactory_Tests_TestGenerateToFile.cs");
                Assert.Empty(cr.Errors); // "Compilation Failed"
            }
            finally
            {
                if (File.Exists("XamlTaskFactory_Tests_TestGenerateToFile.cs"))
                {
                    File.Delete("XamlTaskFactory_Tests_TestGenerateToFile.cs");
                }
            }
        }
Exemplo n.º 3
0
        public void TestGetNamespace()
        {
            string xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL` Namespace=`mynamespace`>
                                       <BoolProperty Name=`GlobalOptimization` Switch=`Og` ReverseSwitch=`Og-` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";

            TaskParser tp = XamlTestHelpers.LoadAndParse(xmlContents, "CL");
            Assert.Equal("XamlTaskNamespace", tp.Namespace);
        }
Exemplo n.º 4
0
        public void TestDynamicEnumProperty()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <DynamicEnumProperty Name=`CLBeforeTargets` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.Single(properties);                                      // "Expected one property but there were " + properties.Count
            Assert.NotNull(properties.First.Value);                         // "CLBeforeTargets switch should exist"
            Assert.Equal("CLBeforeTargets", properties.First.Value.Name);
            Assert.Equal(PropertyType.String, properties.First.Value.Type); // Enum properties are represented as string types
        }
Exemplo n.º 5
0
        public void TestParseIncorrect_PropertyNamesMustBeUnique()
        {
            string incorrectXmlContents = @"<ProjectSchemaDefinitions
                                       xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework`
                                       xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml`
                                       xmlns:sys=`clr-namespace:System;assembly=mscorlib`
                                       xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <BoolProperty Name=`SameName` Switch=`Og` ReverseSwitch=`Og-` />
                                       <BoolProperty Name=`SameName` Switch=`Og` ReverseSwitch=`Og-` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";

            Should
            .Throw <XamlParseException>(() => XamlTestHelpers.LoadAndParse(incorrectXmlContents, "CL"))
            .Message.ShouldStartWith("MSB3724");
        }
Exemplo n.º 6
0
        public void TestBasicStringProperty()
        {
            string xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <StringProperty Name=`TargetAssembly` Switch=`/target:&quot;[value]&quot;` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList<Property> properties = tp.Properties;

            Assert.Equal(1, properties.Count); // "Expected one property but there were " + properties.Count
            Assert.NotNull(properties.First.Value); // "TargetAssembly switch should exist"
            Assert.Equal("TargetAssembly", properties.First.Value.Name);
            Assert.Equal(PropertyType.String, properties.First.Value.Type);
            Assert.Equal("/target:\"[value]\"", properties.First.Value.SwitchName);
        }
Exemplo n.º 7
0
        public void TestBasicNonReversibleBooleanSwitch_WithDefault()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <BoolProperty Name=`SuppressStartupBanner` Switch=`nologo` Default=`true` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.Single(properties);              // "Expected one property but there were " + properties.Count
            Assert.NotNull(properties.First.Value); // "SuppressStartupBanner switch should exist"
            Assert.Equal("SuppressStartupBanner", properties.First.Value.Name);
            Assert.Equal("nologo", properties.First.Value.SwitchName);
            Assert.Null(properties.First.Value.ReverseSwitchName);         // "SuppressStartupBanner shouldn't have a reverse switch value"
            Assert.Equal(String.Empty, properties.First.Value.Reversible); // "Switch should NOT be marked as reversible"
            Assert.Equal("true", properties.First.Value.DefaultValue);     // "Switch should default to true"
            Assert.Equal(PropertyType.Boolean, properties.First.Value.Type);
        }
Exemplo n.º 8
0
        public void TestParseIncorrect_NoMatchingRule()
        {
            bool exceptionCaught = false;

            try
            {
                string     incorrectXmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                           <Rule Name=`TaskGeneratorLoadTest`>
                             <BoolProperty Name=`TestProperty1` Switch=`tp` />
                           </Rule>
                         </ProjectSchemaDefinitions>";
                TaskParser tp = XamlTestHelpers.LoadAndParse(incorrectXmlContents, "CL");
            }
            catch (XamlParseException)
            {
                exceptionCaught = true;
            }

            Assert.True(exceptionCaught); // "Should have caught a XamlParseException"
        }
Exemplo n.º 9
0
        public void TestBasicNonReversibleBooleanSwitch()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <BoolProperty Name=`KeepComments` Switch=`C` />
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.AreEqual(1, properties.Count, "Expected one property but there were " + properties.Count);
            Assert.IsNotNull(properties.First.Value, "KeepComments switch should exist");
            Assert.AreEqual("KeepComments", properties.First.Value.Name);
            Assert.AreEqual("C", properties.First.Value.SwitchName);
            Assert.IsNull(properties.First.Value.ReverseSwitchName, "KeepComments shouldn't have a reverse switch value");
            Assert.AreEqual(String.Empty, properties.First.Value.Reversible, "Switch should NOT marked as reversible");
            Assert.AreEqual(String.Empty, properties.First.Value.DefaultValue, "Switch should NOT have a default value");
            Assert.AreEqual(PropertyType.Boolean, properties.First.Value.Type);
        }
Exemplo n.º 10
0
        public void TestBasicEnumProperty()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <EnumProperty Name=`GeneratePreprocessedFile` Switch=`nologo`>
                                         <EnumValue Name=`Disabled` />
                                         <EnumValue Name=`Yes` Switch=`P` />
                                         <EnumValue Name=`NoLineNumbers` Switch=`EP` />
                                       </EnumProperty>
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.Single(properties);                                      // "Expected one property but there were " + properties.Count
            Assert.NotNull(properties.First.Value);                         // "GeneratePreprocessedFile switch should exist"
            Assert.Equal("GeneratePreprocessedFile", properties.First.Value.Name);
            Assert.Equal(PropertyType.String, properties.First.Value.Type); // Enum properties are represented as string types
            Assert.Equal(3, properties.First.Value.Values.Count);           // "GeneratePreprocessedFile should have three values"
        }
Exemplo n.º 11
0
        public void TestStringArrayPropertyWithDataSource_DataSourceIsItem()
        {
            string     xmlContents = @"<ProjectSchemaDefinitions xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework` xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml` xmlns:sys=`clr-namespace:System;assembly=mscorlib` xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
                                     <Rule Name=`CL`>
                                       <StringListProperty Name=`TargetAssembly` Switch=`/target:&quot;[value]&quot;` Separator=`;`>
                                         <StringListProperty.DataSource>
                                           <DataSource SourceType=`Item` Persistence=`ProjectFile` ItemType=`AssemblyName` />
                                         </StringListProperty.DataSource>
                                       </StringListProperty>
                                     </Rule>
                                   </ProjectSchemaDefinitions>";
            TaskParser tp          = XamlTestHelpers.LoadAndParse(xmlContents, "CL");

            LinkedList <Property> properties = tp.Properties;

            Assert.Single(properties);                                         // "Expected one property but there were " + properties.Count
            Assert.NotNull(properties.First.Value);                            // "TargetAssembly switch should exist"
            Assert.Equal("TargetAssembly", properties.First.Value.Name);
            Assert.Equal(PropertyType.ItemArray, properties.First.Value.Type); // Although it's a String array property, DataSource.SourceType overrides that
            Assert.Equal("/target:\"[value]\"", properties.First.Value.SwitchName);
            Assert.Equal(";", properties.First.Value.Separator);
        }