Ejemplo n.º 1
0
        public void Match_PatternExpressionsInCalls(string patternData, params int[] matchMethodNumbers)
        {
            var     processor   = new DslProcessor();
            var     patternNode = (PatternNode)processor.Deserialize(patternData, LanguageExt.AllPatternLanguages);
            Pattern pattern     = new Pattern
            {
                Data      = patternNode,
                DebugInfo = patternData
            };

            patternsRep.Add(patternsConverter.ConvertBack(new List <Pattern>()
            {
                pattern
            }));
            WorkflowResult workflowResult = workflow.Process();

            MatchingResultDto[] matchingResults = workflowResult.MatchingResults.ToDto(workflow.SourceCodeRepository);
            patternsRep.Clear();

            Assert.AreEqual(matchMethodNumbers.Contains(0) ? 1 : 0, matchingResults.Count(r => r.MatchedCode.StartsWith("test_call_0")));
            Assert.AreEqual(matchMethodNumbers.Contains(1) ? 1 : 0, matchingResults.Count(r => r.MatchedCode.StartsWith("test_call_1")));
            Assert.AreEqual(matchMethodNumbers.Contains(2) ? 1 : 0, matchingResults.Count(r => r.MatchedCode.StartsWith("test_call_2")));
            Assert.AreEqual(matchMethodNumbers.Contains(3) ? 1 : 0, matchingResults.Count(r => r.MatchedCode.StartsWith("test_call_3")));
            Assert.AreEqual(matchMethodNumbers.Contains(4) ? 1 : 0, matchingResults.Count(r => r.MatchedCode.StartsWith("test_call_4")));
        }
Ejemplo n.º 2
0
        public static MatchingResultDto[] GetMatchings(string code, string pattern, LanguageFlags analyzedLanguages,
                                                       LanguageFlags?patternLanguages = null)
        {
            var sourceCodeRep = new MemoryCodeRepository(code);
            var patternsRep   = new MemoryPatternsRepository();
            var workflow      = new Workflow(sourceCodeRep, analyzedLanguages, patternsRep)
            {
                Logger = new LoggerMessageCounter()
            };

            var processor   = new DslProcessor();
            var patternNode = (PatternNode)processor.Deserialize(pattern, patternLanguages ?? LanguageExt.AllPatternLanguages);
            var p           = new Pattern
            {
                Data      = patternNode,
                DebugInfo = pattern
            };
            var patternsConverter = new PatternConverter(
                new JsonUstNodeSerializer(typeof(UstNode), typeof(PatternVarDef)));

            patternsRep.Add(patternsConverter.ConvertBack(new List <Pattern>()
            {
                p
            }));
            WorkflowResult workflowResult = workflow.Process();

            MatchingResultDto[] matchingResults = workflowResult.MatchingResults.ToDto(workflow.SourceCodeRepository)
                                                  .OrderBy(r => r.PatternKey)
                                                  .ToArray();

            return(matchingResults);
        }
Ejemplo n.º 3
0
        public static MatchResultDto[] GetMatches(TextFile source, string pattern,
                                                  IEnumerable <Language> analyzedLanguages,
                                                  IEnumerable <Language> patternLanguages = null)
        {
            var sourceRep = new MemorySourceRepository(source.Data, source.FullName)
            {
                Languages = new HashSet <Language>(analyzedLanguages)
            };
            var patternsRep = new MemoryPatternsRepository();
            var logger      = new TestLogger();
            var workflow    = new Workflow(sourceRep, patternsRep)
            {
                Logger = logger
            };

            var         processor   = new DslProcessor();
            PatternRoot patternNode = processor.Deserialize(new TextFile(pattern)
            {
                PatternKey = pattern
            });

            patternNode.Languages = new HashSet <Language>(patternLanguages ?? LanguageUtils.PatternLanguages);
            patternNode.DebugInfo = pattern;
            var patternsConverter = new PatternConverter();

            patternsRep.Add(patternsConverter.ConvertBack(new List <PatternRoot> {
                patternNode
            }));
            workflow.Process();
            MatchResultDto[] matchResults = logger.Matches.ToDto()
                                            .OrderBy(r => r.PatternKey)
                                            .ToArray();

            return(matchResults);
        }
Ejemplo n.º 4
0
        //[TestCase("#(#*, <[~e]>, #*)", new[] { 0, 1, 3 })]
        public void Match_PatternExpressionsInCalls(string patternData, params int[] matchMethodNumbers)
        {
            var         processor   = new DslProcessor();
            PatternRoot patternNode = processor.Deserialize(new TextFile(patternData)
            {
                PatternKey = patternData
            });

            patternNode.DebugInfo = patternData;
            patternsRepository.Add(patternsConverter.ConvertBack(new List <PatternRoot> {
                patternNode
            }));
            var logger   = new TestLogger();
            var workflow = new Workflow(sourceRep, patternsRepository)
            {
                Logger = logger
            };

            workflow.Process();
            IEnumerable <MatchResultDto> matchResults = logger.Matches.ToDto();

            patternsRepository.Clear();

            Assert.AreEqual(matchMethodNumbers.Contains(0) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_0")));
            Assert.AreEqual(matchMethodNumbers.Contains(1) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_1")));
            Assert.AreEqual(matchMethodNumbers.Contains(2) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_2")));
            Assert.AreEqual(matchMethodNumbers.Contains(3) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_3")));
            Assert.AreEqual(matchMethodNumbers.Contains(4) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_4")));
        }
Ejemplo n.º 5
0
        //[TestCase("#(#*, <[~e]>, #*)", new[] { 0, 1, 3 })]
        public void Match_PatternExpressionsInCalls(string patternData, params int[] matchMethodNumbers)
        {
            var         processor   = new DslProcessor();
            PatternRoot patternNode = processor.Deserialize(new CodeFile(patternData)
            {
                IsPattern = true
            });

            patternNode.DebugInfo = patternData;
            patternsRepository.Add(patternsConverter.ConvertBack(new List <PatternRoot>()
            {
                patternNode
            }));
            WorkflowResult workflowResult             = workflow.Process();
            IEnumerable <MatchResultDto> matchResults = workflowResult.MatchResults.ToDto();

            patternsRepository.Clear();

            Assert.AreEqual(matchMethodNumbers.Contains(0) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_0")));
            Assert.AreEqual(matchMethodNumbers.Contains(1) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_1")));
            Assert.AreEqual(matchMethodNumbers.Contains(2) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_2")));
            Assert.AreEqual(matchMethodNumbers.Contains(3) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_3")));
            Assert.AreEqual(matchMethodNumbers.Contains(4) ? 1 : 0, matchResults.Count(r => r.MatchedCode.StartsWith("test_call_4")));
        }
Ejemplo n.º 6
0
        public static MatchResultDto[] GetMatches(string code, string pattern,
                                                  IEnumerable <Language> analyzedLanguages,
                                                  IEnumerable <Language> patternLanguages = null)
        {
            var sourceCodeRep = new MemoryCodeRepository(code)
            {
                Languages = new HashSet <Language>(analyzedLanguages)
            };
            var patternsRep = new MemoryPatternsRepository();
            var workflow    = new Workflow(sourceCodeRep, patternsRep)
            {
                Logger = new LoggerMessageCounter()
            };

            var         processor   = new DslProcessor();
            PatternRoot patternNode = processor.Deserialize(new CodeFile(pattern)
            {
                IsPattern = true
            });

            patternNode.Languages = new HashSet <Language>(patternLanguages ?? LanguageUtils.PatternLanguages.Values);
            patternNode.DebugInfo = pattern;
            var patternsConverter = new PatternConverter();

            patternsRep.Add(patternsConverter.ConvertBack(new List <PatternRoot>()
            {
                patternNode
            }));
            WorkflowResult workflowResult = workflow.Process();

            MatchResultDto[] matchResults = workflowResult.MatchResults.ToDto()
                                            .OrderBy(r => r.PatternKey)
                                            .ToArray();

            return(matchResults);
        }
Ejemplo n.º 7
0
        public static IPatternsRepository CreatePatternsRepository(string patternsString,
                                                                   IEnumerable <string> patternIds,
                                                                   ILogger logger)
        {
            IPatternsRepository patternsRepository;

            if (string.IsNullOrEmpty(patternsString) || patternsString == "default")
            {
                patternsRepository = new DefaultPatternRepository();
            }
            else if (patternsString.EqualsIgnoreCase("no"))
            {
                patternsRepository = DummyPatternsRepository.Instance;
            }
            else if (patternsString.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
            {
                patternsRepository = new JsonPatternsRepository(FileExt.ReadAllText(patternsString));
            }
            else
            {
                TextFile patternsFile;
                if (patternsString.EndsWith(".pattern", StringComparison.OrdinalIgnoreCase))
                {
                    patternsFile = new TextFile(FileExt.ReadAllText(patternsString))
                    {
                        PatternKey = patternsString,
                        Name       = patternsString
                    };
                }
                else
                {
                    patternsFile = new TextFile(patternsString);
                }

                var processor = new DslProcessor();
                if (logger != null)
                {
                    processor.Logger = logger;
                }

                PatternRoot patternRoot      = processor.Deserialize(patternsFile);
                var         patternConverter = new PatternConverter();
                if (logger != null)
                {
                    patternConverter.Logger = logger;
                }

                List <PatternDto> dtos = patternConverter.ConvertBack(new[] { patternRoot });

                var memoryPatternsRepository = new MemoryPatternsRepository();
                memoryPatternsRepository.Add(dtos);

                patternsRepository = memoryPatternsRepository;
            }

            if (logger != null)
            {
                patternsRepository.Logger = logger;
            }

            patternsRepository.Identifiers = patternIds as List <string> ?? patternIds?.ToList();

            return(patternsRepository);
        }