|
| 1 | +using NUnit.Framework; |
| 2 | +using NUnit.Framework.Legacy; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using Toxy.Parsers; |
| 6 | + |
| 7 | +namespace Toxy.Test |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + public class EPUBParserTest |
| 11 | + { |
| 12 | + void ContainText(string result, string text) |
| 13 | + { |
| 14 | + ClassicAssert.IsTrue(result.IndexOf(text) > 0, result); |
| 15 | + } |
| 16 | + [Test] |
| 17 | + public void TestParseTextSample1() |
| 18 | + { |
| 19 | + string path = TestDataSample.GetEPUBPath("Sample1.epub"); |
| 20 | + var parser = new EPUBTextParser(new ParserContext(path)); |
| 21 | + string result = parser.Parse(); |
| 22 | + string[] lines = result.Split('\n'); |
| 23 | + ClassicAssert.Greater(lines.Length, 1); |
| 24 | + ClassicAssert.IsTrue(lines[1].StartsWith("LA MARCHE")); |
| 25 | + ContainText(result, "Toute discussion stratégique sur nos actions nécessite un rappel de ce que nous avons fait en"); |
| 26 | + ContainText(result, "l’an 2000 et depuis. Au niveau mondial, en l’an 2000, nous avons mené une campagne de"); |
| 27 | + ContainText(result, "Une structure pour nous amener à 2005"); |
| 28 | + ContainText(result, "Lors de la 4e rencontre qui aura lieu en Inde, nous avons deux objectifs majeurs"); |
| 29 | + } |
| 30 | + [Test] |
| 31 | + public void TestParseTextSample5() |
| 32 | + { |
| 33 | + string path = TestDataSample.GetEPUBPath("Sample5.epub"); |
| 34 | + var parser = new EPUBTextParser(new ParserContext(path)); |
| 35 | + string result = parser.Parse(); |
| 36 | + string[] lines = result.Split('\n', StringSplitOptions.RemoveEmptyEntries); |
| 37 | + ClassicAssert.AreEqual("License income by market (%) Philadelphia, Atlanta, Dallas, San Diego, and New", lines[1]); |
| 38 | + ClassicAssert.AreEqual("Orleans. According to company estimates, its own sales", lines[2]); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void TestUnicodeTextContacts() |
| 43 | + { |
| 44 | + string path = TestDataSample.GetEPUBPath("contacts.epub"); |
| 45 | + var parser = new EPUBTextParser(new ParserContext(path)); |
| 46 | + string result = parser.Parse(); |
| 47 | + ContainText(result, "云南国际信托有限公司内部联系方式"); |
| 48 | + ContainText(result, "huyx@yntrust.com 南宁"); |
| 49 | + ContainText(result, "(北京:会议电话:010-209"); |
| 50 | + ContainText(result, "部门总经理助理"); |
| 51 | + ContainText(result, "ranjz@yntrust.com 168"); |
| 52 | + ContainText(result, "王艳芳"); |
| 53 | + ContainText(result, "18616204608"); |
| 54 | + ContainText(result, "wangq@yntrust.com 39"); |
| 55 | + ContainText(result, "高级信息技术经理"); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void TestParseBigFile() |
| 60 | + { |
| 61 | + string path = TestDataSample.GetEPUBPath("Word97-2007BinaryFileFormat_doc_Specification.epub"); |
| 62 | + var parser = new EPUBTextParser(new ParserContext(path)); |
| 63 | + ClassicAssert.DoesNotThrow(() => parser.Parse()); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void TestMetaDataSample1() |
| 68 | + { |
| 69 | + string path = TestDataSample.GetEPUBPath("Metadata sample1.epub"); |
| 70 | + var parser = new EPUBMetaParser(new ParserContext(path)); |
| 71 | + var result = parser.Parse(); |
| 72 | + |
| 73 | + ClassicAssert.AreEqual("高级信息技术经理, 高级信息技术经理", result.Get("Author")?.Value); |
| 74 | + |
| 75 | + List<string> authorList = new List<string>(2); |
| 76 | + if (result.Get("AuthorList")?.Value is List<string> temp) |
| 77 | + { |
| 78 | + authorList = temp; |
| 79 | + } |
| 80 | + ClassicAssert.AreEqual("高级信息技术经理", authorList[0]); |
| 81 | + ClassicAssert.AreEqual("高级信息技术经理", authorList[1]); |
| 82 | + ClassicAssert.AreEqual("高级信息技术经理", result.Get("Description")?.Value); |
| 83 | + ClassicAssert.AreEqual("高级信息技术经理", result.Get("Title")?.Value); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void TestMetaDataSample2() |
| 89 | + { |
| 90 | + string path = TestDataSample.GetEPUBPath("Metadata sample2.epub"); |
| 91 | + var parser = new EPUBMetaParser(new ParserContext(path)); |
| 92 | + var result = parser.Parse(); |
| 93 | + |
| 94 | + ClassicAssert.AreEqual("Unknown, Unknown", result.Get("Author")?.Value); |
| 95 | + |
| 96 | + List<string> authorList = new List<string>(2); |
| 97 | + if (result.Get("AuthorList")?.Value is List<string> temp) |
| 98 | + { |
| 99 | + authorList = temp; |
| 100 | + } |
| 101 | + ClassicAssert.AreEqual("Unknown", authorList[0]); |
| 102 | + ClassicAssert.AreEqual("Unknown", authorList[1]); |
| 103 | + ClassicAssert.AreEqual("Unknown", result.Get("Description")?.Value); |
| 104 | + ClassicAssert.AreEqual("Unknown", result.Get("Title")?.Value); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments