[gbrainy] Fixes games.xml appear twice in the play list + more unit testing
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gbrainy] Fixes games.xml appear twice in the play list + more unit testing
- Date: Tue, 17 Aug 2010 08:10:39 +0000 (UTC)
commit 22b39bb26bd733c2ed6e5d5f798793817ccb4220
Author: Jordi Mas <jmas softcatala org>
Date: Tue Aug 17 10:11:58 2010 +0200
Fixes games.xml appear twice in the play list + more unit testing
src/Core/Main/GameManager.cs | 27 ++++++++------
src/Core/Main/Xml/GameXml.cs | 2 +-
tests/Core/GameManagerTest.cs | 28 ++++++++++++++-
tests/Core/GameXmlFactoryTest.cs | 70 +++++++++++++++++++++++++++++++++++--
tests/test_games.xml | 36 +++++++++++++++++++-
5 files changed, 144 insertions(+), 19 deletions(-)
---
diff --git a/src/Core/Main/GameManager.cs b/src/Core/Main/GameManager.cs
index c002a26..99bf366 100644
--- a/src/Core/Main/GameManager.cs
+++ b/src/Core/Main/GameManager.cs
@@ -223,21 +223,24 @@ namespace gbrainy.Core.Main
foreach (GameXmlDefinition game in xml_games.Definitions)
{
+ // If the game has variants the game definition is used as reference
+ // but only the variants are playable. The first variant is used as game (IsGame = true)
available_games.Add (new GameLocator (type, cnt++, game.Type, true));
- for (int i = 0; i < game.Variants.Count; i++)
+
+ switch (game.Type) {
+ case GameTypes.LogicPuzzle:
+ cnt_logic++;
+ break;
+ case GameTypes.MathTrainer:
+ cnt_calculation++;
+ break;
+ default:
+ break;
+ }
+
+ for (int i = 1; i < game.Variants.Count; i++)
{
available_games.Add (new GameLocator (type, cnt++, game.Type, false));
-
- switch (game.Type) {
- case GameTypes.LogicPuzzle:
- cnt_logic++;
- break;
- case GameTypes.MathTrainer:
- cnt_calculation++;
- break;
- default:
- break;
- }
}
}
}
diff --git a/src/Core/Main/Xml/GameXml.cs b/src/Core/Main/Xml/GameXml.cs
index bfffffb..23b1780 100644
--- a/src/Core/Main/Xml/GameXml.cs
+++ b/src/Core/Main/Xml/GameXml.cs
@@ -279,7 +279,7 @@ namespace gbrainy.Core.Main.Xml
for (int game = 0; game < games.Count; game++)
{
locators.Add (new DefinitionLocator (game, 0));
- for (int variant = 0; variant < games[game].Variants.Count; variant++)
+ for (int variant = 1; variant < games[game].Variants.Count; variant++)
locators.Add (new DefinitionLocator (game, variant));
}
}
diff --git a/tests/Core/GameManagerTest.cs b/tests/Core/GameManagerTest.cs
index b581d75..0ef78b1 100644
--- a/tests/Core/GameManagerTest.cs
+++ b/tests/Core/GameManagerTest.cs
@@ -69,7 +69,7 @@ namespace gbrainyTest
Game game = (Game) Activator.CreateInstance (locator.TypeOf, true);
game.Variant = locator.Variant;
-
+
Assert.AreEqual (false, dictionary.ContainsKey (game.Name),
String.Format ("Game name {0} is duplicated", game.Name));
@@ -150,5 +150,31 @@ namespace gbrainyTest
manager.ResetAvailableGames ();
Assert.AreEqual (0, manager.AvailableGames.Length);
}
+
+ [Test]
+ public void XmlGames ()
+ {
+ GameManager manager = new GameManager ();
+ manager.GameType = GameSession.Types.AllGames;
+ manager.LoadGamesFromXml ("test_games.xml");
+
+ Assert.AreEqual (3, manager.AvailableGames.Length);
+ int logic_variants = 0;
+ int logic_games = 0;
+
+ foreach (GameManager.GameLocator locator in manager.AvailableGames)
+ {
+ if (locator.GameType != GameTypes.LogicPuzzle)
+ continue;
+
+ if (locator.IsGame == true)
+ logic_games++;
+ else
+ logic_variants++;
+
+ }
+ Assert.AreEqual (2, logic_games);
+ Assert.AreEqual (1, logic_variants);
+ }
}
}
diff --git a/tests/Core/GameXmlFactoryTest.cs b/tests/Core/GameXmlFactoryTest.cs
index c544225..17043bc 100644
--- a/tests/Core/GameXmlFactoryTest.cs
+++ b/tests/Core/GameXmlFactoryTest.cs
@@ -30,6 +30,10 @@ namespace gbrainyTest
[TestFixture]
public class GameXmlFactoryTest
{
+ GamesXmlFactory factory;
+ List <GameXmlDefinition> definitions;
+ GameXmlDefinition definition;
+
[TestFixtureSetUp]
public void Construct ()
{
@@ -39,22 +43,80 @@ namespace gbrainyTest
[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 (0, definition.Variants.Count);
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);
+
+
+ }
+
+ [Test]
+ public void DrawingElements ()
+ {
+ factory = new GamesXmlFactory ();
+ factory.Read ("test_games.xml");
+ definitions = factory.Definitions;
+
+ definition = definitions [0];
+ Assert.AreEqual (2, definition.DrawingObjects.Length);
+
+ // Check image drawing object
+ Assert.AreEqual (typeof (gbrainy.Core.Main.Xml.ImageDrawingObject), definition.DrawingObjects [0].GetType ());
+
+ ImageDrawingObject image = definition.DrawingObjects [0] as ImageDrawingObject;
+
+ Assert.AreEqual ("clock.svg", image.Filename);
+ Assert.AreEqual (0.30, image.X);
+ Assert.AreEqual (0.40, image.Y);
+ Assert.AreEqual (0.50, image.Width);
+ Assert.AreEqual (0.60, image.Height);
+
+ // Check text drawing object
+ Assert.AreEqual (typeof (gbrainy.Core.Main.Xml.TextDrawingObject), definition.DrawingObjects [1].GetType ());
+
+ TextDrawingObject text = definition.DrawingObjects [1] as TextDrawingObject;
+ Assert.AreEqual ("Sample text for unit tests", text.Text);
+ Assert.AreEqual (0.5, text.X);
+ Assert.AreEqual (0.4, text.Y);
+ Assert.AreEqual (true, text.Centered);
+ Assert.AreEqual (true, text.Big);
+ }
+
+ [Test]
+ public void GameDefinitionWithVariants ()
+ {
+ GameXmlDefinitionVariant variant;
+ factory = new GamesXmlFactory ();
+ factory.Read ("test_games.xml");
+ definitions = factory.Definitions;
+ definition = definitions [1]; // Age Game
+
+ Assert.AreEqual ("Age", definition.Name);
+ Assert.AreEqual (2, definition.Variants.Count);
+ Assert.AreEqual ("father_son.svg", ((definition.DrawingObjects[0]) as ImageDrawingObject).Filename);
+
+ // Variant: John is 46 years old.
+ variant = definition.Variants [0];
+ Assert.AreEqual (true, variant.Question.String.Contains ("John is 46 years old"));
+ Assert.AreEqual ("[son]", variant.Answer);
+ Assert.AreEqual (true, variant.Variables.Contains ("int father = 46;"));
+
+ // Variant: John's age is nowadays 2 times his son's age.
+ variant = definition.Variants [1];
+ Assert.AreEqual (true, variant.Question.String.Contains ("John's age is nowadays 2 times his son's age."));
+ Assert.AreEqual ("24", variant.Answer);
+ Assert.AreEqual (true, variant.Variables.Contains ("int ago = years [idx];"));
}
}
}
+
diff --git a/tests/test_games.xml b/tests/test_games.xml
index ceecfa7..8c9f210 100644
--- a/tests/test_games.xml
+++ b/tests/test_games.xml
@@ -8,9 +8,43 @@
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"/>
+ <svg file = "clock.svg" x = "0.30" y = "0.40" width = "0.50" height = "0.60"/>
+ <string _text = "Sample text for unit tests" x = "0.5" y = "0.4" centered = "yes" size = "big"/>
<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>
+
+ <game>
+ <_name>Age</_name>
+ <type>Logic</type>
+ <difficulty>All</difficulty>
+ <svg file = "father_son.svg" x = "0.2" y = "0.25" width = "0.6" height = "0.4"/>
+ <variant>
+ <variables>
+ int father = 46;
+ int difference = 2 + random.Next (8);
+ int son = (father / 2) - difference;
+ </variables>
+ <question>John is 46 years old. His son is [difference] year younger than half of John's age. How old is John's son?</question>
+ <question plural ="[difference]">John is 46 years old. His son is [difference] years younger than half of John's age. How old is John's son?</question>
+ <answer>[son]</answer>
+ </variant>
+ <variant>
+ <variables>
+ int [] proportions = new int [] {3,4,5};
+ int [] years = new int [] {12, 16, 18};
+ int idx = random.Next (years.Length);
+
+ int ago = years [idx];
+ int proportion = proportions [idx];
+ </variables>
+ <question>John's age is nowadays 2 times his son's age. [ago] year ago, John was [proportion] times older than his son. How old is John's son nowadays?</question>
+ <question plural ="[ago]">John's age is nowadays 2 times his son's age. [ago] years ago, John was [proportion] times older than his son. How old is John's son nowadays?</question>
+ <answer>24</answer>
+ <rationale>[ago] year ago, John's age minus [ago] was equal to [proportion] times his son's age minus [ago].</rationale>
+ <rationale plural ="[ago]">[ago] years ago, John's age minus [ago] was equal to [proportion] times his son's age minus [ago].</rationale>
+ </variant>
+ </game>
+
</games>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]