Exemplo n.º 1
0
        public ActionResult Post([FromBody] AnagramModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(model));
                }

                var path = System.IO.Path.Combine(_hostingEnvironment.WebRootPath, "dict", $"{model.Language}-dict.txt");
                if (!System.IO.File.Exists(path))
                {
                    return(NotFound($"Dictionary {model.Language} file doesn't exist"));
                }

                var result = _permutationsService.GetByString(model.Value);
                var dict   = System.IO.File.ReadAllLines(path).ToHashSet();
                result = _permutationsService.FilterByDictionary(result, dict);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 2
0
        public void GetAnagram_ReturnsString_String()
        {
            AnagramModel anagramVar = new AnagramModel();
            string       userInput  = "chatmandu";
            string       testInput  = "hatcl";

            Assert.AreEqual(true, anagramVar.CheckAnagram(userInput, testInput));
        }
        public void PostInvalidValueTestMethod()
        {
            // Arrange

            // Act
            var model = new AnagramModel {
                Value = "~!@#$%^&*()_+`1234567890-=,./?\\|", Language = Language.RU
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            var badRequestObjectResult = result as BadRequestObjectResult;

            Assert.AreEqual(badRequestObjectResult.StatusCode, 400);
            Assert.AreEqual("Invalid string value", badRequestObjectResult.Value);
        }
        public void PostNullLanguageTestMethod()
        {
            // Arrange

            // Act
            var model = new AnagramModel {
                Value = "some"
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));
            var notFoundObjectResult = result as NotFoundObjectResult;

            Assert.AreEqual(notFoundObjectResult.StatusCode, 404);
            Assert.AreEqual("Dictionary 0 file doesn't exist", notFoundObjectResult.Value);
        }
        public void PostNullValueTestMethod()
        {
            // Arrange

            // Act
            var model = new AnagramModel {
                Value = null, Language = Language.EN
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            var badRequestObjectResult = result as BadRequestObjectResult;

            Assert.AreEqual(badRequestObjectResult.StatusCode, 400);
            Assert.AreEqual("Input string value can't be empty", badRequestObjectResult.Value);
        }
        public void PostValueAndUseInvalidDictTestMethod()
        {
            // Arrange
            var expected = 0;

            // Act
            var model = new AnagramModel {
                Value = "воз", Language = Language.EN
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            var okObjectResult = result as OkObjectResult;

            Assert.AreEqual(okObjectResult.StatusCode, 200);
            var actual = okObjectResult.Value as List <string>;

            Assert.AreEqual(expected, actual.Count);
        }
        public void PostValueAndUseEnDictTestMethod()
        {
            // Arrange
            var expected = new string[] { "paper", "rappe" };

            // Act
            var model = new AnagramModel {
                Value = "paper", Language = Language.EN
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            var okObjectResult = result as OkObjectResult;

            Assert.AreEqual(okObjectResult.StatusCode, 200);
            var actual = okObjectResult.Value as List <string>;

            CollectionAssert.AreEqual(expected, actual);
        }
        public void PostValueAndUseRuDictTestMethod()
        {
            // Arrange
            var expected = new string[] { "автор", "втора", "тавро", "товар", "отвар", "рвота" };

            // Act
            var model = new AnagramModel {
                Value = "автор", Language = Language.RU
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            var okObjectResult = result as OkObjectResult;

            Assert.AreEqual(okObjectResult.StatusCode, 200);
            var actual = okObjectResult.Value as List <string>;

            CollectionAssert.AreEqual(expected, actual);
        }
        public void PostValueWithDashTestMethod()
        {
            // Arrange
            var expected = new string[] { "лось" };

            // Act
            var model = new AnagramModel {
                Value = "соль-сахар", Language = Language.RU
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            var okObjectResult = result as OkObjectResult;

            Assert.AreEqual(okObjectResult.StatusCode, 200);
            var actual = okObjectResult.Value as List <string>;

            CollectionAssert.AreEqual(expected, actual);;
        }
Exemplo n.º 10
0
        public void PostMultyWordValueTestMethod()
        {
            // Arrange
            var expected = new string[] { "лось" };

            // Act
            var model = new AnagramModel {
                Value = "соль сахар ~!@#$%^&*()_+`1234567890-=,./?\\|", Language = Language.RU
            };
            var result = _controller.Post(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            var okObjectResult = result as OkObjectResult;

            Assert.AreEqual(okObjectResult.StatusCode, 200);
            var actual = okObjectResult.Value as List <string>;

            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 11
0
 public void GetAnagrams_SameChars_true()
 {
     Assert.AreEqual(true, AnagramModel.IsAnagram("Beard", "bread"));
 }
Exemplo n.º 12
0
 public void GetAnagrams_SameLength_true()
 {
     Assert.AreEqual(true, AnagramModel.IsAnagram("Beard", "Bread"));
 }