view Lang/TranslationTest.cs @ 45:7502653a71a0

* Update tests - we now fix Microsoft's lack of capitalisation in language names * Change to not passing schema location to TranslationXmlLoader * Remove schema from test data
author IBBoard <dev@ibboard.co.uk>
date Sun, 24 Jun 2012 15:50:45 +0100
parents 31fdc90f3556
children
line wrap: on
line source

// 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 LGPL, 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/TranslationTests";
		
		[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);
		}
		
		[Test()]
		public void TestResetDoesNotCauseErrors()
		{
			Assert.AreEqual("++ Missing Translation missingID ++", Translation.GetTranslation("missingID"));
		}
		
		[Test()]
		public void TestLoadedListOfLanguageNames()
		{
			Translation.InitialiseTranslations(TEST_DATA_PATH);
			Assert.AreEqual(2, Translation.GetLanguages().Count);
			Assert.That(Translation.GetLanguages().Contains(new TranslationLanguage("en")));
			Assert.That(Translation.GetLanguages().Contains(new TranslationLanguage("it")));
		}
		
		[Test()]
		public void TestTranslationChangedEventFiresOnChange()
		{
			Translation.InitialiseTranslations(TEST_DATA_PATH);
			MethodFireObject obj = new MethodFireObject();
			Translation.TranslationChanged+= obj.Method;
			Translation.LoadTranslation("en");
			Assert.IsTrue(obj.methodFired);
			obj.methodFired = false;
			Translation.LoadTranslation("it");
			Assert.IsTrue(obj.methodFired);
		}
		
		[Test()]
		public void TestTranslationChangedEventNotFiredOnChangeToSameValue()
		{
			Translation.InitialiseTranslations(TEST_DATA_PATH);
			Translation.LoadTranslation("en");
			MethodFireObject obj = new MethodFireObject();
			Translation.TranslationChanged+= obj.Method;
			Translation.LoadTranslation("en");
			Assert.IsFalse(obj.methodFired);
		}
		
		[Test()]
		public void TestTranslationChangedEventFiredOnReset()
		{
			Translation.InitialiseTranslations(TEST_DATA_PATH);
			Translation.LoadTranslation("en");
			MethodFireObject obj = new MethodFireObject();
			Translation.TranslationChanged+= obj.Method;
			Translation.Reset();
			Assert.IsTrue(obj.methodFired);
		}
		
		private class MethodFireObject
		{
			public bool methodFired = false;
			
			public void Method()
			{
				methodFired = true;
			}
		}
	}
}