public void CommandsForWithUnknownType()
        {
            var collection = new LocalCommandCollection();
            var id         = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.Throws <UnknownCommandException>(() => collection.CommandToInvoke(id));
        }
        public void InvokeWithOnlyInterfaceMethodParameters()
        {
            var id = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Func <int, double, string> func = (i, d) => (i + d).ToString(CultureInfo.InvariantCulture);
            var parameters = new[]
            {
                new CommandParameterDefinition(typeof(int), "i", CommandParameterOrigin.FromCommand),
                new CommandParameterDefinition(typeof(double), "d", CommandParameterOrigin.FromCommand),
            };

            Delegate commandToExecute = func;

            var definition = new CommandDefinition(id, parameters, true, commandToExecute);

            var parameterValues = new[]
            {
                new CommandParameterValueMap(
                    parameters[0],
                    10),
                new CommandParameterValueMap(
                    parameters[1],
                    10.0),
            };
            var result = definition.Invoke(new EndpointId("a"), new MessageId(), parameterValues);

            Assert.IsInstanceOf(typeof(string), result);
            Assert.AreEqual(20.0.ToString(CultureInfo.InvariantCulture), result);
        }
        public void CompareToWithUnequalObjectTypes()
        {
            var first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            var second = new object();

            Assert.Throws <ArgumentException>(() => first.CompareTo(second));
        }
        public void SmallerThanOperatorWithSecondObjectNull()
        {
            var       first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            CommandId second = null;

            Assert.IsFalse(first < second);
        }
        public void LargerThanOperatorWithFirstObjectNull()
        {
            CommandId first  = null;
            var       second = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.IsFalse(first > second);
        }
        public void CompareToOperatorWithEqualObjects()
        {
            var    first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            object second = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.AreEqual(0, first.CompareTo(second));
        }
        public void CompareToWithSmallerFirstObject()
        {
            var    first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            object second = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.IsTrue(first.CompareTo(second) < 0);
        }
        public void CompareToWithNullObject()
        {
            var    first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            object second = null;

            Assert.AreEqual(1, first.CompareTo(second));
        }
        public void Clone()
        {
            var first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            var second = first.Clone();

            Assert.AreEqual(first, second);
        }
        public void SmallerThanOperatorWithFirstObjectSmaller()
        {
            var first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            var second = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.IsTrue(first < second);
        }
        public void InvokeWithInvalidOrigin()
        {
            var id = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Func <int, double, string> func = (i, d) => (i + d).ToString(CultureInfo.InvariantCulture);
            var parameters = new[]
            {
                new CommandParameterDefinition(typeof(int), "i", CommandParameterOrigin.FromCommand),
                new CommandParameterDefinition(typeof(double), "d", CommandParameterOrigin.Unknown),
            };

            Delegate commandToExecute = func;

            var definition = new CommandDefinition(id, parameters, true, commandToExecute);

            var parameterValues = new[]
            {
                new CommandParameterValueMap(
                    parameters[0],
                    10),
                new CommandParameterValueMap(
                    parameters[1],
                    10.0),
            };

            Assert.Throws <InvalidCommandParameterOriginException>(() => definition.Invoke(new EndpointId("a"), new MessageId(), parameterValues));
        }
        public void LargerThanOperatorWithEqualObjects()
        {
            var first  = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));
            var second = CommandId.Create(typeof(double).GetMethod("CompareTo", new[] { typeof(object) }));

            Assert.IsFalse(first > second);
        }
Exemple #13
0
        public void From()
        {
            var map = CreateCommandMap();

            foreach (var method in typeof(ICommandInterface).GetMethods())
            {
                var id = CommandId.Create(method);
                Assert.AreEqual(1, map.Definitions.Count(m => m.Id.Equals(id)));
            }
        }
Exemple #14
0
        private MethodMapper CreateMethodMapper(LambdaExpression methodCall)
        {
            var methodInfo = ExtractMethod(methodCall);
            var returnType = ExtractInstanceMethodReturnType(methodInfo);
            var parameters = ExtractInstanceMethodParameters(methodInfo);

            return(new MethodMapper(
                       StoreDefinition,
                       CommandId.Create(methodInfo),
                       returnType,
                       parameters));
        }
        public void Create()
        {
            var id         = CommandId.Create(typeof(string).GetMethod("CompareTo", new[] { typeof(object) }));
            var parameters = new[]
            {
                new CommandParameterValueMap(
                    new CommandParameterDefinition(typeof(string), "a", CommandParameterOrigin.FromCommand),
                    "b"),
            };

            var invocationData = new CommandInvokedData(id, parameters);

            Assert.AreSame(id, invocationData.Command);
            Assert.AreSame(parameters, invocationData.Parameters);
        }
Exemple #16
0
        /// <summary>
        /// Creates the command map for the given command interface.
        /// </summary>
        /// <returns>The command map.</returns>
        /// <exception cref="CommandMethodNotMappedException">
        ///     Thrown when one or more of the methods on the command interface are not mapped to instance methods.
        /// </exception>
        public CommandMap ToMap()
        {
            var type    = typeof(TCommand);
            var methods = type.GetMethods();

            foreach (var method in methods)
            {
                var id = CommandId.Create(method);
                if (!m_Definitions.ContainsKey(id))
                {
                    throw new CommandMethodNotMappedException();
                }
            }

            return(new CommandMap(type, m_Definitions.Values.ToArray()));
        }
        public void Create()
        {
            var id = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            Func <int, double, string> func = (i, d) => (i + d).ToString(CultureInfo.InvariantCulture);
            var parameters = new[]
            {
                new CommandParameterDefinition(typeof(int), "i", CommandParameterOrigin.FromCommand),
                new CommandParameterDefinition(typeof(double), "d", CommandParameterOrigin.FromCommand),
            };

            Delegate commandToExecute = func;

            var definition = new CommandDefinition(id, parameters, true, commandToExecute);

            Assert.AreEqual(id, definition.Id);
            Assert.IsTrue(definition.HasReturnValue);
        }
        public void Register()
        {
            var collection = new LocalCommandCollection();

            var map = new[]
            {
                new CommandDefinition(
                    CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) })),
                    new[]
                {
                    new CommandParameterDefinition(typeof(int), "other", CommandParameterOrigin.FromCommand),
                },
                    false,
                    (Action) delegate { }),
            };

            collection.Register(map);

            Assert.IsTrue(collection.Any(id => id == map[0].Id));
        }
        public void InvokeWithMessageIdParameter()
        {
            var id = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));

            int       storedValue              = -1;
            MessageId storedMessage            = null;
            var       expectedResult           = "20.0";
            Func <int, MessageId, string> func =
                (i, m) =>
            {
                storedValue   = i;
                storedMessage = m;
                return(expectedResult);
            };
            var parameters = new[]
            {
                new CommandParameterDefinition(typeof(int), "i", CommandParameterOrigin.FromCommand),
                new CommandParameterDefinition(typeof(MessageId), "m", CommandParameterOrigin.InvokingMessageId),
            };

            Delegate commandToExecute = func;

            var definition = new CommandDefinition(id, parameters, true, commandToExecute);

            var parameterValues = new[]
            {
                new CommandParameterValueMap(
                    parameters[0],
                    10),
            };
            var endpoint = new EndpointId("a");
            var msg      = new MessageId();
            var result   = definition.Invoke(endpoint, msg, parameterValues);

            Assert.IsInstanceOf(typeof(string), result);
            Assert.AreEqual(expectedResult, result);
            Assert.AreEqual(expectedResult, result);
            Assert.AreEqual(parameterValues[0].Value, storedValue);
            Assert.AreEqual(msg, storedMessage);
        }
        public void CommandsFor()
        {
            var collection = new LocalCommandCollection();

            var map = new[]
            {
                new CommandDefinition(
                    CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) })),
                    new[]
                {
                    new CommandParameterDefinition(typeof(int), "other", CommandParameterOrigin.FromCommand),
                },
                    false,
                    (Action) delegate { }),
            };

            collection.Register(map);

            var commandSet = collection.CommandToInvoke(map[0].Id);

            Assert.AreSame(map[0], commandSet);
        }
        public void RegisterWithExistingType()
        {
            var collection = new LocalCommandCollection();

            var map = new[]
            {
                new CommandDefinition(
                    CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) })),
                    new[]
                {
                    new CommandParameterDefinition(typeof(int), "other", CommandParameterOrigin.FromCommand),
                },
                    false,
                    (Action) delegate { }),
            };

            collection.Register(map);
            Assert.AreEqual(1, collection.Count(id => id == map[0].Id));

            Assert.Throws <CommandAlreadyRegisteredException>(
                () => collection.Register(map));
            Assert.AreEqual(1, collection.Count(id => id == map[0].Id));
        }