Beispiel #1
0
        public void MakeParseOperation_LineParseIsSuccessful_EndsLineParse(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };

            foreach (var handlerIndex in Enumerable.Repeat(0, 3))
            {
                testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Success);
            }

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var configLineData in configData)
            {
                foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers)
                {
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnStarting(), 1);
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnCompleted(), 1);
                }

                testContext.MockManagedBlockConfigParseHandlers[0]
                .ShouldHaveReceivedOnParsing(new ManagedBlockConfigLine(configLineData));

                foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers.Skip(1))
                {
                    mockManagedBlockConfigParseHandler.ShouldNotHaveReceived(x => x.OnParsing(It.IsAny <ManagedBlockConfigLine>()));
                }
            }

            testContext.MockLogger.ShouldNotHaveReceived(x => x.AddLine(It.IsAny <string>()));
        }
Beispiel #2
0
        public void MakeParseOperation_LineIsIgnored_LogsError(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };

            foreach (var _ in Enumerable.Repeat(0, 3))
            {
                testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Ignored);
            }

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var configLineData in configData)
            {
                foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers)
                {
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnStarting(), 1);
                    mockManagedBlockConfigParseHandler.ShouldHaveReceivedOnParsing(new ManagedBlockConfigLine(configLineData));
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnCompleted(), 1);
                }

                testContext.MockLogger.ShouldHaveReceived(x => x.AddLine(It.Is <string>(y => y.Contains(BuildConfigLine(configLineData)))));
            }
        }
Beispiel #3
0
        public void MakeParseOperation_Always_SplitsLinesAndParamsCorrectly(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };
            var mockManagedBlockConfigParseHandler = testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Success);

            var configLines = new List <ManagedBlockConfigLine>();

            mockManagedBlockConfigParseHandler
            .Setup(x => x.OnParsing(It.IsAny <ManagedBlockConfigLine>()))
            .Callback <ManagedBlockConfigLine>(x => configLines.Add(x))
            .Returns(ParseResult.Success);

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            configLines.Count.ShouldBe(configData.Length);
            foreach (var i in Enumerable.Range(0, configLines.Count))
            {
                var expectedConfigLine = new ManagedBlockConfigLine(configData[i]);

                configLines[i].BlockTag.ShouldBe(expectedConfigLine.BlockTag);
                configLines[i].Option.ShouldBe(expectedConfigLine.Option);
                configLines[i].ParamCount.ShouldBe(expectedConfigLine.ParamCount);

                foreach (var index in Enumerable.Range(0, configLines[i].ParamCount))
                {
                    configLines[i].GetParam(index).ShouldBe(expectedConfigLine.GetParam(index));
                }
            }
        }
Beispiel #4
0
        public void BuildParseOperation_OperationIsDisposed_OperationIsRecycled(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };

            testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Success);

            var result = testContext.Uut.MakeParseOperation(testContext.MockBlock.Object);

            result.ShouldRunToCompletion(testContext.MockBackgroundWorker);

            var mockConfigParseHandlersInvocations = testContext.MockManagedBlockConfigParseHandlers
                                                     .Select(x => x.Invocations.ToArray())
                                                     .ToArray();
            var mockBlockInvocations = testContext.MockBlock.Invocations
                                       .ToArray();

            result.ShouldBeAssignableTo <IDisposable>();
            (result as IDisposable).Dispose();

            var secondMockBlock = new Mock <IMyTerminalBlock>();

            secondMockBlock
            .Setup(x => x.CustomName)
            .Returns(() => testContext.MockBlock.Object.CustomName);
            secondMockBlock
            .Setup(x => x.CustomData)
            .Returns(() => testContext.MockBlock.Object.CustomData);

            testContext.MockManagedBlockConfigParseHandlers
            .ForEach(x => x.Invocations.Clear());
            testContext.MockBlock.Invocations.Clear();

            testContext.Uut.MakeParseOperation(secondMockBlock.Object)
            .ShouldBeSameAs(result);

            result.ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var i in Enumerable.Range(0, testContext.MockManagedBlockConfigParseHandlers.Count))
            {
                testContext.MockManagedBlockConfigParseHandlers[i].Invocations.Count.ShouldBe(mockConfigParseHandlersInvocations[i].Length);
                foreach (var j in Enumerable.Range(0, mockConfigParseHandlersInvocations.Length))
                {
                    testContext.MockManagedBlockConfigParseHandlers[i].Invocations[j].ShouldBe(mockConfigParseHandlersInvocations[i][j]);
                }
            }

            testContext.MockBlock.Invocations.Count.ShouldBe(mockBlockInvocations.Length);
            foreach (var i in Enumerable.Range(0, mockBlockInvocations.Length))
            {
                testContext.MockBlock.Invocations[i].ShouldBe(mockBlockInvocations[i]);
            }
        }
Beispiel #5
0
        public void MakeParseOperation_LineParseIsError_LogsErrorAndEndsLineParse(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };

            var errorText = "errorText";

            foreach (var handlerIndex in Enumerable.Repeat(0, 3))
            {
                testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.FromError($"{errorText}{handlerIndex}"));
            }

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var configLineData in configData)
            {
                foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers)
                {
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnStarting(), 1);
                    mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnCompleted(), 1);
                }

                testContext.MockManagedBlockConfigParseHandlers[0]
                .ShouldHaveReceivedOnParsing(new ManagedBlockConfigLine(configLineData));

                foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers.Skip(1))
                {
                    mockManagedBlockConfigParseHandler.ShouldNotHaveReceived(x => x.OnParsing(It.IsAny <ManagedBlockConfigLine>()));
                }

                testContext.MockLogger.ShouldHaveReceived(x => x.AddLine(It.Is <string>(y =>
                                                                                        y.Contains(BuildConfigLine(configLineData)) &&
                                                                                        y.Contains($"{errorText}0"))));

                foreach (var i in Enumerable.Range(0, testContext.MockManagedBlockConfigParseHandlers.Count).Skip(1))
                {
                    testContext.MockLogger.ShouldNotHaveReceived(x => x.AddLine(It.Is <string>(y =>
                                                                                               y.Contains(BuildConfigLine(configLineData)) &&
                                                                                               y.Contains($"{errorText}{i}"))));
                }
            }
        }
Beispiel #6
0
        public void MakeParseOperation_ConfigIsEmptyOrWhitespace_CompletesAfterStarting(string config)
        {
            var testContext = new TestContext()
            {
                Config = config
            };

            testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Ignored);

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers)
            {
                mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnStarting(), 1);
                mockManagedBlockConfigParseHandler.ShouldNotHaveReceived(x => x.OnParsing(It.IsAny <ManagedBlockConfigLine>()));
                mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnCompleted(), 1);
            }

            testContext.MockLogger.ShouldNotHaveReceived(x => x.AddLine(It.IsAny <string>()));
        }
Beispiel #7
0
        public void MakeParseOperation_BlockTagIsWrong_IgnoresLine(string blockTag, string[][] configData)
        {
            var testContext = new TestContext()
            {
                BlockTag = blockTag,
                Config   = BuildConfig(configData)
            };

            testContext.AddFakeManagedBlockConfigParseHandler(ParseResult.Ignored);

            testContext.Uut.MakeParseOperation(testContext.MockBlock.Object)
            .ShouldRunToCompletion(testContext.MockBackgroundWorker);

            foreach (var mockManagedBlockConfigParseHandler in testContext.MockManagedBlockConfigParseHandlers)
            {
                mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnStarting(), 1);
                mockManagedBlockConfigParseHandler.ShouldNotHaveReceived(x => x.OnParsing(It.IsAny <ManagedBlockConfigLine>()));
                mockManagedBlockConfigParseHandler.ShouldHaveReceived(x => x.OnCompleted(), 1);
            }

            testContext.MockLogger.ShouldNotHaveReceived(x => x.AddLine(It.IsAny <string>()));
        }