[gbrainy] Some more unit testing and some decoupling



commit 2fe0bdb7b6038cdd22b2f676b720953d5f9f3802
Author: Jordi Mas <jmas softcatala org>
Date:   Thu Jul 22 20:09:35 2010 +0200

    Some more unit testing and some decoupling

 Makefile.am                                       |    1 -
 src/Clients/Classical/Defines.cs.in               |    1 +
 src/Clients/Classical/Dialogs/HigMessageDialog.cs |    1 -
 src/Clients/Classical/gbrainy.cs                  |    9 ++
 src/Core/Main/GameManager.cs                      |  128 +++++++++++----------
 tests/Core/GameManagerTest.cs                     |   46 ++++++--
 tests/Core/GameSessionTest.cs                     |   15 ++-
 tests/Core/GameXmlFactoryTest.cs                  |   60 ++++++++++
 tests/Makefile.am                                 |   13 ++-
 tests/README                                      |    2 +-
 tests/test_games.xml                              |   16 +++
 11 files changed, 212 insertions(+), 80 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 886abef..4e386ed 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,7 +7,6 @@ SUBDIRS = 		\
 	help
 
 
-
 CLEANFILES =			\
 	intltool-extract	\
 	intltool-update		\
diff --git a/src/Clients/Classical/Defines.cs.in b/src/Clients/Classical/Defines.cs.in
index b10c717..7eee6ba 100644
--- a/src/Clients/Classical/Defines.cs.in
+++ b/src/Clients/Classical/Defines.cs.in
@@ -25,6 +25,7 @@ namespace gbrainy.Clients.Classical
 	{
 		public const string VERSION = "@VERSION@";
 		public const string GNOME_LOCALE_DIR = "@prefix@/share/locale";
+		public const string DATA_DIR = "@prefix@/share/games/gbrainy/";
 		public const string COPYRIGHT = "@COPYRIGHT@";
 		public const string BUILD_TIME = "@BUILD_TIME@";
 	}
diff --git a/src/Clients/Classical/Dialogs/HigMessageDialog.cs b/src/Clients/Classical/Dialogs/HigMessageDialog.cs
index 64b9548..b7d002e 100644
--- a/src/Clients/Classical/Dialogs/HigMessageDialog.cs
+++ b/src/Clients/Classical/Dialogs/HigMessageDialog.cs
@@ -28,7 +28,6 @@ namespace gbrainy.Clients.Classical
 	{
 		Gtk.AccelGroup accel_group;
 		Gtk.VBox extra_widget_vbox;
-		Gtk.Widget extra_widget;
 		Gtk.Image image;
 
 		public HigMessageDialog (Gtk.Window parent,
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index ab8a3d9..fe80173 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -90,7 +90,16 @@ namespace gbrainy.Clients.Classical
 
 		public void Initialize ()
 		{
+			const string ASSEMBLY = "gbrainy.Games.dll";
+
 			session = new GameSession ();
+			
+			session.GameManager.LoadAssemblyGames (ASSEMBLY);
+			session.GameManager.LoadPlugins ();
+			session.GameManager.LoadGamesFromXml (System.IO.Path.Combine (Defines.DATA_DIR, "games.xml"));
+			session.GameManager.ShowGamesSummary ();
+
+			session.GameManager.ColorBlind = Preferences.GetBoolValue (Preferences.ColorBlindKey);
 			session.DrawRequest += SessionDrawRequest;
 			session.UpdateUIElement += SessionUpdateUIElement;
 			session.SynchronizingObject = new GtkSynchronize ();
diff --git a/src/Core/Main/GameManager.cs b/src/Core/Main/GameManager.cs
index a461199..f50d8e2 100644
--- a/src/Core/Main/GameManager.cs
+++ b/src/Core/Main/GameManager.cs
@@ -66,11 +66,9 @@ namespace gbrainy.Core.Main
 			}
 		}
 
-		bool once;
 		GameSession.Types game_type;
 		IEnumerator <int> enumerator;
-		GameDifficulty difficulty;
-		GamesXmlFactory xml_games;
+		GameDifficulty difficulty;		
 
 		List <GameLocator> available_games; 	// List of all available games in the system
 		List <int> play_list;  		// Play list for the Selected difficulty, game types
@@ -85,25 +83,10 @@ namespace gbrainy.Core.Main
 			cnt_logic = cnt_memory = cnt_calculation = cnt_verbal = 0;
 			RandomOrder = true;
 
-			xml_games = new GamesXmlFactory ();
-			xml_games.Read (System.IO.Path.Combine (Defines.DATA_DIR, "games.xml"));
-
-			LoadAssemblyGames ();
-
 			// Load Analogies
 			cnt_verbal += AddVerbalGamesAndVariations (VerbalAnalogiesInternal);
 
-			// Load defined XML games
-			LoadXmlGames ();
-
 			LoadPlugins ();
-
-			if (once == false) {
-				once = true;
-				Console.WriteLine (Catalog.GetString ("Games registered: {0}: {1} logic puzzles, {2} calculation trainers, {3} memory trainers, {4} verbal analogies"),
-					cnt_logic + cnt_memory + cnt_calculation + cnt_verbal,
-					cnt_logic, cnt_calculation, cnt_memory, cnt_verbal);
-			}
 #if PDF_DUMP
 			GeneratePDF ();
 #endif
@@ -173,15 +156,16 @@ namespace gbrainy.Core.Main
 		// Indicates if the PlayList for CustomGames is delivered in RandomOrder
  		public bool RandomOrder { get; set; }
 
+		public bool ColorBlind { get; set; }
+
 		// Returns all the games available for playing
 		public GameLocator [] AvailableGames {
 			get { return available_games.ToArray (); }
 		}
 
 		// Dynamic load of the gbrainy.Games.Dll assembly
-		void LoadAssemblyGames ()
+		public void LoadAssemblyGames (string file)
 		{
-			const string ASSEMBLY = "gbrainy.Games.dll";
 			const string CLASS = "gbrainy.Games.GameList";
 			const string LOGIC_METHOD = "LogicPuzzles";
 			const string CALCULATION_METHOD = "CalculationTrainers";
@@ -198,7 +182,7 @@ namespace gbrainy.Core.Main
 				Assembly asm = Assembly.GetExecutingAssembly ();
 				string asm_dir = System.IO.Path.GetDirectoryName (asm.Location);
 
-				asem = Assembly.LoadFrom (System.IO.Path.Combine (asm_dir, ASSEMBLY));
+				asem = Assembly.LoadFrom (System.IO.Path.Combine (asm_dir, file));
 
 				foreach (Type t in asem.GetTypes())
 				{
@@ -230,45 +214,15 @@ namespace gbrainy.Core.Main
 			}
 		}
 
-		// Adds all the games and its variants into the available games list
-		int AddGamesAndVariations (Type [] types)
-		{
-			Game game;
-			int cnt = 0;
-
-			foreach (Type type in types)
-			{
-				game = (Game) Activator.CreateInstance (type, true);
-				for (int i = 0; i < game.Variants; i++)
-				{
-					available_games.Add (new GameLocator (type, i, game.Type, game.Variants == 1));
-				}
-				cnt += game.Variants;
-			}
-			return cnt;
-		}
-
-		// Adds all the games and its variants into the available games list
-		int AddVerbalGamesAndVariations (Type [] types)
+		// XML are stored using the Variant as a pointer to the game + the internal variant
+		public void LoadGamesFromXml (string file)
 		{
-			Game game;
-			int cnt = 0;
+			// Load defined XML games
+			GamesXmlFactory xml_games;
 
-			foreach (Type type in types)
-			{
-				game = (Game) Activator.CreateInstance (type, true);
-				for (int i = 0; i < game.Variants; i++)
-				{
-					available_games.Add (new GameLocator (type, i, game.Type, true));
-				}
-				cnt += game.Variants;
-			}
-			return cnt;
-		}
+			xml_games = new GamesXmlFactory ();
+			xml_games.Read (file);
 
-		// XML are stored using the Variant as a pointer to the game + the internal variant
-		void LoadXmlGames ()
-		{
 			Type type = typeof (GameXml);
 			int cnt = 0;
 
@@ -293,11 +247,10 @@ namespace gbrainy.Core.Main
 			}
 		}
 
-		void LoadPlugins ()
+		public void LoadPlugins ()
 		{
 
 	#if MONO_ADDINS
-
 			try {
 				ExtensionNodeList addins;
 				Game game;
@@ -305,7 +258,6 @@ namespace gbrainy.Core.Main
 				string dir = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "gbrainy");
 
 				AddinManager.Initialize (dir);
-				Console.WriteLine ("Pluggin database:" + dir);
 				AddinManager.Registry.Update (null);
 				new SetupService (AddinManager.Registry);
 
@@ -348,6 +300,60 @@ namespace gbrainy.Core.Main
 	#endif
 		}
 
+		// Unload previous assembly, xml and verbal analogies loaded games
+		public void ResetAvailableGames ()
+		{
+			available_games.Clear ();
+		}
+
+		public void ShowGamesSummary ()
+		{
+	#if MONO_ADDINS
+				Console.WriteLine ("Pluggin database:" +
+					System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "gbrainy"));
+	#endif
+			Console.WriteLine (Catalog.GetString ("Games registered: {0}: {1} logic puzzles, {2} calculation trainers, {3} memory trainers, {4} verbal analogies"),
+					cnt_logic + cnt_memory + cnt_calculation + cnt_verbal,
+					cnt_logic, cnt_calculation, cnt_memory, cnt_verbal);
+		}
+
+		// Adds all the games and its variants into the available games list
+		int AddGamesAndVariations (Type [] types)
+		{
+			Game game;
+			int cnt = 0;
+
+			foreach (Type type in types)
+			{
+				game = (Game) Activator.CreateInstance (type, true);
+				for (int i = 0; i < game.Variants; i++)
+				{
+					available_games.Add (new GameLocator (type, i, game.Type, game.Variants == 1));
+				}
+				cnt += game.Variants;
+			}
+			return cnt;
+		}
+
+		// Adds all the games and its variants into the available games list
+		int AddVerbalGamesAndVariations (Type [] types)
+		{
+			Game game;
+			int cnt = 0;
+
+			foreach (Type type in types)
+			{
+				game = (Game) Activator.CreateInstance (type, true);
+				for (int i = 0; i < game.Variants; i++)
+				{
+					available_games.Add (new GameLocator (type, i, game.Type, true));
+				}
+				cnt += game.Variants;
+			}
+			return cnt;
+		}
+
+
 		// Taking a GameLocator list builds the play_list
 		void BuildPlayList (List <GameLocator> all_games)
 		{
@@ -457,7 +463,7 @@ namespace gbrainy.Core.Main
 				if (puzzle.IsPlayable == false)
 					continue;
 
-				if ((Preferences.GetBoolValue (Preferences.ColorBlindKey) == true) && puzzle.UsesColors == true)
+				if (ColorBlind == true && puzzle.UsesColors == true)
 					continue;
 
 				if (first == null)
diff --git a/tests/Core/GameManagerTest.cs b/tests/Core/GameManagerTest.cs
index d797a37..d191a29 100644
--- a/tests/Core/GameManagerTest.cs
+++ b/tests/Core/GameManagerTest.cs
@@ -28,18 +28,18 @@ namespace gbrainyTest
 	[TestFixture]
 	public class GameManagerTest
 	{
+		GameManager manager;
+
 		[TestFixtureSetUp]
 		public void Construct ()
 		{
-
+			manager = new GameManager ();
 		}
 
 		//Lists the games without tip
 		public void GamesWithNoTip ()
 		{
 			int notip = 0;
-			GameManager manager = new GameManager ();
-
 			GameManager.GameLocator [] games = manager.AvailableGames;
 
 			foreach (GameManager.GameLocator locator in games)
@@ -59,7 +59,6 @@ namespace gbrainyTest
 		public void GamesNoDuplicatedName ()
 		{
 			Dictionary <string, bool> dictionary;
-			GameManager manager = new GameManager ();
 			GameManager.GameLocator [] games = manager.AvailableGames;
 			dictionary = new Dictionary <string, bool> (games.Length);
 
@@ -85,9 +84,7 @@ namespace gbrainyTest
 			GameManager.GameLocator [] games;
 
 			List <int> list = new List <int> ();
-			GameManager gm = new GameManager ();
-			gm.GameType = GameSession.Types.AllGames;
-			games = gm.AvailableGames;
+			games = manager.AvailableGames;
 
 			// Create a hash to map from game name to locator
 			dictionary = new Dictionary <int, string> (games.Length);
@@ -105,12 +102,12 @@ namespace gbrainyTest
 			Game current;
 			string name;
 
-			gm.RandomOrder = false;
-			gm.PlayList = list.ToArray ();
+			manager.RandomOrder = false;
+			manager.PlayList = list.ToArray ();
 
 			for (int i = 0; i < list.Count; i++)
 			{
-				current = gm.GetPuzzle ();
+				current = manager.GetPuzzle ();
 
 				try
 				{
@@ -123,5 +120,34 @@ namespace gbrainyTest
 				}
 			}
 		}
+
+		[Test]
+		public void ColorBlind ()
+		{
+			Game game;
+			GameManager.GameLocator [] games;
+
+			GameManager manager = new GameManager ();
+			manager.GameType = GameSession.Types.AllGames;
+			manager.ColorBlind = true;
+			games = manager.AvailableGames;
+
+			for (int i = 0; i < games.Length; i++)
+			{
+				game = (Game) manager.GetPuzzle ();
+				Assert.AreEqual (false, game.UsesColors);
+			}
+		}
+
+		[Test]
+		public void ResetAvailableGames ()
+		{
+			GameManager manager = new GameManager ();
+			manager.GameType = GameSession.Types.AllGames;
+			Assert.AreNotEqual (0, manager.AvailableGames.Length);
+
+			manager.ResetAvailableGames ();
+			Assert.AreEqual (0, manager.AvailableGames.Length);
+		}
 	}
 }
diff --git a/tests/Core/GameSessionTest.cs b/tests/Core/GameSessionTest.cs
index bc210a3..0935a20 100644
--- a/tests/Core/GameSessionTest.cs
+++ b/tests/Core/GameSessionTest.cs
@@ -33,11 +33,20 @@ namespace gbrainyTest
 		{
 
 		}
+		
+		GameSession PrepareSession ()
+		{
+			GameSession session = new GameSession ();
+			session.GameManager.LoadAssemblyGames ("gbrainy.Games.dll");
+			session.GameManager.LoadPlugins ();
+			session.GameManager.LoadGamesFromXml (System.IO.Path.Combine (gbrainy.Core.Main.Defines.DATA_DIR, "games.xml"));
+			return session;
+		}
 
 		[Test]
 		public void Status ()
 		{
-			GameSession session = new GameSession ();
+			GameSession session = PrepareSession ();
 			Assert.AreEqual (GameSession.SessionStatus.NotPlaying, session.Status);
 
 			session.Type = GameSession.Types.LogicPuzzles;
@@ -54,7 +63,7 @@ namespace gbrainyTest
 		[Test]
 		public void Paused ()
 		{
-			GameSession session = new GameSession ();
+			GameSession session = PrepareSession ();
 			session.Type = GameSession.Types.LogicPuzzles;
 			session.New ();
 			Assert.AreEqual (GameSession.SessionStatus.NotPlaying, session.Status);
@@ -69,7 +78,7 @@ namespace gbrainyTest
 		[Test]
 		public void ID ()
 		{
-			GameSession session = new GameSession ();
+			GameSession session = PrepareSession ();
 			session.Type = GameSession.Types.LogicPuzzles;
 			session.New ();
 			Assert.AreEqual (1, session.ID);
diff --git a/tests/Core/GameXmlFactoryTest.cs b/tests/Core/GameXmlFactoryTest.cs
new file mode 100644
index 0000000..c544225
--- /dev/null
+++ b/tests/Core/GameXmlFactoryTest.cs
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2010 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+
+using gbrainy.Core.Main;
+using gbrainy.Core.Main.Xml;
+
+namespace gbrainyTest
+{
+
+	[TestFixture]
+	public class GameXmlFactoryTest
+	{
+		[TestFixtureSetUp]
+		public void Construct ()
+		{
+
+		}
+
+		[Test]
+		public void BasicGameDefinition ()
+		{
+			GamesXmlFactory factory;
+			List <GameXmlDefinition> definitions;
+			GameXmlDefinition definition;
+
+			factory = new GamesXmlFactory ();
+			factory.Read ("test_games.xml");
+			definitions = factory.Definitions;
+
+			definition = definitions [0];
+			Assert.AreEqual ("Clock Rotation", definition.Name);
+			Assert.AreEqual (GameTypes.LogicPuzzle, definition.Type);
+			Assert.AreEqual (GameDifficulty.Medium | GameDifficulty.Master, definition.Difficulty);
+			Assert.AreEqual ("Rationale text", definition.Rationale.String);
+			Assert.AreEqual ("How many degrees rotates the minute hand of a clock?", definition.Question.String);
+			Assert.AreEqual ("How many degrees rotates the minute hand of a clocks?", definition.Question.PluralString);
+			Assert.AreEqual ("[rslt]", definition.Answer);
+		}
+	}
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9618916..988df11 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,11 +12,14 @@ CSFILES_CORE =					\
 	$(srcdir)/Core/PlayerPersonalRecordTest.cs \
 	$(srcdir)/Core/GameSessionTest.cs \
 	$(srcdir)/Core/GameManagerTest.cs \
-	$(srcdir)/Core/GameTest.cs
+	$(srcdir)/Core/GameTest.cs \
+	$(srcdir)/Core/GameXmlFactoryTest.cs
 
 CSFILES_CLASSICAL =					\
 	$(srcdir)/Clients/Classical/CommandLineTest.cs
 
+if ENABLE_TESTS
+
 ASSEMBLIES = \
 	$(NUNIT_LIBS)			\
 	$(MONO_ADDINS_LIBS)		\
@@ -29,13 +32,17 @@ RESSOURCES =
 
 $(CORE): $(CSFILES_CORE) $(top_builddir)/src/gbrainy.exe
 	$(CSC) -out:$@ $(CSFLAGS) $(CSFILES_CORE) $(ASSEMBLIES) $(RESSOURCES)
-	MONO_PATH=$(MONO_PATH) $(NUNIT) $(CORE) -nologo
 
 $(CLASSICAL): $(CSFILES_CLASSICAL) $(top_builddir)/src/gbrainy.exe
 	$(CSC) -out:$@ $(CSFLAGS) $(CSFILES_CLASSICAL) $(ASSEMBLIES) $(RESSOURCES)
+
+all: $(CORE) $(CLASSICAL)
+
+run: $(CORE) $(CLASSICAL)
 	MONO_PATH=$(MONO_PATH) $(NUNIT) $(CLASSICAL) -nologo
+	MONO_PATH=$(MONO_PATH) $(NUNIT) $(CORE) -nologo
 
-test: $(CORE) $(CLASSICAL)
+endif
 
 EXTRA_DIST = 				\
 	$(CSFILES_CORE)			\
diff --git a/tests/README b/tests/README
index 3ef4a48..e3318d8 100644
--- a/tests/README
+++ b/tests/README
@@ -4,4 +4,4 @@ behave as intended.
 
 To run the tests use:
 
-make test
+make && make run
diff --git a/tests/test_games.xml b/tests/test_games.xml
new file mode 100644
index 0000000..ceecfa7
--- /dev/null
+++ b/tests/test_games.xml
@@ -0,0 +1,16 @@
+<games>
+	<game>
+		<_name>Clock Rotation</_name>
+		<type>Logic</type>
+		<difficulty>Medium | Master</difficulty>
+		<variables>
+			int num = (1 + random.Next (5)) * 10;
+			int rslt = (2 * 360) + (num * 6);
+		</variables>
+		<_rationale>Rationale text</_rationale>
+		<svg file = "clock.svg" x = "0.25" y = "0.25" width = "0.5" height = "0.5"/>
+		<question>How many degrees rotates the minute hand of a clock?</question>
+		<question plural ="[rslt]">How many degrees rotates the minute hand of a clocks?</question>
+		<answer>[rslt]</answer>
+	</game>
+</games>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]