[gbrainy] Unit testing and fixes for command line + build time
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gbrainy] Unit testing and fixes for command line + build time
- Date: Sat, 3 Jul 2010 09:42:05 +0000 (UTC)
commit adb6c5f3edce0cee8163e8d0cd5524e78845f019
Author: Jordi Mas <jmas softcatala org>
Date: Sat Jul 3 11:43:03 2010 +0200
Unit testing and fixes for command line + build time
configure.ac | 11 ++-
src/Clients/Classical/CommandLine.cs | 28 +++++-
src/Clients/Classical/Defines.cs.in | 1 +
src/Clients/Classical/Dialogs/CustomGameDialog.cs | 5 +-
src/Clients/Classical/gbrainy.cs | 5 +-
src/Core/Libraries/GetText.cs | 2 +-
src/Core/Main/GameManager.cs | 27 +++++-
src/Core/Main/Verbal/Analogies.cs | 4 -
src/Core/Main/Verbal/AnalogiesMultipleOptions.cs | 2 +-
.../Main/Verbal/AnalogiesPairOfWordsCompare.cs | 2 +-
.../Main/Verbal/AnalogiesPairOfWordsOptions.cs | 2 +-
src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs | 2 +-
src/Core/Main/Xml/GameXml.cs | 4 +-
src/Core/Main/Xml/GameXmlDefinition.cs | 2 +-
src/Core/Main/Xml/GameXmlFactory.cs | 27 ++----
tests/Clients/Classical/CommandLineTest.cs | 101 ++++++++++++++++++++
tests/Makefile.am | 32 ++++--
17 files changed, 198 insertions(+), 59 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 52623e5..c6810d7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -145,6 +145,14 @@ if test "x$enable_debug" != "xno" ; then
CSC_DEFINES="$CSC_DEFINES -debug -d:DEBUG"
fi
+dnl --- Assembly information
+
+BUILD_TIME=`date +"%F %T %Z"`
+AC_SUBST(BUILD_TIME)
+
+COPYRIGHT="(c) 2007-2010 Jordi Mas i Hernandez"
+AC_SUBST(COPYRIGHT)
+
dnl --- Prologue
AC_SUBST(CFLAGS)
@@ -165,9 +173,6 @@ AC_CONFIG_FILES([src/Clients/Classical/gbrainy],[chmod +x src/Clients/Classical/
GNOME_ICON_THEME_PREFIX=`$PKG_CONFIG --variable=prefix gnome-icon-theme`
AC_SUBST(GNOME_ICON_THEME_PREFIX)
-COPYRIGHT="(c) 2007-2010 Jordi Mas i Hernandez"
-AC_SUBST(COPYRIGHT)
-
AC_OUTPUT([
src/Core/Main/Defines.cs
src/Clients/Classical/Defines.cs
diff --git a/src/Clients/Classical/CommandLine.cs b/src/Clients/Classical/CommandLine.cs
index e618dda..c5436de 100644
--- a/src/Clients/Classical/CommandLine.cs
+++ b/src/Clients/Classical/CommandLine.cs
@@ -32,10 +32,13 @@ namespace gbrainy.Clients.Classical
int [] play_list;
bool cont_execution;
+ public static readonly char GAME_SEPARATOR = ',';
+
public CommandLine (string [] args)
{
this.args = args;
RandomOrder = true;
+ play_list = new int [0];
}
public bool Continue {
@@ -56,7 +59,13 @@ namespace gbrainy.Clients.Classical
{
switch (args [idx]) {
case "--customgame":
- string [] names = args [idx+1].Split (',');
+ string [] names;
+
+ if (idx + 1 >= args.Length)
+ break;
+
+ idx++;
+ names = args [idx].Split (GAME_SEPARATOR);
for (int i = 0; i < names.Length; i++)
names[i] = names[i].Trim ();
@@ -115,7 +124,6 @@ namespace gbrainy.Clients.Classical
Dictionary <string, int> dictionary;
GameManager.GameLocator [] games;
GameManager gm = new GameManager ();
- gm.GameType = GameSession.Types.AllGames;
games = gm.AvailableGames;
// Create a hash to map from game name to locator
@@ -127,7 +135,15 @@ namespace gbrainy.Clients.Classical
Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
game.Variant = games[i].Variant;
- dictionary.Add (game.Name, i);
+
+ try
+ {
+ dictionary.Add (game.Name, i);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine ("gbrainy. Error adding {0} {1}", game.Name, e.Message);
+ }
}
List <int> list = new List <int> (names.Length);
@@ -138,7 +154,7 @@ namespace gbrainy.Clients.Classical
{
list.Add (dictionary [names [i]]);
}
- catch (KeyNotFoundException e)
+ catch (KeyNotFoundException)
{
Console.WriteLine ("gbrainy. Game [{0}] not found", names [i]);
}
@@ -159,12 +175,14 @@ namespace gbrainy.Clients.Classical
" --norandom \t\t\tThe custom game list provided will not be randomized.\n" +
" --versions \t\t\tShow dependencies.\n");
+ Version ();
Console.WriteLine (usage);
}
static void Version ()
{
- Console.WriteLine ("gbrainy " + Defines.VERSION);
+ Console.WriteLine ("gbrainy " + Defines.VERSION + " " +
+ String.Format (Catalog.GetString ("(build on {0})"), Defines.BUILD_TIME));
}
static void Versions ()
diff --git a/src/Clients/Classical/Defines.cs.in b/src/Clients/Classical/Defines.cs.in
index 9ffa851..b10c717 100644
--- a/src/Clients/Classical/Defines.cs.in
+++ b/src/Clients/Classical/Defines.cs.in
@@ -26,5 +26,6 @@ namespace gbrainy.Clients.Classical
public const string VERSION = "@VERSION@";
public const string GNOME_LOCALE_DIR = "@prefix@/share/locale";
public const string COPYRIGHT = "@COPYRIGHT@";
+ public const string BUILD_TIME = "@BUILD_TIME@";
}
}
diff --git a/src/Clients/Classical/Dialogs/CustomGameDialog.cs b/src/Clients/Classical/Dialogs/CustomGameDialog.cs
index 0aef1f8..07e8234 100644
--- a/src/Clients/Classical/Dialogs/CustomGameDialog.cs
+++ b/src/Clients/Classical/Dialogs/CustomGameDialog.cs
@@ -46,13 +46,10 @@ namespace gbrainy.Clients.Classical
public CustomGameDialog (GameManager manager) : base ("CustomGameDialog.ui", "customgame")
{
Game game;
- GameManager gm;
selection_done = false;
this.manager = manager;
- gm = new GameManager ();
- gm.GameType = GameSession.Types.AllGames;
- games = gm.AvailableGames;
+ games = manager.AvailableGames;
drawing_area = new CairoPreview ();
preview_vbox.Add (drawing_area);
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index c427269..d0abc6a 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -691,12 +691,11 @@ namespace gbrainy.Clients.Classical
if (line.Continue == false)
return;
- if (line.PlayList != null) {
+ app.Initialize ();
+ if (line.PlayList.Length > 0) {
app.Session.GameManager.PlayList = line.PlayList;
app.InitialSessionType = GameSession.Types.Custom;
}
-
- app.Initialize ();
app.Session.GameManager.RandomOrder = line.RandomOrder;
app.ProcessDefaults ();
diff --git a/src/Core/Libraries/GetText.cs b/src/Core/Libraries/GetText.cs
index 0aeb659..b415624 100644
--- a/src/Core/Libraries/GetText.cs
+++ b/src/Core/Libraries/GetText.cs
@@ -37,7 +37,7 @@ namespace gbrainy.Core.Libraries
IntPtr r = gettext (ints);
return r != ints;
}
- catch (Exception e) {
+ catch (Exception) {
return true;
}
diff --git a/src/Core/Main/GameManager.cs b/src/Core/Main/GameManager.cs
index 9709858..984b59f 100644
--- a/src/Core/Main/GameManager.cs
+++ b/src/Core/Main/GameManager.cs
@@ -36,6 +36,7 @@ using Mono.Addins.Setup;
#endif
using gbrainy.Core.Main.Verbal;
+using gbrainy.Core.Main.Xml;
namespace gbrainy.Core.Main
{
@@ -69,6 +70,7 @@ namespace gbrainy.Core.Main
GameSession.Types game_type;
IEnumerator <int> enumerator;
Game.Difficulty difficulty;
+ GamesXmlFactory xml_games;
List <GameLocator> available_games; // List of all available games in the system
List <int> play_list; // Play list for the Selected difficulty, game types
@@ -83,12 +85,13 @@ namespace gbrainy.Core.Main
cnt_logic = cnt_memory = cnt_calculation = cnt_verbal = 0;
RandomOrder = true;
- GamesXmlFactory.Read ();
+ xml_games = new GamesXmlFactory ();
+ xml_games.Read (Path.Combine (Defines.DATA_DIR, "games.xml"));
LoadAssemblyGames ();
// Load Analogies
- cnt_verbal += AddGamesAndVariations (VerbalAnalogiesInternal);
+ cnt_verbal += AddVerbalGamesAndVariations (VerbalAnalogiesInternal);
// Load defined XML games
cnt_logic += LoadXmlGames ();
@@ -245,13 +248,31 @@ namespace gbrainy.Core.Main
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;
+ }
+
// XML are stored using the Variant as a pointer to the game + the internal variant
int LoadXmlGames ()
{
Type type = typeof (GameXml);
int cnt = 0;
- foreach (GameXmlDefinition game in GamesXmlFactory.Definitions)
+ foreach (GameXmlDefinition game in xml_games.Definitions)
{
available_games.Add (new GameLocator (type, cnt++, game.Type, true));
for (int i = 0; i < game.Variants.Count; i++)
diff --git a/src/Core/Main/Verbal/Analogies.cs b/src/Core/Main/Verbal/Analogies.cs
index c393d9c..cd4ad46 100644
--- a/src/Core/Main/Verbal/Analogies.cs
+++ b/src/Core/Main/Verbal/Analogies.cs
@@ -29,10 +29,6 @@ namespace gbrainy.Core.Main.Verbal
{
protected Analogy current;
- public override string Name {
- get { return Catalog.GetString ("Verbal analogies"); }
- }
-
public override string Question {
get {
if (current == null)
diff --git a/src/Core/Main/Verbal/AnalogiesMultipleOptions.cs b/src/Core/Main/Verbal/AnalogiesMultipleOptions.cs
index 4f93268..ffaeb77 100644
--- a/src/Core/Main/Verbal/AnalogiesMultipleOptions.cs
+++ b/src/Core/Main/Verbal/AnalogiesMultipleOptions.cs
@@ -40,7 +40,7 @@ namespace gbrainy.Core.Main.Verbal
}
public override string Name {
- get { return Catalog.GetString ("Multiple options");}
+ get { return String.Format (Catalog.GetString ("Multiple options #{0}"), Variant);}
}
public override string Question {
diff --git a/src/Core/Main/Verbal/AnalogiesPairOfWordsCompare.cs b/src/Core/Main/Verbal/AnalogiesPairOfWordsCompare.cs
index 716c4a6..8555193 100644
--- a/src/Core/Main/Verbal/AnalogiesPairOfWordsCompare.cs
+++ b/src/Core/Main/Verbal/AnalogiesPairOfWordsCompare.cs
@@ -42,7 +42,7 @@ namespace gbrainy.Core.Main.Verbal
}
public override string Name {
- get { return Catalog.GetString ("Pair of words compare");}
+ get { return String.Format (Catalog.GetString ("Pair of words compare #{0}"), Variant);}
}
public override Dictionary <int, Analogy> List {
diff --git a/src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs b/src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs
index 1a1631c..58cab0d 100644
--- a/src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs
+++ b/src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs
@@ -39,7 +39,7 @@ namespace gbrainy.Core.Main.Verbal
}
public override string Name {
- get { return Catalog.GetString ("Pair of words");}
+ get { return String.Format (Catalog.GetString ("Pair of words #{0}"), Variant);}
}
public override Dictionary <int, Analogy> List {
diff --git a/src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs b/src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs
index 2d4748c..e87818c 100644
--- a/src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs
+++ b/src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs
@@ -37,7 +37,7 @@ namespace gbrainy.Core.Main.Verbal
}
public override string Name {
- get { return Catalog.GetString ("Question and answer");}
+ get { return String.Format (Catalog.GetString ("Question and answer #{0}"), Variant);}
}
public override Dictionary <int, Analogy> List {
diff --git a/src/Core/Main/Xml/GameXml.cs b/src/Core/Main/Xml/GameXml.cs
index d5f045a..4f748cd 100644
--- a/src/Core/Main/Xml/GameXml.cs
+++ b/src/Core/Main/Xml/GameXml.cs
@@ -27,7 +27,7 @@ using System.Text.RegularExpressions;
using Mono.CSharp;
using Mono.Unix;
-namespace gbrainy.Core.Main
+namespace gbrainy.Core.Main.Xml
{
public class GameXml : Game
{
@@ -297,7 +297,7 @@ namespace gbrainy.Core.Main
static string ReplaceVariables (string str)
{
const string exp = "\\[[a-z_]+\\]+";
- string eval, var, vars, var_value;
+ string var, vars, var_value;
Regex regex;
Match match;
diff --git a/src/Core/Main/Xml/GameXmlDefinition.cs b/src/Core/Main/Xml/GameXmlDefinition.cs
index e6abe0d..5176f0c 100644
--- a/src/Core/Main/Xml/GameXmlDefinition.cs
+++ b/src/Core/Main/Xml/GameXmlDefinition.cs
@@ -21,7 +21,7 @@ using System;
using System.Text;
using System.Collections.Generic;
-namespace gbrainy.Core.Main
+namespace gbrainy.Core.Main.Xml
{
public class GameXmlDefinitionVariant
{
diff --git a/src/Core/Main/Xml/GameXmlFactory.cs b/src/Core/Main/Xml/GameXmlFactory.cs
index 639ac23..12f7433 100644
--- a/src/Core/Main/Xml/GameXmlFactory.cs
+++ b/src/Core/Main/Xml/GameXmlFactory.cs
@@ -23,33 +23,23 @@ using System.IO;
using System.Collections.Generic;
using System.Globalization;
-using Mono.Unix;
-
-namespace gbrainy.Core.Main
+namespace gbrainy.Core.Main.Xml
{
- static public class GamesXmlFactory
+ public class GamesXmlFactory
{
- static List <GameXmlDefinition> games;
- static bool read = false;
+ List <GameXmlDefinition> games;
+ bool read = false;
- static GamesXmlFactory ()
+ public GamesXmlFactory ()
{
games = new List <GameXmlDefinition> ();
}
- static public List <GameXmlDefinition> Definitions {
- get {
- Read ();
- return games;
- }
- }
-
- static public void Read ()
- {
- Read (Path.Combine (Defines.DATA_DIR, "games.xml"));
+ public List <GameXmlDefinition> Definitions {
+ get { return games; }
}
- static void Read (string file)
+ public void Read (string file)
{
GameXmlDefinition game;
string name, str;
@@ -220,6 +210,5 @@ namespace gbrainy.Core.Main
Console.WriteLine ("GameXmlFactory. Error loading: {0}", e.Message);
}
}
-
}
}
diff --git a/tests/Clients/Classical/CommandLineTest.cs b/tests/Clients/Classical/CommandLineTest.cs
new file mode 100644
index 0000000..49d414e
--- /dev/null
+++ b/tests/Clients/Classical/CommandLineTest.cs
@@ -0,0 +1,101 @@
+/*
+ * 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.Text;
+using NUnit.Framework;
+
+using gbrainy.Clients.Classical;
+using gbrainy.Core.Main;
+
+namespace gbrainyTest
+{
+ [TestFixture]
+ public class GameTest
+ {
+ [TestFixtureSetUp]
+ public void Construct ()
+ {
+
+ }
+
+ [Test]
+ public void RandomOrder ()
+ {
+ CommandLine line;
+ string [] args;
+
+ args = new string [1];
+ args [0] = "--norandom";
+
+ line = new CommandLine (args);
+ Assert.AreEqual (true, line.RandomOrder);
+ line.Parse ();
+
+ Assert.AreEqual (false, line.RandomOrder);
+ }
+
+ [Test]
+ public void CustomGame ()
+ {
+ const int game_cnt = 5; // number of games to pass as parameters
+ CommandLine line;
+ string [] args;
+ GameManager.GameLocator [] games;
+ GameManager gm = new GameManager ();
+ StringBuilder game_list = new StringBuilder ();
+ int [] candidates; // Stores the indexes of the games passed as parameters
+ int cand_idx = 0;
+
+ games = gm.AvailableGames;
+ candidates = new int [game_cnt];
+
+ for (int i = 0; i < games.Length; i++)
+ {
+ if (games[i].IsGame == false)
+ continue;
+
+ Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
+ game.Variant = games[i].Variant;
+
+ if (cand_idx > 0)
+ game_list.Append (CommandLine.GAME_SEPARATOR + " ");
+
+ game_list.Append (game.Name);
+ candidates [cand_idx++] = i;
+
+ if (cand_idx >= game_cnt)
+ break;
+ }
+
+ args = new string [2];
+ args [0] = "--customgame";
+ args [1] = game_list.ToString ();
+
+ line = new CommandLine (args);
+ line.Parse ();
+
+ Assert.AreEqual (cand_idx, line.PlayList.Length);
+
+ cand_idx = 0;
+ foreach (int idx in line.PlayList)
+ Assert.AreEqual (idx, candidates [cand_idx++]);
+ }
+ }
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 93fefdb..9618916 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,11 +1,12 @@
-TARGET = gbrainy.Core.Test.dll
+CORE = gbrainy.Core.Test.dll
+CLASSICAL = gbrainy.Test.dll
CSFLAGS = \
-debug \
-define:DEBUG \
-target:library
-CSFILES = \
+CSFILES_CORE = \
$(srcdir)/Core/AnalogiesFactoryTest.cs \
$(srcdir)/Core/PlayerHistoryTest.cs \
$(srcdir)/Core/PlayerPersonalRecordTest.cs \
@@ -13,27 +14,38 @@ CSFILES = \
$(srcdir)/Core/GameManagerTest.cs \
$(srcdir)/Core/GameTest.cs
+CSFILES_CLASSICAL = \
+ $(srcdir)/Clients/Classical/CommandLineTest.cs
+
ASSEMBLIES = \
$(NUNIT_LIBS) \
$(MONO_ADDINS_LIBS) \
- -pkg:gbrainy
+ -r:../src/gbrainy.Core.dll \
+ -r:../src/gbrainy.exe
MONO_PATH = .:$(top_builddir)/src:
RESSOURCES =
-$(TARGET): $(CSFILES) $(top_builddir)/src/gbrainy.exe
- $(CSC) -out:$@ $(CSFLAGS) $(CSFILES) $(ASSEMBLIES) $(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)
+ MONO_PATH=$(MONO_PATH) $(NUNIT) $(CLASSICAL) -nologo
-test: $(TARGET)
- MONO_PATH=$(MONO_PATH) $(NUNIT) $(TARGET) -nologo
+test: $(CORE) $(CLASSICAL)
EXTRA_DIST = \
- $(CSFILES)
+ $(CSFILES_CORE) \
+ $(CSFILES_CLASSICAL)
CLEANFILES = \
- $(TARGET) \
- $(TARGET).mdb \
+ $(CORE) \
+ $(CORE).mdb \
+ $(CLASSICAL) \
+ $(CLASSICAL).mdb \
TestResult.xml
.PHONY: test
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]