// ================================================================================================ // // XmlElementParserのテストクラスソース。 // // // Copyright (C) 2012 Honeplus. All rights reserved. // // Honeplus // ================================================================================================ namespace Honememo.Parsers { using System; using Microsoft.VisualStudio.TestTools.UnitTesting; /// /// のテストクラスです。 /// [TestClass] internal class XmlElementParserTest { #region private変数 /// /// 前処理・後処理で毎回生成/解放される。 /// private XmlParser xmlParser; #endregion #region 前処理・後処理 /// /// テストの前処理。 /// /// が必要なの生成。 [TestInitialize] public void SetUp() { this.xmlParser = new XmlParser(); } /// /// テストの後処理。 /// /// が必要なの解放。 [TestCleanup] public void TearDown() { this.xmlParser.Dispose(); } #endregion #region 公開プロパティテストケース /// /// プロパティテストケース。 /// [TestMethod] public void TestTargets() { XmlElementParser parser = new XmlElementParser(this.xmlParser); // 初期状態でオブジェクトが存在すること Assert.IsNotNull(parser.Targets); Assert.AreEqual(0, parser.Targets.Count); parser.Targets.Add("span"); Assert.AreEqual("span", parser.Targets[0]); // 設定すればそのオブジェクトが入ること string[] targets = new string[] { "div", "p" }; parser.Targets = targets; Assert.AreSame(targets, parser.Targets); } /// /// プロパティテストケース(null)。 /// [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void TestTargetsNull() { new XmlElementParser(this.xmlParser).Targets = null; } #endregion #region インタフェース実装メソッドテストケース /// /// メソッドテストケース(実例)。 /// [TestMethod] public void TestTryParse() { IElement element; XmlElement xmlElement; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("

test

", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("

test

", xmlElement.ToString()); Assert.AreEqual("h1", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("

test
", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("
", xmlElement.ToString()); Assert.AreEqual("br", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("

", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("
", xmlElement.ToString()); Assert.AreEqual("div", xmlElement.Name); Assert.AreEqual(2, xmlElement.Attributes.Count); Assert.AreEqual("testid", xmlElement.Attributes["id"]); Assert.AreEqual("testname", xmlElement.Attributes["name"]); this.xmlParser.IsHtml = true; Assert.IsTrue(parser.TryParse("

段落1

段落2", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("

", xmlElement.ToString()); Assert.AreEqual("p", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("input", xmlElement.Name); Assert.AreEqual(4, xmlElement.Attributes.Count); Assert.AreEqual("checkbox", xmlElement.Attributes["type"]); Assert.AreEqual("param", xmlElement.Attributes["name"]); Assert.AreEqual("test", xmlElement.Attributes["value"]); Assert.AreEqual(string.Empty, xmlElement.Attributes["checked"]); Assert.IsTrue(parser.TryParse("

outertext
innertext
", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("
outertext
innertext
", xmlElement.ToString()); Assert.AreEqual("div", xmlElement.Name); Assert.AreEqual(1, xmlElement.Attributes.Count); Assert.AreEqual("outer", xmlElement.Attributes["id"]); } /// /// メソッドテストケース(基本形)。 /// [TestMethod] public void TestTryParseNormal() { IElement element; XmlElement xmlElement; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("test value", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("test value", xmlElement.ToString()); Assert.AreEqual("testtag2", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse(" test value2 testend", out element)); xmlElement = (XmlElement)element; Assert.AreEqual(" test value2 ", xmlElement.ToString()); Assert.AreEqual("testtag3", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse(" test value3 testend", out element)); xmlElement = (XmlElement)element; Assert.AreEqual(" test value3 ", xmlElement.ToString()); Assert.AreEqual("testtag4", xmlElement.Name); Assert.AreEqual(1, xmlElement.Attributes.Count); Assert.AreEqual("testvalue", xmlElement.Attributes["testattr"]); Assert.IsTrue(parser.TryParse("testbodytestend", out element)); xmlElement = (XmlElement)element; Assert.IsInstanceOfType(xmlElement[0], typeof(XmlTextElement)); Assert.AreEqual("testbody", xmlElement.ToString()); Assert.AreEqual("testtag5", xmlElement.Name); Assert.AreEqual(1, xmlElement.Attributes.Count); Assert.AreEqual("testvalue2", xmlElement.Attributes["testattr2"]); } /// /// メソッドテストケース(普通でNGパターン)。 /// [TestMethod] public void TestTryParseNormalNg() { IElement element; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsFalse(parser.TryParse(" ", out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse("", out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse(string.Empty, out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse(null, out element)); Assert.IsNull(element); } /// /// メソッドテストケース(単一のパターン)。 /// [TestMethod] public void TestTryParseSingle() { IElement element; XmlElement xmlElement; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag2", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("testtag4 />", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag3", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("/>", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag5", xmlElement.Name); Assert.AreEqual(1, xmlElement.Attributes.Count); Assert.AreEqual("testvalue", xmlElement.Attributes["testattr"]); Assert.IsTrue(parser.TryParse("/>", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag6", xmlElement.Name); Assert.AreEqual(2, xmlElement.Attributes.Count); Assert.AreEqual("testvalue1", xmlElement.Attributes["testattr1"]); Assert.AreEqual("testvalue2", xmlElement.Attributes["testattr2"]); } /// /// メソッドテストケース(不正な構文)。 /// [TestMethod] public void TestTryParseLazy() { IElement element; XmlElement xmlElement; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("

", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("

", xmlElement.ToString()); Assert.AreEqual("p", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("test value", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("test value", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("test value", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("test value", xmlElement.ToString()); Assert.AreEqual("testtag2", xmlElement.Name); Assert.AreEqual(2, xmlElement.Attributes.Count); Assert.AreEqual("test", xmlElement.Attributes["testattr"]); Assert.AreEqual(string.Empty, xmlElement.Attributes["value"]); Assert.IsTrue(parser.TryParse("test value2testend", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("test value2testend", xmlElement.ToString()); Assert.AreEqual("testtag3", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); } ///

/// メソッドテストケース(不正でNG)。 /// [TestMethod] public void TestTryParseLazyNg() { IElement element; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsFalse(parser.TryParse("< testtag>
", out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse("", out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse("", out element)); Assert.IsNull(element); Assert.IsFalse(parser.TryParse("4", out element)); Assert.IsNull(element); } /// /// メソッドテストケース(HTML)。 /// [TestMethod] public void TestTryParseHtml() { IElement element; HtmlElement htmlElement; this.xmlParser.IsHtml = true; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("", htmlElement.ToString()); Assert.AreEqual("testtag", htmlElement.Name); Assert.AreEqual(0, htmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("

", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("

", htmlElement.ToString()); Assert.AreEqual("p", htmlElement.Name); Assert.AreEqual(0, htmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("testtag3 />", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("", htmlElement.ToString()); Assert.AreEqual("testtag2", htmlElement.Name); Assert.AreEqual(0, htmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("", htmlElement.ToString()); Assert.AreEqual("testtag4", htmlElement.Name); Assert.AreEqual(0, htmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse(">", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("", htmlElement.ToString()); Assert.AreEqual("testtag6", htmlElement.Name); Assert.AreEqual(1, htmlElement.Attributes.Count); Assert.AreEqual("testvalue", htmlElement.Attributes["testattr"]); Assert.IsTrue(parser.TryParse("test", out element)); htmlElement = (HtmlElement)element; Assert.AreEqual("test", htmlElement.ToString()); Assert.AreEqual("testtag7", htmlElement.Name); Assert.AreEqual(2, htmlElement.Attributes.Count); Assert.AreEqual("testvalue1", htmlElement.Attributes["testattr1"]); Assert.AreEqual("testvalue2", htmlElement.Attributes["testattr2"]); } ///

/// メソッドテストケース(大文字小文字)。 /// [TestMethod] public void TestTryParseIgnoreCase() { IElement element; XmlElement xmlElement; this.xmlParser.IgnoreCase = false; XmlElementParser parser = new XmlElementParser(this.xmlParser); Assert.IsTrue(parser.TryParse("
", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); Assert.IsTrue(parser.TryParse("
", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("
", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); this.xmlParser.IgnoreCase = true; Assert.IsTrue(parser.TryParse("", out element)); xmlElement = (XmlElement)element; Assert.AreEqual("", xmlElement.ToString()); Assert.AreEqual("testtag", xmlElement.Name); Assert.AreEqual(0, xmlElement.Attributes.Count); } /// /// メソッドテストケース(タグ限定)。 /// [TestMethod] public void TestTryParseTargets() { IElement element; XmlElementParser parser = new XmlElementParser(this.xmlParser); // 特定のタグのみを処理対象とするよう指定する parser.Targets = new string[] { "div", "span" }; Assert.IsFalse(parser.TryParse("

test

", out element)); Assert.IsFalse(parser.TryParse("
", out element)); Assert.IsTrue(parser.TryParse("
test
", out element)); Assert.AreEqual("
test
", element.ToString()); // XmlParserに大文字小文字無視が指定されている場合、ここも無視する Assert.IsTrue(parser.TryParse("test", out element)); Assert.AreEqual("test", element.ToString()); // 指定されていない場合、区別する this.xmlParser.IgnoreCase = false; Assert.IsFalse(parser.TryParse("test", out element)); Assert.IsTrue(parser.TryParse("test", out element)); Assert.AreEqual("test", element.ToString()); } /// /// メソッドテストケース。 /// [TestMethod] public void TestIsElementPossible() { XmlElementParser parser = new XmlElementParser(new XmlParser()); Assert.IsTrue(parser.IsPossibleParse('<')); Assert.IsFalse(parser.IsPossibleParse('[')); Assert.IsFalse(parser.IsPossibleParse('-')); Assert.IsFalse(parser.IsPossibleParse('/')); Assert.IsFalse(parser.IsPossibleParse('#')); } #endregion } }