Mercurial > repos > IBBoard.Tests
changeset 14:15cf2fe2a627
Re #30: Improve Translations API
* Add translations tests and test data
* Add reset method to translations
* Fix translations where API documentation (contract) didn't match behaviour
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Apr 2010 14:40:35 +0000 |
parents | f6fd30d3b817 |
children | e3e4f7a92f8b |
files | IBBoard.Tests.csproj Lang/Mock/MockTranslatable.cs Lang/TranslationTest.cs test-data/schemas/translation.xsd test-data/translations/en.translation test-data/translations/it.translation |
diffstat | 6 files changed, 158 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.Tests.csproj Mon Nov 30 21:00:41 2009 +0000 +++ b/IBBoard.Tests.csproj Tue Apr 06 14:40:35 2010 +0000 @@ -36,6 +36,8 @@ <Compile Include="Limits\UnlimitedLimitTest.cs" /> <Compile Include="Xml\XmlToolsTests.cs" /> <Compile Include="EnumToolsTests.cs" /> + <Compile Include="Lang\TranslationTest.cs" /> + <Compile Include="Lang\Mock\MockTranslatable.cs" /> </ItemGroup> <ItemGroup> <Reference Include="System" /> @@ -48,6 +50,11 @@ <Folder Include="CustomMath\" /> <Folder Include="Limits\" /> <Folder Include="Xml\" /> + <Folder Include="Lang\" /> + <Folder Include="test-data\" /> + <Folder Include="test-data\translations\" /> + <Folder Include="test-data\schemas\" /> + <Folder Include="Lang\Mock\" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\IBBoard\IBBoard.csproj"> @@ -64,4 +71,18 @@ </Properties> </MonoDevelop> </ProjectExtensions> + <ItemGroup> + <None Include="test-data\translations\en.translation"> + <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + <None Include="test-data\translations\it.translation"> + <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + <None Include="test-data\schemas\translation.xsd"> + <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + </ItemGroup> </Project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/Mock/MockTranslatable.cs Tue Apr 06 14:40:35 2010 +0000 @@ -0,0 +1,43 @@ +// This file (MockTranslatable.cs) is a part of the IBBoard.Tests project and is copyright 2010 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; + +namespace IBBoard.Lang.Mock +{ + public class MockTranslatable : ITranslatable + { + private string name; + private string text; + + public MockTranslatable(string mockName) + { + name = mockName; + } + + public string Name + { + get + { + return name; + } + set + { + name = value; + } + } + + public string Text + { + get + { + return text; + } + set + { + text = value; + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/TranslationTest.cs Tue Apr 06 14:40:35 2010 +0000 @@ -0,0 +1,68 @@ +// This file (TranslationTest.cs) is a part of the IBBoard.Tests project and is copyright 2010 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using NUnit.Framework; +using IBBoard.Lang.Mock; + +namespace IBBoard.Lang +{ + [TestFixture()] + public class TranslationTest + { + private const string TEST_ID = "testString"; + private const string TEST_DATA_PATH = "test-data"; + + [SetUp()] + public void Setup() + { + Translation.Reset(); + } + + [Test()] + public void TestMissingTranslationGivesMissingMessage() + { + Assert.AreEqual("++ Missing Translation missingID ++", Translation.GetTranslation("missingID")); + Assert.AreEqual("++ Missing Translation missingID ++", Translation.GetTranslation("missingID", false)); + string defaultMessage = "default message"; + Assert.AreEqual(defaultMessage, Translation.GetTranslation("missingID", defaultMessage)); + Assert.IsNull(Translation.GetTranslation("missingID", true)); + } + + [Test()] + public void TestInitialiseTranslations() + { + Assert.AreEqual("++ Missing Translation "+TEST_ID+" ++", Translation.GetTranslation(TEST_ID)); + Translation.InitialiseTranslations(TEST_DATA_PATH, "en"); + Assert.AreEqual("Test String", Translation.GetTranslation(TEST_ID)); + Assert.AreEqual("en", Translation.GetTranslationLanguage()); + Translation.InitialiseTranslations(TEST_DATA_PATH, "it"); + Assert.AreEqual("Fakius Romanius", Translation.GetTranslation(TEST_ID)); + Assert.AreEqual("it", Translation.GetTranslationLanguage()); + } + + [Test()] + public void TestLoadTranslations() + { + Assert.AreEqual("++ Missing Translation "+TEST_ID+" ++", Translation.GetTranslation(TEST_ID)); + Translation.InitialiseTranslations(TEST_DATA_PATH, "en"); + Assert.AreEqual("Test String", Translation.GetTranslation(TEST_ID)); + Assert.AreEqual("en", Translation.GetTranslationLanguage()); + Translation.LoadTranslation("it"); + Assert.AreEqual("Fakius Romanius", Translation.GetTranslation(TEST_ID)); + Assert.AreEqual("it", Translation.GetTranslationLanguage()); + } + + [Test()] + public void TestITranslationTranslation() + { + MockTranslatable translatable = new MockTranslatable(TEST_ID); + Translation.Translate(translatable); + Assert.AreEqual("++ Missing Translation "+TEST_ID+" ++", translatable.Text); + Translation.InitialiseTranslations(TEST_DATA_PATH, "en"); + Translation.Translate(translatable); + Assert.AreEqual("Test String", translatable.Text); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/schemas/translation.xsd Tue Apr 06 14:40:35 2010 +0000 @@ -0,0 +1,18 @@ +<?xml version="1.0"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ibboard.co.uk/translation" xmlns="http://ibboard.co.uk/translation" elementFormDefault="qualified"> +<xs:complexType name="translationtype"> + <xs:simpleContent> + <xs:extension base="xs:string"> + <xs:attribute name="id" type="xs:ID" /> + </xs:extension> + </xs:simpleContent> +</xs:complexType> +<xs:element name="translations"> + <xs:complexType> + <xs:sequence> + <xs:element name="translation" minOccurs="0" maxOccurs="unbounded" type="translationtype"/> + </xs:sequence> + <xs:attribute name="lang" type="xs:string" use="required"/> + </xs:complexType> +</xs:element> +</xs:schema> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/translations/en.translation Tue Apr 06 14:40:35 2010 +0000 @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<translations xmlns="http://ibboard.co.uk/translation" lang="en"> + <translation id="testString">Test String</translation> +</translations> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/translations/it.translation Tue Apr 06 14:40:35 2010 +0000 @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<translations xmlns="http://ibboard.co.uk/translation" lang="en"> + <translation id="testString">Fakius Romanius</translation> +</translations> \ No newline at end of file