Ejemplo n.º 1
0
        public void When_prepending_item_on_cache_will_reply_with_stored()
        {
            var      buffer  = new byte[] { 1, 2, 3, 4 };
            ICommand command = new SetCommand();

            command.SetContext(GetStreamWithData(buffer));
            command.Init("foo", "1", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            wait.Reset();

            buffer = new byte[] { 5, 6, 7, 8 };
            MemoryStream stream = GetStreamWithData(buffer);

            command = new PrependCommand();
            command.SetContext(stream);
            command.Init("foo", "1", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("STORED\r\n", ReadAll(6, stream));
        }
Ejemplo n.º 2
0
        public void When_prepending_item_on_cache_will_prepend_to_data_already_on_cache()
        {
            var      buffer  = new byte[] { 1, 2, 3, 4 };
            ICommand command = new SetCommand();

            command.SetContext(GetStreamWithData(buffer));
            command.Init("foo", "1", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            wait.Reset();

            buffer = new byte[] { 5, 6, 7, 8 };
            MemoryStream stream = GetStreamWithData(buffer);

            command = new PrependCommand();
            command.SetContext(stream);
            command.Init("foo", "1", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            var item = (CachedItem)Cache.Get("foo");

            CollectionAssert.AreEqual(new byte[] { 5, 6, 7, 8, 1, 2, 3, 4, }, item.Buffer);
        }
Ejemplo n.º 3
0
        public void When_prepending_item_on_cache_will_not_modify_flags()
        {
            var      buffer  = new byte[] { 1, 2, 3, 4 };
            ICommand command = new SetCommand();

            command.SetContext(GetStreamWithData(buffer));
            command.Init("foo", "1", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            wait.Reset();

            buffer = new byte[] { 5, 6, 7, 8 };
            MemoryStream stream = GetStreamWithData(buffer);

            command = new PrependCommand();
            command.SetContext(stream);
            command.Init("foo", "15", "6000", "4");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            var item = (CachedItem)Cache.Get("foo");

            Assert.AreEqual(1, item.Flags);
        }
Ejemplo n.º 4
0
 public async Task ReadResponse()
 {
     var command = new PrependCommand();
     await StorageCommandValidator.AssertReadResponse(command, StorageCommandResult.Stored, true);
     await StorageCommandValidator.AssertReadResponse(command, StorageCommandResult.NotStored, false);
     await StorageCommandValidator.AssertReadResponseFailure<PrependCommand, bool>(command, StorageCommandResult.NotFound);
     await StorageCommandValidator.AssertReadResponseFailure<PrependCommand, bool>(command, StorageCommandResult.Exists);
 }
            public async Task PrependCommandHandler_MissingTask_ReturnsNull()
            {
                var command = new PrependCommand {
                    ItemNumber = 12, Text = "Add"
                };
                var result = await _handler.Handle(command, new CancellationToken());

                result.Should().BeNull();
            }
            public async Task PrependCommandHandler_PrependedIsWrittenToFile()
            {
                var command = new PrependCommand {
                    ItemNumber = 4, Text = "Add"
                };

                _ = await _handler.Handle(command, new CancellationToken());

                _taskFile.TaskLines[2].Should().StartWith("Add This");
            }
            public async Task PrependCommandHandler_StripsWhitespace()
            {
                var command = new PrependCommand {
                    ItemNumber = 4, Text = " Add "
                };
                var result = await _handler.Handle(command, new CancellationToken());

                result.Should().NotBeNull();
                result.Description.Should().StartWith("Add This");
            }
            public async Task PrependCommandHandler_PrependsText(int line, string expected)
            {
                var command = new PrependCommand {
                    ItemNumber = line, Text = "Add"
                };
                var result = await _handler.Handle(command, new CancellationToken());

                result.Should().NotBeNull();
                result.ToString().Should().StartWith(expected);
            }
            public async Task PrependCommandHandler_RemovesBlankLinesFromTodoFile()
            {
                var command = new PrependCommand {
                    ItemNumber = 2, Text = "Add"
                };

                _ = await _handler.Handle(command, new CancellationToken());

                _taskFile.TaskLines.Should().HaveCount(4);
            }
Ejemplo n.º 10
0
        public async Task ReadResponse()
        {
            var command = new PrependCommand();
            await StorageCommandValidator.AssertReadResponse(command, StorageCommandResult.Stored, true);

            await StorageCommandValidator.AssertReadResponse(command, StorageCommandResult.NotStored, false);

            await StorageCommandValidator.AssertReadResponseFailure <PrependCommand, bool>(command, StorageCommandResult.NotFound);

            await StorageCommandValidator.AssertReadResponseFailure <PrependCommand, bool>(command, StorageCommandResult.Exists);
        }
Ejemplo n.º 11
0
        private async Task Prepend(int item, string text)
        {
            var command = new PrependCommand {
                ItemNumber = item, Text = text
            };
            var result = await Mediator.Send(command);

            if (result is null)
            {
                Console.WriteLine($"TODO: No task {item}.");
            }
            else
            {
                Console.WriteLine(result.ToString(true));
            }
        }
        public Command ParseCommand(string commandInput)
        {
            var commandArguments = commandInput.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            // first check if it's a valid command

            var length = commandArguments.Length;

            Command command = null;

            var action     = commandArguments[0];
            var parameters = commandArguments.Skip(1).ToList();


            // make it with reflection, if you hate your life
            switch (action)
            {
            case "append":
                ValidateCommandLength(length, Constants.AppendCommandLength);
                command = new AppendCommand(parameters);
                break;

            case "prepend":
                ValidateCommandLength(length, Constants.PrependCommandLength);
                command = new PrependCommand(parameters);
                break;

            case "reverse":
                ValidateCommandLength(length, Constants.ReverseCommandLength);
                command = new ReverseCommand(parameters);
                break;

            case "insert":
                ValidateCommandLength(length, Constants.InsertCommandLength);
                command = new InsertCommand(parameters);
                break;

            case "delete":
                ValidateCommandLength(length, Constants.DeleteCommandLength);
                command = new DeleteCommand(parameters);
                break;

            case "roll":
                ValidateCommandLength(length, Constants.RollCommandLength);
                command = new RollCommand(parameters);
                break;

            case "sort":
                ValidateCommandLength(length, Constants.SortCommandLength);
                command = new SortCommand(parameters);
                break;

            case "count":
                ValidateCommandLength(length, Constants.CountCommandLength);
                command = new CountCommand(parameters);
                break;

            case "end":
                this.logger.Write(Constants.FinishedMessage);
                command = new EndCommand(parameters);
                break;

            default:
                throw new ArgumentException(Constants.InvalidCommand);
            }

            return(command);
        }