Beispiel #1
0
 private void PlayPauseCommandAction()
 {
     if (CommandText.Equals(PLAY_COMMAND))
     {
         if (_stopped)
         {
             _stopped = false;
             _audioPlayerService.PlayFromFile("Galway.mp3");
         }
         else
         {
             _audioPlayerService.Play();
         }
         CommandText = PAUSE_COMMAND;
     }
     else
     {
         _audioPlayerService.Pause();
         CommandText = PLAY_COMMAND;
     }
 }
        private T ValidateCommand<T>() where T : CommandResult
        {
            if (_inCommands.Count == 0)
                return null;

            if (_commandIndex >= _inCommands.Count)
                return null;

            if (string.IsNullOrEmpty(CommandText))
                throw new CommandValidationException("CommandText is null or empty", this);

            var expected = _inCommands[_commandIndex];
            if (!string.IsNullOrEmpty(expected.CommandText) && !CommandText.Equals(expected.CommandText))
                throw new CommandValidationException(
                    "CommandText differs (expected, actual): \r\n" + expected.CommandText + "\r\n" + CommandText, this);

            // We should validate parameters
            if (expected.Parameters != null)
            {
                var max = Math.Max(expected.Parameters.Count, Parameters.Count);
                var msg = "Parameter validation failed.\r\n";
                var failed = false;
                for (var i = 0; i < max; i++)
                {
                    if (i >= expected.Parameters.Count)
                    {
                        msg += string.Format("Parameter '{0}': Did not expect this parameter, got value: '{1}'\r\n",
                                             Parameters[i].ParameterName,
                                             Parameters[i].Value);
                        failed = true;
                    }
                    else if (i >= Parameters.Count)
                    {
                        msg += string.Format("Parameter '{0}': Expected value '{1}', parameter was not specified\r\n",
                                             expected.Parameters[i].ParameterName,
                                             expected.Parameters[i].Value);
                        failed = true;
                    }
                    else if (!expected.Parameters[i].Value.Equals(Parameters[i].Value))
                    {
                        msg += string.Format("Parameter '{0}': Expected: '{1}', got: '{2}'\r\n",
                                             expected.Parameters[i].ParameterName, expected.Parameters[i].Value,
                                             Parameters[i].Value);
                        failed = true;
                    }
                    else
                    {
                        msg += string.Format("Parameter '{0}': Correct.\r\n", expected.Parameters[i].ParameterName);
                    }
                }

                if (failed)
                    throw new CommandValidationException(msg, this);
            }

            var casted = expected as T;
            if (casted == null)
                throw new InvalidOperationException(
                    string.Format("Expected Setup() command with index {0} to be of type '{1}'.", _commandIndex,
                                  typeof(T).Name));

            ++_commandIndex;
            return casted;
        }