コード例 #1
0
        public void ParseInvalidQueries()
        {
            var query1 = "wts.frontendframework = framework & wts.type != Page&$name == Map";

            Assert.Throws <InvalidCompositionQueryException>(() =>
            {
                var result = CompositionQuery.Parse(query1);
            });

            var query2 = "wts.frontendframework= framework &wts.type ! Page&$name != Map";

            Assert.Throws <InvalidCompositionQueryException>(() =>
            {
                var result = CompositionQuery.Parse(query2);
            });

            var query3 = "wts.frontendframework==framework&wts.type!Page&$name == Map";

            Assert.Throws <InvalidCompositionQueryException>(() =>
            {
                var result = CompositionQuery.Parse(query3);
            });

            var query4 = "wts.frontendframework   == framework & wts.type=Page& $name == Map";

            Assert.Throws <InvalidCompositionQueryException>(() =>
            {
                var result = CompositionQuery.Parse(query4);
            });
        }
コード例 #2
0
        public void Parse_MultipleItems()
        {
            var query = new string[]
            {
                "wts.frontendframework == framework",
                "wts.type != Page",
                "$name == Map",
            };

            var result = CompositionQuery.Parse(query);

            Assert.Collection(
                result.Items,
                r1 =>
            {
                Assert.Equal("wts.frontendframework", r1.Field);
                Assert.Equal(QueryOperator.Equals, r1.Operator);
                Assert.Equal("framework", r1.Value);
                Assert.False(r1.IsContext);
            },
                r2 =>
            {
                Assert.Equal("wts.type", r2.Field);
                Assert.Equal(QueryOperator.NotEquals, r2.Operator);
                Assert.Equal("Page", r2.Value);
                Assert.False(r2.IsContext);
            },
                r3 =>
            {
                Assert.Equal("name", r3.Field);
                Assert.Equal(QueryOperator.Equals, r3.Operator);
                Assert.Equal("Map", r3.Value);
                Assert.True(r3.IsContext);
            });
        }
コード例 #3
0
        public void Parse()
        {
            var query  = "wts.framework == framework & wts.type != Page&$name == Map";
            var result = CompositionQuery.Parse(query);

            Assert.Collection(result.Items,
                              r1 =>
            {
                Assert.Equal(r1.Field, "wts.framework");
                Assert.Equal(r1.Operator, QueryOperator.Equals);
                Assert.Equal(r1.Value, "framework");
                Assert.False(r1.IsContext);
            },
                              r2 =>
            {
                Assert.Equal(r2.Field, "wts.type");
                Assert.Equal(r2.Operator, QueryOperator.NotEquals);
                Assert.Equal(r2.Value, "Page");
                Assert.False(r2.IsContext);
            },
                              r3 =>
            {
                Assert.Equal(r3.Field, "name");
                Assert.Equal(r3.Operator, QueryOperator.Equals);
                Assert.Equal(r3.Value, "Map");
                Assert.True(r3.IsContext);
            });
        }
コード例 #4
0
        public void Match_MultiValueBoth()
        {
            var data   = GetFactData();
            var target = CompositionQuery.Parse("identity==item-identity&tag3==tag3Val1|tag3Val3");
            var result = target.Match(data, null);

            Assert.True(result);
        }
コード例 #5
0
        public void Match_NotEquals()
        {
            var data   = GetFactData();
            var target = CompositionQuery.Parse("identity==item-identity&tag2!=tagVal1");
            var result = target.Match(data, null);

            Assert.True(result);
        }
コード例 #6
0
        public void Parse_NoValueInParam()
        {
            var query = "wts.frontendframework == framework & wts.type";

            Assert.Throws <InvalidCompositionQueryException>(() =>
            {
                var result = CompositionQuery.Parse(query);
            });
        }
コード例 #7
0
 private static IEnumerable <CompositionInfo> GetCompositionCatalog(string platform)
 {
     return(GenContext.ToolBox.Repo
            .Get(t => t.GetTemplateType() == TemplateType.Composition && t.GetPlatform() == platform)
            .Select(t => new CompositionInfo()
     {
         Query = CompositionQuery.Parse(t.GetCompositionFilter()), Template = t
     })
            .ToList());
 }
コード例 #8
0
 private static void VerifyWtsCompositionFilterTagValue(KeyValuePair <string, string> tag, List <string> results)
 {
     try
     {
         CompositionQuery.Parse(tag.Value);
     }
     catch (InvalidCompositionQueryException ex)
     {
         results.Add($"Unable to parse the wts.compositionFilter value of '{tag.Value}': {ex}.");
     }
 }
コード例 #9
0
        public void Match_WithContext_NotEquals_Empty()
        {
            var data    = GetFactData();
            var target  = CompositionQuery.Parse("identity==item-identity&tag2==tagVal2&$name!=context-name");
            var context = new QueryablePropertyDictionary {
                new QueryableProperty("name", "")
            };

            var result = target.Match(data, context);

            Assert.True(result);
        }
コード例 #10
0
        public void NoMatch_WithContext()
        {
            var data    = GetFactData();
            var target  = CompositionQuery.Parse("identity!=item-identity&tag2==tagVal2&$name==context-name");
            var context = new QueryablePropertyDictionary {
                new QueryableProperty("name", "context-name")
            };

            var result = target.Match(data, context);

            Assert.False(result);
        }
コード例 #11
0
 private static void VerifyWtsCompositionfilterTagValue(KeyValuePair <string, string> tag, List <string> results)
 {
     try
     {
         // Use a linked copy of this (and related files) as can't reference the core lib directly
         CompositionQuery.Parse(tag.Value);
     }
     catch (InvalidCompositionQueryException ex)
     {
         results.Add($"Unable to parse the wts.compositionFilter value of '{tag.Value}': {ex}.");
     }
 }
コード例 #12
0
        public void EnsureVisualBasicTemplatesHaveEquivalentPrimaryOutputsAndFilters()
        {
            var errors       = new List <string>();
            var allTemplates = TemplateJsonValidationTests.GetAllTemplateJsonFiles();

            try
            {
                foreach (var template in allTemplates)
                {
                    if (template[0].ToString().Contains("._VB"))
                    {
                        var vbFileContents = File.ReadAllText(template[0].ToString());
                        var vbTemplate     = JsonConvert.DeserializeObject <ValidationTemplateInfo>(vbFileContents);

                        var csFileContents = File.ReadAllText(template[0].ToString().Replace("._VB", string.Empty));
                        var csTemplate     = JsonConvert.DeserializeObject <ValidationTemplateInfo>(csFileContents);

                        if (vbTemplate.PrimaryOutputs != null)
                        {
                            if (vbTemplate.PrimaryOutputs.Count != csTemplate.PrimaryOutputs.Count)
                            {
                                errors.Add($"{template[0].ToString()} should have {csTemplate.PrimaryOutputs.Count} primary outputs.");
                            }
                        }

                        if (csTemplate.TemplateTags.ContainsKey("wts.compositionFilter"))
                        {
                            var vbFilter = vbTemplate.TemplateTags["wts.compositionFilter"];
                            var csFilter = csTemplate.TemplateTags["wts.compositionFilter"];

                            var vbquery = CompositionQuery.Parse(vbFilter);
                            var csquery = CompositionQuery.Parse(csFilter);
                            if (vbquery.Items.Count != csquery.Items.Count)
                            {
                                errors.Add($"{template[0].ToString()}: {vbFilter} should have contained {csquery.Items.Count} items.");
                            }
                            else
                            {
                                foreach (var vbItem in vbquery.Items)
                                {
                                    var csItem = csquery.Items.First(n => n.Field == vbItem.Field);

                                    if (csItem.Value.Split('|').Count() != vbItem.Value.Split('|').Count())
                                    {
                                        if (!csItem.Value.Contains("Prism") && !csItem.Value.Contains("Caliburn"))
                                        {
                                            errors.Add($"{template[0].ToString()}: check {vbItem.Field}.");
                                        }
                                    }
                                }
                            }
                        }

                        var csCompOrder = csTemplate.TemplateTags.ContainsKey("wts.compositionOrder") ? csTemplate.TemplateTags["wts.compositionOrder"] : null;
                        var vbCompOrder = vbTemplate.TemplateTags.ContainsKey("wts.compositionOrder") ? vbTemplate.TemplateTags["wts.compositionOrder"] : null;

                        if (csCompOrder != vbCompOrder)
                        {
                            errors.Add($"CompostionOrder issue in {template[0].ToString()}");
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine(exc);
            }

            Assert.True(!errors.Any(), string.Join(Environment.NewLine, errors));
        }
コード例 #13
0
        public static VerifierResult VerifyTemplateFolders(bool showWarnings, params string[] templateFolders)
        {
            var results = new List <string>();

            if (templateFolders == null)
            {
                results.Add("Paths to template folders not provided.");
            }
            else
            {
                results.AddRange(from templateFolder
                                 in templateFolders
                                 where !Directory.Exists(templateFolder)
                                 select $"Folder '{templateFolder}' does not exist.");
            }

            // Don't bother with actual tests if not all folders are valid
            if (!results.Any())
            {
                var allTemplateFilePaths = new List <string>();

                foreach (var rootFolder in templateFolders)
                {
                    allTemplateFilePaths.AddRange(new DirectoryInfo(rootFolder).GetFiles("template.json", SearchOption.AllDirectories)
                                                  .Select(file => file.FullName));
                }

                var allIdentities   = new Dictionary <string, string>(); // identity, filepath
                var allDependencies = new Dictionary <string, string>(); // filepath, dependency
                var allFileHashes   = new Dictionary <string, string>(); // filehash, filepath
                var allCompFilters  = new Dictionary <string, string>(); // filepath, filter

                foreach (var templateFilePath in allTemplateFilePaths)
                {
                    var fileContents = File.ReadAllText(templateFilePath);

                    var template = JsonConvert.DeserializeObject <ValidationTemplateInfo>(fileContents);

                    if (template.Identity != null)
                    {
                        if (allIdentities.ContainsKey(template.Identity))
                        {
                            results.Add($"Duplicate Identity detected in: '{templateFilePath}' & '{allIdentities[template.Identity]}'");
                        }
                        else
                        {
                            allIdentities.Add(template.Identity, templateFilePath);
                        }

                        // Check that localized files have the same identity
                        foreach (var localizedFile in new DirectoryInfo(Path.GetDirectoryName(templateFilePath)).EnumerateFiles("*.template.json"))
                        {
                            var localizedContents = File.ReadAllText(localizedFile.FullName);
                            var localizedTemplate = JsonConvert.DeserializeObject <ValidationTemplateInfo>(localizedContents);

                            if (template.Identity != localizedTemplate.Identity)
                            {
                                results.Add($"'{localizedFile.FullName}' does not have the correct identity.");
                            }
                        }
                    }
                    else
                    {
                        if (allIdentities.ContainsKey(template.Name))
                        {
                            results.Add($"Duplicate Identity detected in: '{templateFilePath}' & '{allIdentities[template.Name]}'");
                        }
                        else
                        {
                            allIdentities.Add(template.Name, templateFilePath);
                        }
                    }

                    // Get list of dependencies while the file is open. These are all checked later
                    if (template.TemplateTags.ContainsKey("wts.dependencies"))
                    {
                        allDependencies.Add(templateFilePath, template.TemplateTags["wts.dependencies"]);
                    }

                    // Get list of filters while the file is open. These are all checked later
                    if (template.TemplateTags.ContainsKey("wts.compositionFilter"))
                    {
                        allCompFilters.Add(templateFilePath, template.TemplateTags["wts.compositionFilter"]);
                    }

                    var templateRoot = templateFilePath.Replace("\\.template.config\\template.json", string.Empty);

                    foreach (var file in new DirectoryInfo(templateRoot).GetFiles("*.*", SearchOption.AllDirectories))
                    {
                        // Filter out files the following tests cannot handle
                        if (!file.Name.Contains("_postaction") &&
                            !file.Name.Contains("_gpostaction") &&
                            !file.FullName.Contains("\\Projects\\Default") &&
                            !file.FullName.Contains(".template.config"))
                        {
                            // Use of FileInfo and Path to handle comparison of relative and exact paths
                            if (template.PrimaryOutputs.All(p => file.FullName != new FileInfo(Path.Combine(templateRoot, p.Path)).FullName))
                            {
                                results.Add($"'{file.FullName}' is not used in the template.");
                            }

                            // Duplicate file checking can be avoided as some duplicate files exist in the official templates at the time of writing.
                            // It is done by default to encourage anyone creating new templates to follow this guidance.
                            if (showWarnings)
                            {
                                // Ignore xaml files as we know these are duplicated across VB & C# versions of the same template
                                if (file.Extension != ".xaml")
                                {
                                    var hash = GetFileHash(file.FullName);

                                    // if hash is already in the dictionary then write to results as a duplicate file
                                    // if not add to the dictionary
                                    if (allFileHashes.ContainsKey(hash))
                                    {
                                        results.Add($"WARNING: '{file.FullName}' and '{allFileHashes[hash]}' have identical contents and could be combined into a single template.");
                                    }
                                    else
                                    {
                                        allFileHashes.Add(hash, file.FullName);
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (var dependencies in allDependencies)
                {
                    foreach (var dependency in dependencies.Value.Split('|'))
                    {
                        if (!allIdentities.ContainsKey(dependency))
                        {
                            results.Add($"'{dependencies.Key}' contains dependency '{dependency}' that does not exist.");
                        }
                    }
                }

                foreach (var compFilter in allCompFilters)
                {
                    var query = CompositionQuery.Parse(compFilter.Value);

                    foreach (var queryItem in query.Items)
                    {
                        if (queryItem.Field == "identity")
                        {
                            foreach (var templateIdentity in queryItem.Value.Split('|'))
                            {
                                if (!allIdentities.Keys.Contains(templateIdentity))
                                {
                                    results.Add($"'{compFilter.Key}' contains composition filter identity '{templateIdentity}' that does not exist.");
                                }
                            }
                        }
                    }
                }
            }

            var success = results.Count == 0;

            if (success)
            {
                results.Add("All looks good.");
            }

            return(new VerifierResult(success, results));
        }