annotate Xml/XmlToolsTests.cs @ 12:298786adcddc

Fixes #25: Add method for parsing enums from XML elements * Add unit tests for XmlToolsTests to test enum parsing
author IBBoard <dev@ibboard.co.uk>
date Mon, 30 Nov 2009 20:47:22 +0000
parents
children f6fd30d3b817
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (AbstractLimitTest.cs) is a part of the IBBoard.Tests project and is copyright 2009 IBBoard
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Xml;
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using NUnit.Framework;
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 namespace IBBoard.Xml
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 [TestFixture()]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 public class XmlToolsTests
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 private enum TestEnum
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 ALPHA
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 [Test()]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 public void TestParsingOfValidEnumString ()
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 XmlElement elem = GetElement("ALPHA");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 Assert.AreEqual(TestEnum.ALPHA, XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum"));
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 static XmlElement GetElement(string enumValue)
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 XmlDocument doc = new XmlDocument();
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 XmlElement elem = doc.CreateElement ("sample");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 XmlAttribute attrib = doc.CreateAttribute ("enum");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 attrib.Value = enumValue;
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 elem.Attributes.Append (attrib);
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 return elem;
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 [Test()]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 public void TestParsingOfValidEnumStringWithDifferentCase ()
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 XmlElement elem = GetElement("ALPHA");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 Assert.AreEqual(TestEnum.ALPHA, XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum"));
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 [Test()]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 [ExpectedException(typeof(FormatException), ExpectedMessage="Attribute 'enum' of sample with ID was not a valid TestEnum enum")]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 public void TestParsingValidEnumStringWithInvalidValueThrowsException ()
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 XmlElement elem = GetElement("Omega");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 [Test()]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 [ExpectedException(typeof(FormatException), ExpectedMessage="Attribute 'enum' of sample with ID was not a valid TestEnum enum")]
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 public void TestCaseSensitiveParsingOfValidValueInInvalidCaseThrowsException ()
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 {
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 XmlElement elem = GetElement("Alpha");
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum", false);
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 }
298786adcddc Fixes #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 }