예제 #1
0
    public static void InputShouldProduceGivenOutput(byte[] input, byte[] output)
    {
        var(clips, formula, id, trackNo) = Decoder.DecodeData(input);
        var chainedCommandWrapper = Parser.ParseFormulaToChainedCommand(formula, clips, new ClipMetaData(id, trackNo));

        Assert.IsTrue(chainedCommandWrapper.Success);

        var processedClipWrapper = ClipProcessor.ProcessChainedCommand(chainedCommandWrapper.Result);

        Assert.IsTrue(processedClipWrapper.Success);
        Assert.IsTrue(processedClipWrapper.Result.Length > 0);

        var processedClip = processedClipWrapper.Result[0];

        byte[] clipData = IOUtilities.GetClipAsBytes(chainedCommandWrapper.Result.TargetMetaData.Id, processedClip).ToArray();

        Assert.IsTrue(output.Length == clipData.Length);
        Assert.IsTrue(output.SequenceEqual(clipData));
    }
예제 #2
0
    public static LegacyClipSlot HandleInput(byte[] inputData)
    {
        var generateUnitTest = false;
        var generateSvgDoc   = false;

        if (Decoder.IsStringData(inputData))
        {
            string text = Decoder.GetText(inputData);
            Console.WriteLine(text);
            return(LegacyClipSlot.Empty);
        }

        (List <Clip> clips, string formula, ushort id, byte trackNo) = Decoder.DecodeData(inputData);
        formula = formula.Trim(' ');
        Console.WriteLine($"Received {clips.Count} clips and formula: {formula}");
        if (formula.EndsWith(UnitTestDirective))
        {
            Console.WriteLine(
                $"Saving autogenerated unit test to {Path.Join(Environment.CurrentDirectory, "GeneratedUnitTests.txt")}");
            formula          = formula.Substring(0, formula.Length - UnitTestDirective.Length);
            generateUnitTest = true;
        }

        if (formula.EndsWith(SvgDocDirective))
        {
            Console.WriteLine(
                $"Saving autogenerated SVG documentation for this formula to {Path.Join(Environment.CurrentDirectory, "GeneratedDocs.svg")}");
            formula        = formula.Substring(0, formula.Length - SvgDocDirective.Length);
            generateSvgDoc = true;
        }

        var chainedCommandWrapper = Parser.ParseFormulaToChainedCommand(formula, clips, new ClipMetaData(id, trackNo));

        if (!chainedCommandWrapper.Success)
        {
            Console.WriteLine(chainedCommandWrapper.ErrorMessage);
            return(LegacyClipSlot.Empty);
        }

        ProcessResult <Clip[]> processedClipWrapper;

        try
        {
            processedClipWrapper = ClipProcessor.ProcessChainedCommand(chainedCommandWrapper.Result);
        }
        catch (Exception e)
        {
            processedClipWrapper =
                new ProcessResult <Clip[]>($"{formula}. Please check your syntax. Exception: {e.Message}");
        }

        if (processedClipWrapper.WarningMessage.Length > 0)
        {
            Console.WriteLine($"Warnings were generated:{Environment.NewLine}" +
                              $"{processedClipWrapper.WarningMessage}");
        }

        if (processedClipWrapper.Success && processedClipWrapper.Result.Length > 0)
        {
            var    processedClip     = processedClipWrapper.Result[0];
            byte[] processedClipData = IOUtilities
                                       .GetClipAsBytes(chainedCommandWrapper.Result.TargetMetaData.Id, processedClip).ToArray();

            if (generateUnitTest)
            {
                TestUtilities.AppendUnitTest(formula, inputData, processedClipData);
            }

            if (generateSvgDoc)
            {
                SvgUtilities.GenerateSvgDoc(formula, clips, processedClip, 882, 300);
            }

            LegacyClipSlot processedLegacyClipSlot =
                new LegacyClipSlot(formula, processedClip, chainedCommandWrapper.Result, id);
            return(processedLegacyClipSlot);
        }

        Console.WriteLine($"Error applying formula: {processedClipWrapper.ErrorMessage}");
        return(LegacyClipSlot.Empty);
    }