Mercurial > repos > IBBoard.WarFoundry.API.Tests
changeset 69:b2517bb113d0
Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
* Validate loading of different key/keyref options in army file
This may be all of it, but needs checking
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jul 2010 20:04:16 +0000 |
parents | a82f164e26e3 |
children | 90a9f13b9627 |
files | API/Factories/Xml/SingleXmlObjectLoader.cs API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs IBBoard.WarFoundry.API.Tests.csproj testdata/xml-factory-util/duplicate-unit-id.armyx testdata/xml-factory-util/invalid-contained-id.armyx testdata/xml-factory-util/valid-contained-id.armyx |
diffstat | 6 files changed, 85 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Factories/Xml/SingleXmlObjectLoader.cs Sun Jul 04 19:11:08 2010 +0000 +++ b/API/Factories/Xml/SingleXmlObjectLoader.cs Sun Jul 04 20:04:16 2010 +0000 @@ -32,6 +32,11 @@ } } + public static XmlElement CreateDocumentElementFromFile(string filePath) + { + return CreateDocumentElementFromFile(new FileInfo(filePath)); + } + public static XmlElement CreateDocumentElementFromFile(FileInfo file) { return CreateDocumentElementFromStream(file.OpenRead());
--- a/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs Sun Jul 04 19:11:08 2010 +0000 +++ b/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs Sun Jul 04 20:04:16 2010 +0000 @@ -3,9 +3,11 @@ // // 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 System.IO; using System.Xml; +using System.Xml.Schema; +using IBBoard.IO; using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; namespace IBBoard.WarFoundry.API.Factories.Xml { @@ -15,15 +17,48 @@ [Test()] public void TestParsingExtendedDataFiles() { - TestParsingExtendedDataFile("testdata/extended-data.racex"); - TestParsingExtendedDataFile("testdata/extended-data.systemx"); - TestParsingExtendedDataFile("testdata/extended-data.armyx"); + TestValidFileLoading("testdata/extended-data.racex"); + TestValidFileLoading("testdata/extended-data.systemx"); + TestValidFileLoading("testdata/extended-data.armyx"); + } + + [Test()] + public void TestParsingValidArmyContainedID() + { + TestValidFileLoading("testdata/xml-factory-util/valid-contained-id.armyx"); + } + + private static void TestValidFileLoading(string path) + { + XmlElement document = SingleXmlObjectLoader.CreateDocumentElementFromFile(path); + Assert.IsNotNull(document); } - private static void TestParsingExtendedDataFile(string path) + [Test()] + public void TestArmyFileValidationFailsForDuplicateUnitID() + { + TestFileValidationFailure("testdata/xml-factory-util/duplicate-unit-id.armyx"); + } + + [Test()] + public void TestArmyFileValidationFailsForInvalidContainedID() { - XmlDocument document = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(new FileInfo(path).OpenRead()); - Assert.IsNotNull(document); + TestFileValidationFailure("testdata/xml-factory-util/invalid-contained-id.armyx"); + } + + private void TestFileValidationFailure(string filePath) + { + try + { + SingleXmlObjectLoader.CreateDocumentElementFromFile(filePath); + Assert.Fail("Excepted exception not thrown"); + } + catch (InvalidFileException ex) + { + Console.WriteLine(ex.Message); + Exception innerException = ex.InnerException; + Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException))); + } } } }
--- a/IBBoard.WarFoundry.API.Tests.csproj Sun Jul 04 19:11:08 2010 +0000 +++ b/IBBoard.WarFoundry.API.Tests.csproj Sun Jul 04 20:04:16 2010 +0000 @@ -201,6 +201,15 @@ <None Include="testdata\xml-system-factory\invalid-default-stats.systemx"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> + <None Include="testdata\xml-factory-util\duplicate-unit-id.armyx"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + <None Include="testdata\xml-factory-util\invalid-contained-id.armyx"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + <None Include="testdata\xml-factory-util\valid-contained-id.armyx"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> </ItemGroup> <ItemGroup> <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73"> @@ -235,5 +244,6 @@ <Folder Include="testdata\" /> <Folder Include="testdata\" /> <Folder Include="testdata\xml-system-factory\" /> + <Folder Include="testdata\xml-factory-util\" /> </ItemGroup> </Project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/duplicate-unit-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100"> + <units> + <unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"/> + <unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"/> + </units> +</army> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/invalid-contained-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100"> + <units> + <unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"> + <contains> + <containedUnit containedID="unit2" /> + </contains> + </unit> + </units> +</army> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/valid-contained-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100"> + <units> + <unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"> + <contains> + <containedUnit containedID="unit2" /> + </contains> + </unit> + <unit id="unit2" unitType="axemen" unitName="Axemen" size="10"/> + </units> +</army> \ No newline at end of file