<commit_before>﻿using FinesSE.Launcher.Formats;
using FinesSE.Launcher.Infrastructure;
using fitSharp.Fit.Fixtures;
using fitSharp.Fit.Model;
using fitSharp.Fit.Operators;
using fitSharp.Fit.Service;
using fitSharp.IO;
using fitSharp.Machine.Engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinesSE.Launcher
{
    public class FitRunner
    {
        readonly IEnumerable<string> assemblies;
        readonly IEnumerable<string> namespaces;
        readonly FormatConvertor convertor;

        public FitRunner(IEnumerable<string> assemblies, IEnumerable<string> namespaces)
        {
            this.assemblies = assemblies;
            this.namespaces = namespaces;
            convertor = new FormatConvertor();
        }

        public async Task ExecuteAsync(FileInfo inputPath, TableFormat inputFormat, string outputFilePath)
        {
            var inputData = File.ReadAllText(inputPath.FullName);
            var result = await ExecuteAsync(convertor.Convert(inputData, inputFormat), System.IO.Path.GetFileName(inputPath.FullName));
            File.WriteAllText(outputFilePath, result);
        }

        public async Task<string> ExecuteAsync(string inputData, TableFormat inputFormat, string inputAnnotation)
        {
            return await ExecuteAsync(convertor.Convert(inputData, inputFormat), inputAnnotation);
        }

        Task<string> ExecuteAsync(string input, string inputAnnotation)
        {
            CreateStoryTest(input, out StoryTestStringWriter writer, out StoryTest storyTest);

            var elapsedTime = new ElapsedTime();
            storyTest.Execute();

            var result = new PageResult(inputAnnotation, writer.Tables, writer.Counts, elapsedTime);
            Console.WriteLine(FormatInfo(result));

            return Task.FromResult(result.Content);
        }

        string FormatInfo(PageResult result)
            => $"{result.ElapsedTime}\t{result.Title}\t{result.TestCounts.Description}";

        void CreateStoryTest(string input, out StoryTestStringWriter writer, out StoryTest storyTest)
        {
            CellProcessorBase processor = CreateProcessor();
            
            writer = new StoryTestStringWriter(processor);
            storyTest = new StoryTest(processor, writer).WithInput(input);

            var tree = new fitSharp.Slim.Model.SlimTree();
<commit_msg>Interpose arguments in Fit runner<commit_after>﻿using FinesSE.Launcher.Formats;
using FinesSE.Launcher.Infrastructure;
using fitSharp.Fit.Fixtures;
using fitSharp.Fit.Model;
using fitSharp.Fit.Operators;
using fitSharp.Fit.Service;
using fitSharp.IO;
using fitSharp.Machine.Engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FinesSE.Launcher
{
    public class FitRunner
    {
        readonly IEnumerable<string> assemblies;
        readonly IEnumerable<string> namespaces;
        readonly FormatConvertor convertor;

        public FitRunner(IEnumerable<string> assemblies, IEnumerable<string> namespaces)
        {
            this.assemblies = assemblies;
            this.namespaces = namespaces;
            convertor = new FormatConvertor();
        }

        public async Task ExecuteAsync(FileInfo inputPath, TableFormat inputFormat, string outputFilePath)
        {
            var inputData = File.ReadAllText(inputPath.FullName);
            var result = await ExecuteAsync(convertor.Convert(inputData, inputFormat), System.IO.Path.GetFileName(inputPath.FullName));
            File.WriteAllText(outputFilePath, result);
        }

        public async Task<string> ExecuteAsync(string inputData, TableFormat inputFormat, string inputAnnotation)
        {
            return await ExecuteAsync(convertor.Convert(inputData, inputFormat), inputAnnotation);
        }

        string ApplyInterposedArguments(string input)
        {
            const string tdPattern = @"<td>.*?<\/td>";
            const string interposeCharacter = ";";

            var builder = new StringBuilder();
            foreach (var line in input.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
            {
                var tdIndex = 0;
                foreach (Match td in Regex.Matches(line, tdPattern))
                {
                    if (tdIndex == 0 && !td.Value.Contains(interposeCharacter))
                    {
                        builder.Append(line);
                        builder.AppendLine();
                        break;
                    }
                    else if (tdIndex == 0)
                        builder.Append("<tr>");

                    builder.Append(td.Value);

                    if (tdIndex != 0)
                        builder.Append(@"<td></td>");

                    tdIndex++;
                }

                builder.Append("</tr>");
                builder.AppendLine();
            }

            return builder.ToString();
        }

        Task<string> ExecuteAsync(string input, string inputAnnotation)
        {
            CreateStoryTest(ApplyInterposedArguments(input), out StoryTestStringWriter writer, out StoryTest storyTest);

            var elapsedTime = new ElapsedTime();
            storyTest.Execute();

            var result = new PageResult(inputAnnotation, writer.Tables, writer.Counts, elapsedTime);
            Console.WriteLine(FormatInfo(result));

            return Task.FromResult(result.Content);
        }

        string FormatInfo(PageResult result)
            => $"{result.ElapsedTime}\t{result.Title}\t{result.TestCounts.Description}";

        void CreateStoryTest(string input, out StoryTestStringWriter writer, out StoryTest storyTest)
        {
            CellProcessorBase processor = CreateProcessor();

            writer = new StoryTestStringWriter(processor);
            storyTest = new StoryTest(processor, writer).WithInput(input);

            var tree = new fitSharp.Slim.Model.SlimTree();

{'fraction_non_alphanumeric': 0.08861588861588862, 'fraction_numerical': 0.0006552006552006552, 'mean_word_length': 2.4438804286520024, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '958208', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1491, 'n_words': 342}