示例#1
0
        static bool Test(string query, double xmin, double xmax, CompareAction compare)
        {
            var parser = Parser.Parse(query);

            Console.WriteLine("Testing: {0} = ...", query);
            var x       = xmin;
            var success = 0;
            var total   = 0;

            while (x < xmax)
            {
                total++;
                var value = parser.Execute(new { x });

                if (value.Equals(compare(x)))
                {
                    success++;
                }

                x += 0.1;
            }

            Console.WriteLine("with x = {2}..{3} ;\n{0}\n-> correct: {1}", success, total, xmin, xmax);
            return(Assert(success, total));
        }
        private void AddOrRemoveFile(string path1, string path2, CompareAction action)
        {
            var path1Files = Directory.GetFiles(path1).ToList();
            foreach (var path1File in path1Files)
            {
                var componentName = System.IO.Path.GetDirectoryName(path1File).Split(System.IO.Path.DirectorySeparatorChar).LastOrDefault();
                var fileName = System.IO.Path.GetFileName(path1File);

                var path2File = Directory.GetFiles(path2, System.IO.Path.GetFileName(path1File)).FirstOrDefault();
                var actionParsed = string.Format("{0} config file", action.ToString());

                if (path2File == null)
                {
                    var xmlDiff = new XmlDifferences
                    {
                        Action = actionParsed,
                        Component = componentName,
                        Config = fileName,
                        Sprint = txbSprint.Text
                    };

                    if (action == CompareAction.add)
                    {
                        //todo: check if file is a valid xml
                        xmlDiff.XmlExample = XDocument.Load(path1File).ToString();
                    }

                    Diffs.Add(xmlDiff);

                    if (action == CompareAction.add)
                    {
                        AllKeys(path1File, action, componentName, fileName);
                    }
                }
            }
        }
        private void CompareFolderStructure(string path1, string path2, CompareAction action)
        {
            var path1Directories = Directory.GetDirectories(path1).ToList();
            var path2Directories = Directory.GetDirectories(path2).ToList();

            foreach (var path1Dir in path1Directories)
            {
                var path1Name = path1Dir.Split(System.IO.Path.DirectorySeparatorChar).LastOrDefault();

                var path2Dir = path2Directories.FirstOrDefault(i => i.EndsWith(path1Name));
                var path2FolderNames = path2Directories.Select(i => i.Split(System.IO.Path.DirectorySeparatorChar).LastOrDefault()).ToList();
                var path2Name = path2FolderNames.FirstOrDefault(i => i == path1Name);

                if (path2Name == null)
                {
                    var parsedAction = string.Format("{0} folder", action.ToString());

                    Diffs.Add(new XmlDifferences
                    {
                        Action = parsedAction,
                        Component = path1Name,
                        Sprint = Sprint
                    });

                    if (action == CompareAction.add)
                    {
                        AddAllFiles(path1Dir);
                    }
                }
                else
                {
                    AddOrRemoveFile(path1Dir, path2Dir, action);
                }
            }
        }
        private void Keys(XElement element, CompareAction action, string component, string fileName, bool includeAllLevels = true)
        {
            var ancestors = element.AncestorsAndSelf().Select(i => i.Name.LocalName).Reverse().ToList();
            var xpath = string.Concat("\\\\", string.Join("\\", ancestors));

            var parsedAction = element.HasElements ? string.Format("{0} section", action.ToString()) : string.Format("{0} key", action.ToString());

            Diffs.Add(new XmlDifferences
            {
                Action = parsedAction,
                Component = component,
                Config = fileName,
                Sprint = Sprint,
                XPath = xpath
            });

            if (element.HasElements && includeAllLevels)
            {
                AllKeys(element.Elements().ToList(), action, component, fileName, includeAllLevels);
            }
        }
 private void AllKeys(List<XElement> elements, CompareAction action, string component, string fileName, bool includeAllLevels = true)
 {
     if (elements.Count > 0)
     {
         foreach (var element in elements)
         {
             Keys(element, action, component, fileName, includeAllLevels);
         }
     }
 }
        private void AllKeys(string xmlPath, CompareAction action, string component, string fileName, bool includeAllLevels = true)
        {
            //todo: check if file is a valid xml
            var xml = !string.IsNullOrWhiteSpace(xmlPath) ? XDocument.Load(xmlPath) : new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));
            var xmlElements = xml.Root != null ? xml.Root.Elements().ToList() : new List<XElement>(0);

            if (xmlElements.Count > 0)
            {
                foreach (var element in xmlElements)
                {
                    Keys(element, action, component, fileName, includeAllLevels);
                }
            }
        }
        private void CompareKeys(List<XElement> xmlOldElements, List<XElement> xmlNewElements, CompareAction action, string component, string fileName, bool includeAllLevels = false)
        {
            foreach (var oldElement in xmlOldElements)
            {
                var xmlNewElement = (from xmlNew in xmlNewElements
                                     where xmlNew.Name.LocalName == oldElement.Name.LocalName
                                     select xmlNew).FirstOrDefault();

                if (xmlNewElement == null)
                {
                    Keys(oldElement, action, component, fileName, includeAllLevels);
                }
                else
                {
                    CompareKeys(oldElement.Elements().ToList(), xmlNewElement.Elements().ToList(), action, component, fileName, includeAllLevels);
                }
            }
        }