[TestCase("TestDataLangs.xml", "en", "английский")] // верно ли задан атрибут?
        public void LangByKey_ValidXmlResponse_WorksCorrectly(string testDataFile, string key, string expect)
        {
            ITranslateAPI fakeYandexAPI = Substitute.For <ITranslateAPI>();
            string        responseLangs = File.ReadAllText(testDataFile);

            fakeYandexAPI.GetLangs(Arg.Any <string>(), Arg.Any <string>()).Returns(responseLangs);

            ITranslatorModel model = new TranslatorModel("api_key", "ui", fakeYandexAPI);

            var l = model.LangByKey(key);

            Assert.AreEqual(l.Value, expect);
        }
        public void LangsProperty_ValidXmlResponse_ContainsSomething(string testDataFile)
        {
            ITranslateAPI fakeYandexAPI = Substitute.For <ITranslateAPI>();
            string        responseLangs = File.ReadAllText(testDataFile);

            fakeYandexAPI.GetLangs(Arg.Any <string>(), Arg.Any <string>()).Returns(responseLangs);

            ITranslatorModel model = new TranslatorModel("api_key", "ui", fakeYandexAPI);

            var langs = model.Langs;

            Assert.IsTrue(model.Langs.Count > 0);
        }
示例#3
0
        private Dictionary <string, string> GetLangs()
        {
            Dictionary <string, string> langs = new Dictionary <string, string>();
            XmlDocument document = new XmlDocument();
            var         xml      = _api.GetLangs(ApiKey, UiLang);

            document.LoadXml(xml);
            foreach (XmlElement e in document.GetElementsByTagName("Error"))
            {
                throw new Exception(e.Attributes["message"].Value);
            }
            foreach (XmlElement e in document.GetElementsByTagName("Item"))
            {
                langs.Add(e.Attributes["key"].Value, e.Attributes["value"].Value);
            }
            return(langs);
        }
        public void LangsProperty_ErrorXmlResponse_ThrowsExWithErrorMessage(string testDataFile)
        {
            ITranslateAPI fakeYandexAPI = Substitute.For <ITranslateAPI>();
            XmlDocument   doc           = new XmlDocument();
            string        responseLangs = File.ReadAllText(testDataFile);

            doc.LoadXml(responseLangs);

            fakeYandexAPI.GetLangs(Arg.Any <string>(), Arg.Any <string>()).Returns(responseLangs);

            ITranslatorModel model = new TranslatorModel("api_key", "ui", fakeYandexAPI);

            Exception ex = Assert.Catch <Exception>(() => model.Langs.ToString());
            string    expectedMessage = doc.LastChild.Attributes["message"].Value;

            StringAssert.Contains(expectedMessage, ex.Message);
        }
        public void DetectLang_ValidXmlResponse_WorksCorrectly(
            string text,
            string testDataFile,
            string testDataLangsFile,
            string expect)
        {
            ITranslateAPI fakeYandexAPI  = Substitute.For <ITranslateAPI>();
            string        responseLangs  = File.ReadAllText(testDataLangsFile);
            string        responseDetect = File.ReadAllText(testDataFile);

            fakeYandexAPI.GetLangs(Arg.Any <string>(), Arg.Any <string>()).Returns(responseLangs);
            fakeYandexAPI.Detect(Arg.Any <string>(), text).Returns(responseDetect);

            ITranslatorModel model = new TranslatorModel("api_key", "ui", fakeYandexAPI);

            string actual = model.DetectLang(text).Key;

            Assert.AreEqual(expect, actual);
        }
        public void Translate_ValidXmlResponse_WorksCorrectly(
            string text,
            string sourceLang,
            string testDataFile,
            string testDetectDataFile,
            string testDataLangsFile,
            string expect)
        {
            ITranslateAPI fakeYandexAPI     = Substitute.For <ITranslateAPI>();
            string        responseLangs     = File.ReadAllText(testDataLangsFile);
            string        responseTranslate = File.ReadAllText(testDataFile);
            string        responseDetect    = File.ReadAllText(testDetectDataFile);

            fakeYandexAPI.GetLangs(Arg.Any <string>(), Arg.Any <string>()).Returns(responseLangs);
            fakeYandexAPI.Detect(Arg.Any <string>(), text).Returns(responseDetect);
            fakeYandexAPI.Translate(Arg.Any <string>(), text, sourceLang).Returns(responseTranslate);

            ITranslatorModel model = new TranslatorModel("api_key", "ui", fakeYandexAPI);

            model.SetTargetLang(model.LangByKey(sourceLang.Split('-')[1]));
            string actual = model.Translate(text);

            Assert.AreEqual(expect, actual, "Model.Translate() works.");
        }