Ejemplo n.º 1
0
        private static async Task <Dictionary <string, object> > GetPromptResults(string originPath)
        {
            var prompts = await PromptReader
                          .GetPromptsFromFile(originPath)
                          .ConfigureAwait(false);

            return(ConsolePromptReader.WritePrompts(prompts.ToList()));
        }
        public async Task GivenAnEmptyString_WhenGetPromptsFromFileIsCalled_ThenAnArgumentExceptionIsThrown()
        {
            //arrange

            //act
            await Assert.ThrowsAsync <ArgumentException>(
                () => PromptReader.GetPromptsFromFile(string.Empty))
            .ConfigureAwait(false);

            //assert
        }
        public async Task GivenAJsonFileWithMultipleValidPrompts_AndADifferentFilename_WhenGetPromptsFromFileIsCalled_ThenAFileNotFoundExceptionWillBeThrown()
        {
            //arrange
            var          filename   = Guid.NewGuid().ToString();
            var          file       = Path.Join(TempPath, filename);
            const string jsonString = VALID_MULTIPLE_PROMPTS;
            await File.WriteAllTextAsync(file, jsonString).ConfigureAwait(false);

            //act
            await Assert.ThrowsAsync <FileNotFoundException>(
                () => PromptReader.GetPromptsFromFile(TempPath))
            .ConfigureAwait(false);
        }
        public async Task GivenAJsonStringWithAPromptWithAWhenArray_WhenGetPromptsFromFileIsCalled_ThenTheResultPromptWillHaveTheGivenWhenArray(object value, string jsonValue)
        {
            //arrange
            var expectedWhen = new PromptWhen
            {
                Id = "question",
                Is = value
            };
            var expectedPrompt = new TemplatePrompt
            {
                Id           = "BooleanPromptId",
                Message      = "Boolean Prompt Message",
                DefaultValue = false,
                When         = new List <PromptWhen>
                {
                    expectedWhen
                }
            };
            var jsonString = @"
[
	{
		""promptType"": ""Boolean"",
		""id"": ""BooleanPromptId"",
		""message"": ""Boolean Prompt Message"",
		""when"": [{
			""id"": ""question"",
			""is"": "             + jsonValue + @"
		}]
	}
]";
            await File.WriteAllTextAsync(TempFile, jsonString).ConfigureAwait(false);

            //act
            var result = await PromptReader.GetPromptsFromFile(TempPath).ConfigureAwait(false);

            //assert
            Assert.Single(result);
            var actualPrompt = result.First();

            Assert.Equal(expectedPrompt.When.Count, actualPrompt.When.Count);
            var actualWhen = actualPrompt.When[0];

            Assert.Equal(expectedWhen.Id, actualWhen.Id);
            Assert.Equal(expectedWhen.Is, actualWhen.Is);
        }
        public async Task GivenAJsonFileWithMultipleValidPrompts_WhenGetPromptsFromFileIsCalled_ThenTheResultWillHaveTheCorrentAmountsOfPrompts()
        {
            //arrange
            const int    expectedCount       = 6;
            const int    expectedStringCount = 2;
            const int    expectedIntCount    = 3;
            const string jsonString          = VALID_MULTIPLE_PROMPTS;
            await File.WriteAllTextAsync(TempFile, jsonString).ConfigureAwait(false);

            //act
            var actual = await PromptReader.GetPromptsFromFile(TempPath)
                         .ConfigureAwait(false);

            //assert
            Assert.Equal(expectedCount, actual.Count());
            Assert.Single(actual.Where(e => e.PromptType == PromptType.Boolean));
            Assert.Equal(expectedStringCount, actual.Count(e => e.PromptType == PromptType.String));
            Assert.Equal(expectedIntCount, actual.Count(e => e.PromptType == PromptType.Int));
        }
        public async Task GivenAJsonFileWithNoMessage_WhenGetPromptsFromFileIsCalled_ThenTheResultWillThrowAValidationException()
        {
            //arrange
            const string jsonString = @"
[
	{
		""promptType"": ""Boolean"",
		""id"": ""BooleanPromptId"",
		""message"": """"
	}
]";
            await File.WriteAllTextAsync(TempFile, jsonString).ConfigureAwait(false);

            //act
            await Assert.ThrowsAsync <ValidationException>(
                () => PromptReader.GetPromptsFromFile(TempPath))
            .ConfigureAwait(false);

            //assert
        }