/
XlinqExtensionsTests.cs
68 lines (58 loc) · 1.6 KB
/
XlinqExtensionsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Xml.Linq;
using NUnit.Framework;
using ServiceStack.Common;
namespace ServiceStack.ServiceModel.Tests
{
[TestFixture]
public class XlinqExtensionsTests
{
private const int IntValue = 2147483647;
private static XElement CreateChildElement<T>(T value)
{
return new XElement("el", new XElement("child", value));
}
private static XElement CreateEmptyChildElement()
{
return new XElement("el", new XElement("child"));
}
[Test]
public void xelement_get_int_test()
{
var el = CreateChildElement(IntValue);
Assert.AreEqual(IntValue, el.GetInt("child"));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void xelement_get_int_null_throws_exception_test()
{
var el = CreateEmptyChildElement();
el.GetInt("child");
Assert.Fail("Expected ArgumentNullException");
}
[Test]
[ExpectedException(typeof(FormatException))]
public void xelement_get_int_text_throws_exception_test()
{
var el = CreateChildElement("Non int value");
el.GetInt("child");
Assert.Fail("Expected FormatException");
}
[Test]
public void xelement_get_int_or_default_test()
{
var el = CreateChildElement(IntValue);
Assert.AreEqual(IntValue, el.GetIntOrDefault("child"));
el = CreateEmptyChildElement();
Assert.AreEqual(default(int), el.GetIntOrDefault("child"));
}
[Test]
public void xelement_get_nullable_int_test()
{
var el = CreateChildElement(IntValue);
Assert.AreEqual(IntValue, el.GetNullableInt("child").Value);
el = CreateChildElement((int?)null);
Assert.AreEqual(null, el.GetNullableInt("child"));
}
}
}