Ejemplo n.º 1
0
        private static bool ConvertInput(InputItem item, IConsoleAdapter consoleOut, out object value, string input)
        {
            if (item.ReadInfo != null && item.ReadInfo.Options.Any())
                return SelectOption(input, item.ReadInfo.Options, consoleOut, out value);

            return ConvertString(input, item.Type, consoleOut, out value);
        }
 private static string MakeSelectPrompt(InputItem item, string prompt)
 {
     var options = item.ReadInfo.Options.Select(MakeOptionText);
     var optionString = string.Format("[{0}]", string.Join(", ", options));
     var displayPrompt = string.Format("{0}{1}{2}",
         prompt,
         string.IsNullOrWhiteSpace(prompt) ? string.Empty : " ",
         optionString);
     return AddPromptSuffix(displayPrompt);
 }
        internal static string FromItem(InputItem item)
        {
            if (item.ReadInfo == null)
                return AddPromptSuffix(PropertyNameConverter.ToPrompt(item.Property));

            var prompt = item.ReadInfo.Prompt ?? PropertyNameConverter.ToPrompt(item.Property);

            if (item.ReadInfo.Options.Any())
                return MakeOptionsPrompt(item, prompt);

            return AddPromptSuffix(prompt);
        }
Ejemplo n.º 4
0
        public void InteractiveInputContinuesUntilGoodInputReceived()
        {
            _interface.SetInputStream(_goodStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Approvals.Verify(_interface.GetBuffer());
        }
Ejemplo n.º 5
0
        private static bool ApplyValidations(InputItem item, object value, IConsoleAdapter consoleOut)
        {
            if (item.ReadInfo != null)
            {
                var error = item.ReadInfo.GetValidationError(value);
                if (error != null)
                {
                    consoleOut.WrapLine(error);
                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 6
0
        public void ErrorIsDisplayedWhenInputIsInvalid()
        {
            _interface.SetInputStream(_goodStream);
            _interface.InputIsRedirected = true;
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Approvals.Verify(_interface.GetBuffer());
        }
        private static string MakeMenuPrompt(InputItem item, string prompt)
        {
            var promptBuilder = new StringBuilder();
            if (item.ReadInfo.MenuHeading != null)
            {
                promptBuilder.AppendLine(item.ReadInfo.MenuHeading);
                promptBuilder.AppendLine();
            }

            foreach (var option in item.ReadInfo.Options)
                promptBuilder.AppendLine(MakeOptionText(option));

            promptBuilder.AppendLine();
            promptBuilder.Append(AddPromptSuffix(prompt));
            return promptBuilder.ToString();
        }
        public void NoPropertyInfoGivesNoPromptText()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = null,
                Type = typeof(int)
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            Assert.That(result, Is.EqualTo(": "));
        }
Ejemplo n.º 9
0
        public void InvalidReadReturnsFalse()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "text" }));
            _interface.InputIsRedirected = true;
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int)
            };

            //Act
            object value;
            var result = ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Assert.That(result, Is.False);
        }
Ejemplo n.º 10
0
        public void ErrorIsDisplayedWhenInputIsInvalid()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "text" }));
            _interface.InputIsRedirected = true;
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
            };

            //Act
            object value;
            ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Approvals.Verify(_interface.GetBuffer());
        }
        public void NoPropertyInfoOnMenuGivesMenuText()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = null,
                Type = typeof(int),
                ReadInfo = Read.Int().Option(4, "A", "Four").Option(10, "B", "Ten").AsMenu("No Property")
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            var expected = @"No Property

            A-Four
            B-Ten

            : ";
            Assert.That(result, Is.EqualTo(expected));
        }
Ejemplo n.º 12
0
        public void ValidationsAreApplied()
        {
            _interface.SetInputStream(_validationStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Validate(i => i > 10, "Value must be greater than 10")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            var value = ((Read<int>)item.Value).Value;
            Assert.That(value, Is.EqualTo(11));
        }
Ejemplo n.º 13
0
        public void ValidationErrorMessageIsDisplayed()
        {
            _interface.SetInputStream(_validationStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Validate(i => i > 10, "Value must be greater than 10")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Approvals.Verify(_interface.GetBuffer());
        }
Ejemplo n.º 14
0
        public void TextIsConvertedToRequiredType()
        {
            _interface.SetInputStream(_goodStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int)
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Assert.That(item.Value, Is.EqualTo(45));
        }
Ejemplo n.º 15
0
        public void StringCanBeRead()
        {
            _interface.SetInputStream(_goodStream);
            var item = new InputItem
            {
                Name = "StringVal",
                Property = StringProp,
                Type = typeof(string)
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Assert.That(item.Value, Is.EqualTo("text"));
        }
Ejemplo n.º 16
0
        public void OptionInputIsNotCaseSensitive()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "c" }));
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
                    .Option(100, "B", "First")
                    .Option(200, "C", "Second")
                    .Option(300, "D", "Third")
            };

            //Act
            object value;
            ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Assert.That(value, Is.EqualTo(200));
        }
Ejemplo n.º 17
0
        public void ValidReadReturnsTrue()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "text" }));
            var item = new InputItem
            {
                Name = "StringVal",
                Property = StringProp,
                Type = typeof(string)
            };

            //Act
            object value;
            var result = ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Assert.That(result, Is.True);
        }
Ejemplo n.º 18
0
 private static string MakeOptionsPrompt(InputItem item, string prompt)
 {
     return !item.ReadInfo.ShowAsMenu
         ? MakeSelectPrompt(item, prompt)
         : MakeMenuPrompt(item, prompt);
 }
        public void PromptIsTakenFromPropertyInfo()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("My prompt")
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            Assert.That(result, Is.EqualTo("My prompt: "));
        }
        public void PromptIsAutomaticallyDerivedFromPropertyName()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int)
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            Assert.That(result, Is.EqualTo("Int Val: "));
        }
        public void PromptCanDisplaySelectionAsMenu()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Option(4, "A", "Four").Option(10, "B", "Ten")
                .AsMenu("Which value?")
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            var expected = @"Which value?

            A-Four
            B-Ten

            Int Val: ";
            Assert.That(result, Is.EqualTo(expected));
        }
Ejemplo n.º 22
0
        public void ValidReadReturnsTrue()
        {
            _interface.SetInputStream(_stringOnlyStream);
            var item = new InputItem
            {
                Name = "StringVal",
                Property = StringProp,
                Type = typeof(string)
            };

            Assert.That(ReadInputItem.GetValue(item, _interface, _adapter), Is.True);
        }
Ejemplo n.º 23
0
        public void ValidationPassSetsValue()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "11" }));
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Validate(i => i > 10, "Value must be greater than 10")
            };

            //Act
            object value;
            ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Assert.That(value, Is.EqualTo(11));
        }
Ejemplo n.º 24
0
        public void OptionsCanBeSpecified()
        {
            _interface.SetInputStream(_selectStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
                .Option(1, "1", "First")
                .Option(2, "2", "Second")
                .Option(3, "3", "Third")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Approvals.Verify(_interface.GetBuffer());
        }
Ejemplo n.º 25
0
        public void InvalidReadReturnsFalse()
        {
            _interface.SetInputStream(_stringOnlyStream);
            _interface.InputIsRedirected = true;
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int)
            };

            Assert.That(ReadInputItem.GetValue(item, _interface, _adapter), Is.False);
        }
Ejemplo n.º 26
0
        public void PromptIsDisplayed()
        {
            _interface.SetInputStream(_goodStream);
            var item = new InputItem
            {
                Name = "StringVal",
                Property = StringProp,
                Type = typeof(string),
                ReadInfo = Read.String().Prompt("prompt")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            Approvals.Verify(_interface.GetBuffer());
        }
        public void NoPropertyInfoOnSelectionGivesSelectionText()
        {
            //Arrange
            var inputItem = new InputItem
            {
                Name = "IntVal",
                Property = null,
                Type = typeof(int),
                ReadInfo = Read.Int().Option(4, "A", "Four").Option(10, "B", "Ten")
            };

            //Act
            var result = ConstructPromptText.FromItem(inputItem);

            //Assert
            Assert.That(result, Is.EqualTo("[A-Four, B-Ten]: "));
        }
Ejemplo n.º 28
0
 internal static bool UsingReadLine(InputItem item, IConsoleInInterface consoleIn, IConsoleAdapter consoleOut, out object value)
 {
     var input = consoleIn.ReadLine();
     return ConvertInput(item, consoleOut, out value, input)
            && ApplyValidations(item, value, consoleOut);
 }
Ejemplo n.º 29
0
        public void OptionIsSelected()
        {
            _interface.SetInputStream(_selectStream);
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Prompt("prompt")
                .Option(100, "B", "First")
                .Option(200, "C", "Second")
                .Option(300, "D", "Third")
            };

            ReadInputItem.GetValue(item, _interface, _adapter);
            var value = ((Read<int>)item.Value).Value;
            Assert.That(value, Is.EqualTo(200));
        }
Ejemplo n.º 30
0
        public void ValidationErrorMessageIsDisplayed()
        {
            //Arrange
            _interface.SetInputStream(MakeStream(new[] { "10" }));
            var item = new InputItem
            {
                Name = "IntVal",
                Property = IntProp,
                Type = typeof(int),
                ReadInfo = Read.Int().Validate(i => i > 10, "Value must be greater than 10")
            };

            //Act
            object value;
            ReadValue.UsingReadLine(item, _interface, _adapter, out value);

            //Assert
            Approvals.Verify(_interface.GetBuffer());
        }