Ejemplo n.º 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));
    }
Ejemplo n.º 2
0
        public static ClipSlot HandleInput(byte[] inputData)
        {
            var generateUnitTest = false;
            var generateSvgDoc   = false;

            if (Decoder.IsStringData(inputData))
            {
                string text = Decoder.GetText(inputData);
                Console.WriteLine(text);
                return(ClipSlot.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(ClipSlot.Empty);
            }

            ProcessResultArray <Clip> processedClipWrapper;

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

            if (processedClipWrapper.WarningMessage.Length > 0)
            {
                Console.WriteLine($"Warnings were generated:{System.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);
                }
                ClipSlot processedClipSlot = new ClipSlot(formula, processedClip, chainedCommandWrapper.Result, id);
                return(processedClipSlot);
            }
            Console.WriteLine($"Error applying formula: {processedClipWrapper.ErrorMessage}");
            return(ClipSlot.Empty);
        }
Ejemplo n.º 3
0
        public static void Start()
        {
            while (true)
            {
                var generateUnitTest = false;
                var generateSvgDoc   = false;
                var inputData        = UdpConnector.WaitForData();

                if (UdpConnector.IsString(inputData))
                {
                    string text = UdpConnector.GetText(inputData);
                    Console.WriteLine(text);
                    continue;
                }

                (List <Clip> clips, string formula, ushort id, byte trackNo) = UdpConnector.DecodeData(inputData);
                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);
                    continue;
                }

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

                if (processedClipWrapper.WarningMessage.Length > 0)
                {
                    Console.WriteLine($"Warnings were generated:{System.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);
                    }

                    UdpConnector.SetClipAsBytesById(processedClipData);
                }
                else
                {
                    Console.WriteLine($"Error applying formula: {processedClipWrapper.ErrorMessage}");
                }
            }
        }