From 91a3e2f91769c9732cf009aa42a7ab107b9cf3ec Mon Sep 17 00:00:00 2001 From: niksedk Date: Tue, 7 Aug 2012 10:15:11 +0000 Subject: [PATCH] Added unit test for completion of language files - thx Pimmetje :) git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1316 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Test/Test.csproj | 1 + src/Test/languageTest.cs | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/Test/languageTest.cs diff --git a/src/Test/Test.csproj b/src/Test/Test.csproj index 2694ac4b5..63f4f1e51 100644 --- a/src/Test/Test.csproj +++ b/src/Test/Test.csproj @@ -62,6 +62,7 @@ + diff --git a/src/Test/languageTest.cs b/src/Test/languageTest.cs new file mode 100644 index 000000000..1f1d1c579 --- /dev/null +++ b/src/Test/languageTest.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nikse.SubtitleEdit.Logic; + +namespace Test +{ + + /// + /// Summary description for languageTest + /// + [TestClass] + public class languageTest + { + /// + /// Load a list of currently existing languages + /// + public languageTest() + { + list = new List(); + if (Directory.Exists(Path.Combine(Configuration.BaseDirectory, "Languages"))) + { + foreach (string fileName in Directory.GetFiles(Path.Combine(Configuration.BaseDirectory, "Languages"), "*.xml")) + { + string cultureName = Path.GetFileNameWithoutExtension(fileName); + list.Add(cultureName); + } + } + list.Sort(); + } + + private TestContext testContextInstance; + private List list; //Store the list of existing languages + + /// + ///Gets or sets the test context which provides + ///information about and functionality for the current test run. + /// + public TestContext TestContext + { + get + { + return testContextInstance; + } + set + { + testContextInstance = value; + } + } + + #region Additional test attributes + // + // You can use the following additional attributes as you write your tests: + // + // Use ClassInitialize to run code before running the first test in the class + // [ClassInitialize()] + // public static void MyClassInitialize(TestContext testContext) { } + // + // Use ClassCleanup to run code after all tests in a class have run + // [ClassCleanup()] + // public static void MyClassCleanup() { } + // + // Use TestInitialize to run code before running each test + // [TestInitialize()] + // public void MyTestInitialize() { } + // + // Use TestCleanup to run code after each test has run + // [TestCleanup()] + // public void MyTestCleanup() { } + // + #endregion + + [TestMethod] + public void TestAllLanguageTranslationsExists() + { + Language defaultlang = new Language(); //Load the English version + defaultlang.General.TranslatedBy = "Translated by ..."; // to avoid assertion + + foreach (String cultureName in list) //Loop over all language files + { + //Load language + var reader = new System.IO.StreamReader(Path.Combine(Configuration.BaseDirectory, "Languages") + Path.DirectorySeparatorChar + cultureName + ".xml"); + Language lang = Language.Load(reader); + + //Loop over all field in language + checkFields(cultureName, defaultlang, lang, defaultlang.GetType().GetFields()); + + checkProperty(cultureName, defaultlang, lang, defaultlang.GetType().GetProperties()); + + //If u want to save a kind of fixed lang file + //Disabled the assert fail function for it! + // lang.Save("Languagesnew\\" + cultureName + ".xml"); + reader.Close(); + } + } + + private void checkFields(string cultureName, object completeLang, object cultureLang, FieldInfo[] fields) + { + foreach (FieldInfo fieldInfo in fields) + { + if (fieldInfo.IsPublic && fieldInfo.FieldType.Namespace.Equals("Nikse.SubtitleEdit.Logic")) { + object completeLangatt = fieldInfo.GetValue(completeLang); + object cultureLangatt = fieldInfo.GetValue(cultureLang); + + if ((cultureLangatt == null) || (completeLangatt == null)) + { + Assert.Fail(fieldInfo.Name + " is mssing"); + } + //Console.Out.WriteLine("Field: " + fieldInfo.Name + " checked of type:" + fieldInfo.FieldType.FullName); + if (!fieldInfo.FieldType.FullName.Equals("System.String")) + { + checkFields(cultureName, completeLang, cultureLang, fieldInfo.FieldType.GetFields()); + checkProperty(cultureName, completeLangatt, cultureLangatt, fieldInfo.FieldType.GetProperties()); + } + else + { + Assert.Fail("no expecting a string here"); + } + } + } + } + + private void checkProperty(string cultureName, object completeLang, object cultureLang, PropertyInfo[] properties) + { + foreach (PropertyInfo propertie in properties) + { + if (propertie.CanRead && propertie.Name != "HelpFile") + { + object completeLangValue = propertie.GetValue(completeLang, null); + object cultureLangValue = propertie.GetValue(cultureLang, null); + //If the translated version is null there is a error (also the english version is not allowed to be null) + if ((cultureLangValue == null) || (completeLangValue == null) || (String.IsNullOrWhiteSpace(completeLangValue.ToString()))) + { + Assert.Fail(propertie.Name + " is mssing in language " + cultureName); + propertie.SetValue(cultureLang, completeLangValue, null); + //Console.Out.WriteLine(propertie.Name + " inserted"); + } + //Console.Out.WriteLine("propertie: " + propertie.Name + " checked of type:" + propertie.PropertyType.FullName); + if (!propertie.PropertyType.FullName.Equals("System.String")) + { + checkFields(cultureName, completeLangValue, cultureLangValue, propertie.PropertyType.GetFields()); + checkProperty(cultureName, completeLangValue, cultureLangValue, propertie.PropertyType.GetProperties()); + } + } + } + } + } + +}