private void AddResult(PatternComplex r)
        {
            var p = new PatternTestResult
            {
                Name          = r.Name,
                PatternName   = r.Name,
                Path          = _path,
                StartiLine    = r.Events.Any() ? r.Events.First(n => n.Found).iLine : 0,
                EndiLine      = r.Events.Any() ? r.Events.Last(n => n.Found).iLine : 0,
                Warnings      = r.Warnings,
                TotalDuration = r.TotalDuration,
                Options       = r.Options.Clone()
            };

            r.Events = r.Events.OrderBy(n => n.iLine).ToList();

            foreach (var e in r.Events.Where(n => n.Found))
            {
                e.Path = _path;

                #region Process Search
                if (e.EventType == EventType.Search)
                {
                    _result.Log.Add($@"{DateTime.Now.ToShortTimeString()} Pattern Search EventPattern ""{e.EventTag}"" found {e.iLineSearchMatches.Count} matches.");
                    foreach (var i in e.iLineSearchMatches)
                    {
                        var res = new PatternTestResultEventPattern(e, i.iLine);
                        e.iLine = i.iLine;
                        e.Line  = i.Line;
                        ProcessValues(e, res);
                        p.Events.Add(res);
                    }
                    continue;
                }
                #endregion Process Search

                var re = new PatternTestResultEventPattern(e, e.iLine);
                ProcessDuration(r.Events, p.Events, e, re);
                ProcessValues(e, re);
                p.Events.Add(re);

                // TODO: What is this for, should it be moved to a Summary of Totals section? - Sara
                #region Process Stop
                if (e.BodyStopFound &&
                    e.DateTime.HasValue &&
                    e.DateTimeBodyStop.HasValue)
                {
                    var _duration = e.DateTimeBodyStop.Value - e.DateTime;
                    var b         = new PatternTestResultEventPattern($"{_duration.Value.ToFixedTimeString()}",
                                                                      EventType.Duration,
                                                                      e.Path,
                                                                      e.iLineBodyStop,
                                                                      0,
                                                                      null,
                                                                      e.DateTimeBodyStop,
                                                                      _duration.Value);
                    p.Events.Add(b);
                }
                #endregion Process Stop
            }


            #region Add Processed Pattern to List
            p.Events = p.Events.OrderBy(n => n.StartiLine).ToList();
            var _summary = _result.PatternSummaries.Where(n => n.PatternName == p.PatternName).FirstOrDefault();
            if (_summary == null)
            {
                _summary = new PatternTestSummary(p.PatternName);
                _result.PatternSummaries.Add(_summary);
            }

            _summary.Patterns.Add(p);
            #endregion
        }
Пример #2
0
        /// <summary>
        /// Using the Abstract Syntax Tree, this method will create concreate Pattern's
        /// </summary>
        private static bool AbstractSyntaxTreeToPatterns(TestArgs args)
        {
            var noErrors = true;

            foreach (PatternDeclaration pd in args.AbstractSyntaxTree.Children.Where(n => n.Kind == SyntaxKind.PatternDeclaration))
            {
                // Default
                ScanType scanType = ScanType.FirstOccurance;
                if (pd.Options.FirstOrDefault(n => n.Value == Keyword._Repeating) != null)
                {
                    scanType = ScanType.Repeating;
                }

                var pc = new PatternComplex(pd.Name, scanType);

                foreach (var source in pd.Sources)
                {
                    pc.Sources.Add(source.Value);
                }

                foreach (var option in pd.Options)
                {
                    switch (option.Name)
                    {
                    case Keyword._TotalTime:
                        pc.Options.TotalTimeOption = true;
                        break;

                    case Keyword._KnownIdle:
                        pc.Options.KnownIdle           = true;
                        pc.Options.KnownIdleParameters = option.Parameters;
                        break;

                    case Keyword._UnknownIdle:
                        pc.Options.UnknownIdle           = true;
                        pc.Options.UnknownIdleParameters = option.Parameters;
                        break;

                    case Keyword._IdleTime:
                        pc.Options.IdleTimeOption = true;
                        break;

                    case Keyword._HidePattern:
                        pc.Options.HidePattern = true;
                        break;

                    case Keyword._HideFilePath:
                        pc.Options.HideFilePath = true;
                        break;

                    case Keyword._Unexpected:
                        pc.Options.PatternUnexpected = true;
                        break;

                    default:
                        break;
                    }
                }

                foreach (var item in pd.Body.Contents)
                {
                    try
                    {
                        var e = item as EventStatement;

                        if (e == null)
                        {
                            var temp = DateTime.Now.ToShortTimeString();
                            throw new OptionException($@"{temp} {PATTERN_SYNTAX_ERROR}
{new string(' ', temp.Length)} Unexpected '{item.GetType()}'.Expected 'EventStatement'", item.Span);
                        }

                        var regularExpression = LookupEvent(e.Event, e.Span);

                        EventOption option = ParseOption(e);

                        var regularExpressionBodyStop = string.Empty;

                        if (option.BodyStop)
                        {
                            regularExpressionBodyStop = LookupEvent(option.BodyStopParameter, e.Span);
                        }

                        pc.AddEvent(new EventPattern(ConvertEventType(e.EventType), e.Event, regularExpression, regularExpressionBodyStop, option));
                    }
                    catch (OptionException ex)
                    {
                        var e = new ErrorEntry(ex.Message, args.SourceCode.GetLines(ex.Span.Start.Line, ex.Span.End.Line), Severity.Fatal, ex.Span);
                        args.Result.Log.AddRange(WriteError(e));
                        noErrors = false;
                    }
                }
                args.Patterns.Add(pc);
            }
            return(noErrors);
        }