Ejemplo n.º 1
0
        public bool Optimize()
        {
            Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

            mErrorsList.ClearAll();
            ShowErros(false);

            ColorIndex = 0;

            mGherkinScenarioDefinition.ClearAll();
            mGherkinSteps.ClearAll();
            mTags.ClearAll();

            mOptimizedSteps.ClearAll();
            OptimizedStepsGrid.DataSourceList = mOptimizedSteps;

            var parser = new Parser();

            try
            {
                string     txt             = GherkinTextEditor.GetText();
                TextReader sr              = new StringReader(txt);
                var        gherkinDocument = parser.Parse(sr);

                if (gherkinDocument.Feature == null || gherkinDocument.Feature.Children == null)
                {
                    Mouse.OverrideCursor = null;
                    return(false);
                }


                foreach (ScenarioDefinition SD in gherkinDocument.Feature.Children)
                {
                    mGherkinScenarioDefinition.Add(new GherkinScenarioDefinition(SD));
                }


                FeatureName = System.IO.Path.GetFileName(GherkinTextEditor.FileName).Replace(".feature", "");
                foreach (var c in gherkinDocument.Comments)
                {
                    WriteTXT("Comments:" + c.Text);
                }

                foreach (Tag t in gherkinDocument.Feature.Tags)
                {
                    mTags.Add(new GherkinTag(t));
                }

                mSolTags.ClearAll();
                foreach (GherkinTag tag in mTags)
                {
                    Guid guid = GetTagInSolution(tag.Name.Substring(1));
                    if (guid != Guid.Empty && !mSolTags.Contains(guid))
                    {
                        mSolTags.Add(guid);
                    }
                }
                foreach (var c in gherkinDocument.Feature.Children)
                {
                    if (c.Keyword == "Scenario" || c.Keyword == "Scenario Outline")
                    {
                        WriteTXT("Keyword:" + c.Keyword);
                        WriteTXT("Name:" + c.Name);

                        if (c is Scenario)
                        {
                            foreach (Tag t in ((Gherkin.Ast.Scenario)c).Tags)
                            {
                                mTags.Add(new GherkinTag(t));
                                Guid guid = GetTagInSolution(t.Name.Substring(1));
                                if (guid != Guid.Empty && !mSolTags.Contains(guid))
                                {
                                    mSolTags.Add(guid);
                                }
                            }
                        }

                        if (c is ScenarioOutline)
                        {
                            foreach (Tag t in ((Gherkin.Ast.ScenarioOutline)c).Tags)
                            {
                                mTags.Add(new GherkinTag(t));
                                Guid guid = GetTagInSolution(t.Name.Substring(1));
                                if (guid != Guid.Empty && !mSolTags.Contains(guid))
                                {
                                    mSolTags.Add(guid);
                                }
                            }
                        }
                    }
                }
                foreach (var c in gherkinDocument.Feature.Children)
                {
                    if (c.Keyword == "Scenario" || c.Keyword == "Scenario Outline")
                    {
                        foreach (var step in c.Steps)
                        {
                            WriteTXT("Keyword:" + step.Keyword);
                            WriteTXT("Text:" + step.Text);

                            GherkinStep GS = new GherkinStep();
                            GS.Text = step.Text;
                            GS.Step = step;
                            mGherkinSteps.Add(GS);

                            String GherkingActivityName = GherkinGeneral.GetActivityGherkinName(step.Text);

                            GherkinStep OptimizedStep = SearchStepInOptimizedSteps(GherkingActivityName);
                            if (OptimizedStep == null)
                            {
                                GherkinStep NewOptimizedStep = new GherkinStep();
                                NewOptimizedStep.Text       = GherkingActivityName;
                                NewOptimizedStep.Counter    = 1;
                                NewOptimizedStep.ColorIndex = ColorIndex;
                                ColorIndex++;
                                NewOptimizedStep.AutomationStatus = GetStepAutomationStatus(GherkingActivityName);
                                GS.AutomationStatus = NewOptimizedStep.AutomationStatus;
                                mOptimizedSteps.Add(NewOptimizedStep);
                            }
                            else
                            {
                                OptimizedStep.Counter++;
                                GS.ColorIndex       = OptimizedStep.ColorIndex;
                                GS.AutomationStatus = OptimizedStep.AutomationStatus;
                            }
                        }
                    }
                }

                // Warnings - TODO check other possible warnings
                // Check Dups Scenario names
                var query = mGherkinScenarioDefinition.GroupBy(x => x.Name)
                            .Where(g => g.Count() > 1)
                            .Select(y => y.Key)
                            .ToList();
                foreach (var v in query)
                {
                    IEnumerable <GherkinScenarioDefinition> SCS = from x in mGherkinScenarioDefinition where x.Name == v select x;
                    foreach (GherkinScenarioDefinition sc in SCS)
                    {
                        GherkinParserException PE = new GherkinParserException(sc.ScenarioDefintion.Location.Line, sc.ScenarioDefintion.Location.Column, "Duplicate Scenario Name: " + sc.Name);
                        mErrorsList.Add(PE);
                    }
                }
            }
            catch (CompositeParserException ex)
            {
                // we show the errors in the grid + mark in the textEditor
                GherkinTextEditor.BackgroundRenderer.Segments.Clear();
                foreach (ParserException PE in ex.Errors)
                {
                    mErrorsList.Add(new GherkinParserException(PE));
                    if (PE.Location.Line > GherkinTextEditor.textEditor.Document.LineCount)
                    {
                        continue;
                    }
                    var line = GherkinTextEditor.textEditor.Document.GetLineByNumber(PE.Location.Line);
                    GherkinTextEditor.BackgroundRenderer.Segments.Add(line);
                }
            }

            if (mErrorsList.Count > 0)
            {
                ShowErros(true);
                GherkinTextEditor.Focus();
            }
            else
            {
                GherkinTextEditor.BackgroundRenderer.Segments.Clear();
            }


            ARP.xActivitiesRepositoryGrid.Tags = mSolTags;
            SharedActivitiesFrame.Content      = ARP;

            foreach (GherkinStep gStep in mOptimizedSteps)
            {
            }

            OptimizedStepsGrid.DataSourceList = mOptimizedSteps;
            Mouse.OverrideCursor = null;
            List <GherkinParserException> Errors = mErrorsList.Where(x => x.ErrorType == GherkinParserException.eErrorType.Error).ToList();
            bool result = mOptimizedSteps.Count > 0 && Errors.Count == 0;

            return(result);
        }
Ejemplo n.º 2
0
        private ActivitiesGroup CreateScenario(ScenarioDefinition sc, int?index = null)
        {
            // Create new activity per each step
            // Each scenario is in one ActivitiesGroup
            ActivitiesGroup AG = new ActivitiesGroup();

            AG.Name = sc.Name;
            if (index != null)
            {
                AG.Name += " #" + index;                 // Adding Scenario index so it will be unique
            }
            mBizFlow.AddActivitiesGroup(AG);

            //Add Tags - on ActivitiesGroup and solution if needed

            IEnumerable <Tag> TagList = null;

            if (sc is ScenarioOutline)
            {
                TagList = ((ScenarioOutline)sc).Tags;
            }
            else if (sc is Scenario)
            {
                TagList = ((Scenario)sc).Tags;
            }

            foreach (var t in TagList)
            {
                Guid tg = GetOrCreateTagInSolution(t.Name);
                AG.Tags.Add(tg);
            }
            foreach (Guid guid in mBizFlow.Tags)
            {
                AG.Tags.Add(guid);
            }

            foreach (Gherkin.Ast.Step step in sc.Steps)
            {
                // Find the Activity from the template BF with All activity, create a copy and add to BF
                string   GN = GherkinGeneral.GetActivityGherkinName(step.Text);
                Activity a  = (Activity)SearchActivityByName(GN);
                if (a != null)
                {
                    Activity a1 = (Activity)a.CreateCopy(false);
                    a1.ActivityName = step.Text;
                    a1.Active       = true;
                    a1.ParentGuid   = a1.Guid;
                    Guid g = Guid.NewGuid();
                    a1.Guid = g;
                    foreach (var t in AG.Tags)
                    {
                        if (!a1.Tags.Contains(t))
                        {
                            a1.Tags.Add(t);
                        }
                        if (!a.Tags.Contains(t))
                        {
                            a.Tags.Add(t);
                        }
                    }
                    //Adding feature tags
                    foreach (Guid guid in mBizFlow.Tags)
                    {
                        if (!a1.Tags.Contains(guid))
                        {
                            a1.Tags.Add(guid);
                        }
                        if (!a.Tags.Contains(guid))
                        {
                            a.Tags.Add(guid);
                        }
                    }

                    UpdateActivityVars(a1, a.ActivityName);
                    mBizFlow.AddActivity(a1, AG);
                }
                else
                {
                    //TODO: err activity not found...
                    Reporter.ToUser(eUserMsgKey.GherkinActivityNotFound, GN);
                }
            }
            return(AG);
        }