Exemplo n.º 1
0
        public void Parse_Concurrency_ThrowsNodeConcurrencyException()
        {
            // Arrange
            INodeFamily nodeFamily = new NodeFamily("family");
            INode       idle       = new IdleNode(nodeFamily, null);
            INode       exactText  = new ExactTextNode(
                "foo",
                new[] { WordTextClass.Instance, },
                false,
                (node, token, arg3) => { },
                nodeFamily,
                null);
            INode someText = new TextNode(
                new ITextClass[] { WordTextClass.Instance, },
                null,
                nodeFamily,
                null);

            idle.EstablishLink(someText);
            idle.EstablishLink(exactText);
            someText.EstablishLink(EndNode.Instance);
            exactText.EstablishLink(EndNode.Instance);

            IParser parser = new Parser();

            var tokens = new List <IToken>
            {
                new TextToken(
                    WordTextClass.Instance,
                    NoneTextDecoration.Instance,
                    "foo",
                    Position.Zero,
                    3),
            };

            // Act
            parser.Root = idle;
            var ex = Assert.Throws <NodeConcurrencyException>(() => parser.Parse(tokens));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("More than one node accepted the token."));
            Assert.That(ex.ConcurrentNodes, Has.Length.EqualTo(2));
            Assert.That(ex.ConcurrentNodes, Does.Contain(exactText));
            Assert.That(ex.ConcurrentNodes, Does.Contain(someText));
            Assert.That(ex.Token, Is.SameAs(tokens.Single()));
        }
Exemplo n.º 2
0
        public static ICliFunctionalityProvider AddCustomHandlerWithParameter(
            this ICliFunctionalityProvider functionalityProvider,
            Action <IToken> handler,
            string tokenText)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (tokenText == null)
            {
                throw new ArgumentNullException(nameof(tokenText));
            }

            var family      = CheckArgumentsAndGetOrCreateFamily(functionalityProvider);
            var verbClass   = GetVerbTextClass(tokenText);
            var commandNode = new ExactTextNode(
                tokenText,
                verbClass,
                true,
                null,
                family,
                $"Custom handler node for verb '{tokenText}'");

            var argumentNode = new CustomActionNode(
                (node, token, resultAccumulator) =>
            {
                handler(token);
                throw new CliCustomHandlerException();
            },
                (token, resultAccumulator) => true,
                family,
                $"Argument node for custom handler with verb '{tokenText}'.");

            functionalityProvider.Node.EstablishLink(commandNode);
            commandNode.EstablishLink(argumentNode);

            return(functionalityProvider);
        }