<commit_before>﻿namespace chocolatey.tests.infrastructure.commandline
{
    using System;
    using System.Collections.Generic;
    using Moq;
    using Should;
    using chocolatey.infrastructure.adapters;
    using chocolatey.infrastructure.commandline;

    public class InteractivePromptSpecs
    {
        public abstract class InteractivePromptSpecsBase : TinySpec
        {
            protected Mock<IConsole> console = new Mock<IConsole>();
            protected IList<string> choices = new List<string>();
            protected string prompt_value;

            public override void Context()
            {
                prompt_value = "hi";

                InteractivePrompt.initialize_with(new Lazy<IConsole>(() => console.Object));
          
                choices.Add("yes");
                choices.Add("no");
            }

            public void should_have_called_Console_ReadLine()
            {
                console.Verify(c => c.ReadLine(), Times.AtLeastOnce);
            }
        }

        public class when_prompting_with_interactivePrompt : InteractivePromptSpecsBase
        {
            private string default_choice = null;
            private Func<string> prompt;

            public override void Because()
            {
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, default_choice, requireAnswer: false);
            }

            [Fact]
            public void should_error_when_the_choicelist_is_null()
            {
                choices = null;
                bool errored = false;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                try
                {
                    prompt();
                }
                catch (Exception)
                {
                    errored = true;
                }

                errored.ShouldBeTrue();
                console.Verify(c=> c.ReadLine(),Times.Never);
            }

            [Fact]
            public void should_error_when_the_prompt_input_is_null()
            {
                prompt_value = null;
                bool errored = false;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                try
                {
                    prompt();
                }
                catch (Exception)
                {
                    errored = true;
                }

                errored.ShouldBeTrue();
                console.Verify(c => c.ReadLine(), Times.Never);
            }  
            
            [Fact]
            public void should_error_when_the_default_choice_is_not_in_list()
            {
                default_choice = "maybe";
                bool errored = false;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                string result = null;
                try
                {
                   result = prompt();
                }
                catch (Exception)
                {
                    errored = true;
                }

                result.ShouldNotEqual("maybe");
                errored.ShouldBeTrue();
                console.Verify(c => c.ReadLine(), Times.Never);
            }
        }

        public class when_prompting_with_interactivePrompt_without_default_and_answer_is_not_required : InteractivePromptSpecsBase
        {
            private Func<string> prompt;
            public override void Because()
            {
                prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, null, requireAnswer: false);
            }

            public override void AfterObservations()
            {
                base.AfterObservations();
                should_have_called_Console_ReadLine();
            }

            [Fact]
            public void should_return_null_when_no_answer_given()
            {
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                var result = prompt();<commit_msg>(specs) adding to InteractivePrompt behaviors<commit_after>﻿namespace chocolatey.tests.infrastructure.commandline
{
    using System;
    using System.Collections.Generic;
    using Moq;
    using Should;
    using chocolatey.infrastructure.adapters;
    using chocolatey.infrastructure.commandline;

    public class InteractivePromptSpecs
    {
        public abstract class InteractivePromptSpecsBase : TinySpec
        {
            protected Mock<IConsole> console = new Mock<IConsole>();
            protected IList<string> choices = new List<string>();
            protected string prompt_value;

            public override void Context()
            {
                prompt_value = "hi";

                InteractivePrompt.initialize_with(new Lazy<IConsole>(() => console.Object));
          
                choices.Add("yes");
                choices.Add("no");
            }

            public void should_have_called_Console_ReadLine()
            {
                console.Verify(c => c.ReadLine(), Times.AtLeastOnce);
            }
        }

        public class when_prompting_with_interactivePrompt : InteractivePromptSpecsBase
        {
            private string default_choice = null;
            private Func<string> prompt;

            public override void Because()
            {
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, default_choice, requireAnswer: false);
            }

            [Fact]
            public void should_error_when_the_choicelist_is_null()
            {
                choices = null;
                bool errored = false;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                try
                {
                    prompt();
                }
                catch (Exception)
                {
                    errored = true;
                }

                errored.ShouldBeTrue();
                console.Verify(c=> c.ReadLine(),Times.Never);
            }      
            
            [Fact]
            public void should_error_when_the_choicelist_is_empty()
            {
                choices = new List<string>();
                bool errored = false;
                string errorMessage = string.Empty;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                try
                {
                    prompt();
                }
                catch (Exception ex)
                {
                    errored = true;
                    errorMessage = ex.Message;
                }

                errored.ShouldBeTrue();
                errorMessage.ShouldContain("No choices passed in.");
                console.Verify(c=> c.ReadLine(),Times.Never);
            }

            [Fact]
            public void should_error_when_the_prompt_input_is_null()
            {
                choices = new List<string>{"bob"};
                prompt_value = null;
                bool errored = false;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                try
                {
                    prompt();
                }
                catch (Exception)
                {
                    errored = true;
                }

                errored.ShouldBeTrue();
                console.Verify(c => c.ReadLine(), Times.Never);
            }  
            
            [Fact]
            public void should_error_when_the_default_choice_is_not_in_list()
            {
                choices = new List<string> { "bob" };
                default_choice = "maybe";
                bool errored = false;
                string errorMessage = string.Empty;
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                string result = null;
                try
                {
                   result = prompt();
                }
                catch (Exception ex)
                {
                    errored = true;
                    errorMessage = ex.Message;
                }

                result.ShouldNotEqual("maybe");
                errored.ShouldBeTrue();
                errorMessage.ShouldEqual("Default choice value must be one of the given choices.");
                console.Verify(c => c.ReadLine(), Times.Never);
            }
        }

        public class when_prompting_with_interactivePrompt_without_default_and_answer_is_not_required : InteractivePromptSpecsBase
        {
            private Func<string> prompt;
            public override void Because()
            {
                prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, null, requireAnswer: false);
            }

            public override void AfterObservations()
            {
                base.AfterObservations();
                should_have_called_Console_ReadLine();
            }

            [Fact]
            public void should_return_null_when_no_answer_given()
            {
                console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
                var result = prompt();
{'fraction_non_alphanumeric': 0.08708740995591872, 'fraction_numerical': 0.0, 'mean_word_length': 1.3173891380169407, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2499194', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2125, 'n_words': 427}