<commit_before>﻿// Copyright (c) 2007-2017 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Linq;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing.Drawables.StepButtons;
using osu.Framework.Threading;
using OpenTK;
using OpenTK.Graphics;

namespace osu.Framework.Testing
{
    public abstract class TestCase : Container
    {
        public virtual string Description => @"The base class for a test case";

        public FillFlowContainer<StepButton> StepsContainer;

        private Container content;

        protected override Container<Drawable> Content => content;

        protected TestCase()
        {
            Name = GetType().ReadableName().Substring(8); // Skip the "TestCase prefix

            RelativeSizeAxes = Axes.Both;
            Masking = true;
        }

        private const float steps_width = 180;
        private const float padding = 0;

        public virtual void Reset()
        {
            if (content == null)
            {
                InternalChildren = new Drawable[]
                {
                    new Box
                    {
                        Colour = new Color4(25, 25, 25, 255),
                        RelativeSizeAxes = Axes.Y,
                        Width = steps_width,
                    },
                    StepsContainer = new FillFlowContainer<StepButton>
                    {
                        Direction = FillDirection.Vertical,
                        Depth = float.MinValue,
                        Padding = new MarginPadding(5),
                        Spacing = new Vector2(5),
                        AutoSizeAxes = Axes.Y,
                        Width = steps_width,
                    },
                    new Container
                    {
                        Masking = true,
                        Padding = new MarginPadding
                        {
                            Left = steps_width + padding,
                            Right = padding,
                            Top = padding,
                            Bottom = padding,
                        },
                        RelativeSizeAxes = Axes.Both,
                        Children = new []
                        {
                            content = new Container
                            {
                                Masking = true,
                                RelativeSizeAxes = Axes.Both
                            }
                        }
                    },
                };
            }
            else
            {
                content.Clear();
                StepsContainer.Clear();
            }
        }

        private int actionIndex;
        private int actionRepetition;
        private ScheduledDelegate stepRunner;

        public void RunAllSteps(Action onCompletion = null)
        {
            stepRunner?.Cancel();

            actionIndex = -1;
            actionRepetition = 0;
            runNextStep(onCompletion);
        }

        private StepButton loadableStep => actionIndex >= 0 ? StepsContainer.Children.Skip(actionIndex).FirstOrDefault() : null;

        protected virtual double TimePerAction => 200;

        private void runNextStep(Action onCompletion)
        {
            loadableStep?.TriggerClick();

            string text = $"{(int)Time.Current}: ".PadLeft(7);

            if (actionIndex < 0)
                text += $"{GetType().ReadableName()}";
            else
            {
                if (actionRepetition == 0)
                    text += $"  Step #{actionIndex + 1}";
                text = text.PadRight(20) + $"{loadableStep?.ToString() ?? string.Empty}";
            }

            Console.WriteLine(text);

            actionRepetition++;

            if (actionRepetition > (loadableStep?.RequiredRepetitions ?? 1) - 1)
            {
                actionIndex++;
                actionRepetition = 0;
            }

            if (actionIndex > StepsContainer.Children.Count() - 1)
            {
                onCompletion?.Invoke();
                return;
            }

            if (Parent != null)
                stepRunner = Scheduler.AddDelayed(() => runNextStep(onCompletion), TimePerAction);
        }

        protected void AddStep(string description, Action action)
        {
            StepsContainer.Add(new SingleStepButton
            {<commit_msg>Make visualtest output more concise.<commit_after>﻿// Copyright (c) 2007-2017 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Linq;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing.Drawables.StepButtons;
using osu.Framework.Threading;
using OpenTK;
using OpenTK.Graphics;

namespace osu.Framework.Testing
{
    public abstract class TestCase : Container
    {
        public virtual string Description => @"The base class for a test case";

        public FillFlowContainer<StepButton> StepsContainer;

        private Container content;

        protected override Container<Drawable> Content => content;

        protected TestCase()
        {
            Name = GetType().ReadableName().Substring(8); // Skip the "TestCase prefix

            RelativeSizeAxes = Axes.Both;
            Masking = true;
        }

        private const float steps_width = 180;
        private const float padding = 0;

        public virtual void Reset()
        {
            if (content == null)
            {
                InternalChildren = new Drawable[]
                {
                    new Box
                    {
                        Colour = new Color4(25, 25, 25, 255),
                        RelativeSizeAxes = Axes.Y,
                        Width = steps_width,
                    },
                    StepsContainer = new FillFlowContainer<StepButton>
                    {
                        Direction = FillDirection.Vertical,
                        Depth = float.MinValue,
                        Padding = new MarginPadding(5),
                        Spacing = new Vector2(5),
                        AutoSizeAxes = Axes.Y,
                        Width = steps_width,
                    },
                    new Container
                    {
                        Masking = true,
                        Padding = new MarginPadding
                        {
                            Left = steps_width + padding,
                            Right = padding,
                            Top = padding,
                            Bottom = padding,
                        },
                        RelativeSizeAxes = Axes.Both,
                        Children = new []
                        {
                            content = new Container
                            {
                                Masking = true,
                                RelativeSizeAxes = Axes.Both
                            }
                        }
                    },
                };
            }
            else
            {
                content.Clear();
                StepsContainer.Clear();
            }
        }

        private int actionIndex;
        private int actionRepetition;
        private ScheduledDelegate stepRunner;

        public void RunAllSteps(Action onCompletion = null)
        {
            stepRunner?.Cancel();

            actionIndex = -1;
            actionRepetition = 0;
            runNextStep(onCompletion);
        }

        private StepButton loadableStep => actionIndex >= 0 ? StepsContainer.Children.Skip(actionIndex).FirstOrDefault() : null;

        protected virtual double TimePerAction => 200;

        private void runNextStep(Action onCompletion)
        {
            loadableStep?.TriggerClick();

            string text = ".";

            if (actionRepetition == 0)
            {
                text = $"{(int)Time.Current}: ".PadLeft(7);

                if (actionIndex < 0)
                    text += $"{GetType().ReadableName()}";
                else
                {
                    text += $"step {actionIndex + 1}";
                    text = text.PadRight(16) + $"{loadableStep?.ToString() ?? string.Empty}";
                }
            }

            Console.Write(text);

            actionRepetition++;

            if (actionRepetition > (loadableStep?.RequiredRepetitions ?? 1) - 1)
            {
                Console.WriteLine();
                actionIndex++;
                actionRepetition = 0;
            }

            if (actionIndex > StepsContainer.Children.Count() - 1)
            {
                onCompletion?.Invoke();
                return;
            }

            if (Parent != null)
                stepRunner = Scheduler.AddDelayed(() => runNextStep(onCompletion), TimePerAction);
        }

        protected void AddStep(string description, Action action)
        {
            StepsContainer.Add(new SingleStepButton
            {
{'fraction_non_alphanumeric': 0.08179110827483514, 'fraction_numerical': 0.008934269304403318, 'mean_word_length': 1.2077952571026063, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5265890', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2078, 'n_words': 496}