OSDN Git Service

45b87da638c38bd4fcea2d94062683f346020a8d
[wptscs/wpts.git] / HmLibTest / Parsers / XmlElementTest.cs
1 // ================================================================================================
2 // <summary>
3 //      XmlElementのテストクラスソース。</summary>
4 //
5 // <copyright file="XmlElementTest.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Parsers
12 {
13     using System;
14     using System.Collections.Generic;
15     using NUnit.Framework;
16
17     /// <summary>
18     /// XmlElementのテストクラスです。
19     /// </summary>
20     [TestFixture]
21     public class XmlElementTest
22     {
23         #region コンストラクタテストケース
24
25         /// <summary>
26         /// コンストラクタテストケース。
27         /// </summary>
28         [Test]
29         public void TestConstructor()
30         {
31             XmlElement element = new XmlElement("testname1");
32             Assert.AreEqual("testname1", element.Name);
33             Assert.AreEqual(0, element.Attributes.Count);
34             Assert.AreEqual(0, element.Count);
35
36             element = new XmlElement("testname2", "testvalue");
37             Assert.AreEqual("testname2", element.Name);
38             Assert.AreEqual(0, element.Attributes.Count);
39             Assert.AreEqual(1, element.Count);
40             Assert.IsInstanceOf(typeof(TextElement), element[0]);
41             Assert.AreEqual("testvalue", element[0].ToString());
42
43             IDictionary<string, string> attribute = new Dictionary<string, string>();
44             attribute.Add("testattr1", "testattrvalue1");
45             ICollection<IElement> collection = new List<IElement>();
46             collection.Add(new XmlCommentElement("testcomment"));
47             element = new XmlElement("testname3", attribute, collection);
48             Assert.AreEqual("testname3", element.Name);
49             Assert.AreEqual("testattrvalue1", element.Attributes["testattr1"]);
50             Assert.AreEqual("testcomment", ((XmlCommentElement)element[0]).Text);
51         }
52
53         #endregion
54
55         #region プロパティテストケース
56
57         /// <summary>
58         /// Nameプロパティテストケース。
59         /// </summary>
60         [Test]
61         public void TestName()
62         {
63             XmlElement element = new XmlElement("testname1");
64             Assert.AreEqual("testname1", element.Name);
65             element.Name = "testname2";
66             Assert.AreEqual("testname2", element.Name);
67         }
68
69         /// <summary>
70         /// Nameプロパティテストケース(null)。
71         /// </summary>
72         [Test]
73         [ExpectedException(typeof(ArgumentNullException))]
74         public void TestNameNull()
75         {
76             XmlElement element = new XmlElement(null);
77         }
78
79         /// <summary>
80         /// Nameプロパティテストケース(空文字列)。
81         /// </summary>
82         [Test]
83         [ExpectedException(typeof(ArgumentException))]
84         public void TestNameEmpty()
85         {
86             XmlElement element = new XmlElement(String.Empty);
87         }
88
89         /// <summary>
90         /// Nameプロパティテストケース(空白文字列)。
91         /// </summary>
92         [Test]
93         [ExpectedException(typeof(ArgumentException))]
94         public void TestNameBlank()
95         {
96             XmlElement element = new XmlElement(" ");
97         }
98
99         /// <summary>
100         /// Attributesプロパティテストケース。
101         /// </summary>
102         [Test]
103         public void TestAttributes()
104         {
105             XmlElement element = new XmlElement("testname");
106             Assert.IsNotNull(element.Attributes);
107         }
108
109         #endregion
110
111         #region インタフェース実装メソッドテストケース
112
113         /// <summary>
114         /// ToStringメソッドテストケース。
115         /// </summary>
116         [Test]
117         public void TestToString()
118         {
119             XmlElement element = new XmlElement("form");
120             Assert.AreEqual("<form />", element.ToString());
121             element.Attributes.Add("action", "/test.html");
122             Assert.AreEqual("<form action=\"/test.html\" />", element.ToString());
123             element.Attributes.Add("disabled", String.Empty);
124             Assert.AreEqual("<form action=\"/test.html\" disabled=\"\" />", element.ToString());
125             element.Add(new TextElement("フォーム内のテキスト"));
126             Assert.AreEqual("<form action=\"/test.html\" disabled=\"\">フォーム内のテキスト</form>", element.ToString());
127             element.Add(new XmlCommentElement("コメント"));
128             Assert.AreEqual("<form action=\"/test.html\" disabled=\"\">フォーム内のテキスト<!--コメント--></form>", element.ToString());
129             element.Attributes.Add("test_attr", "&<>\"");
130             Assert.AreEqual("<form action=\"/test.html\" disabled=\"\" test_attr=\"&amp;&lt;&gt;&quot;\">フォーム内のテキスト<!--コメント--></form>", element.ToString());
131         }
132
133         #endregion
134     }
135 }