Skip to content

Commit 1d5937d

Browse files
committed
新增單元測試
1 parent b65efdb commit 1d5937d

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

src/MiniWord/MiniWord.Implment.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
204204
}
205205
t.Remove();
206206
}
207-
else if (tag.Value is MiniWordHyperLink ||
208-
tag.Value is IEnumerable<MiniWordHyperLink>)
207+
else if (IsHyperLink(tag.Value))
209208
{
210209
AddHyperLink(docx, run, tag.Value);
211210
t.Remove();
@@ -253,6 +252,12 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
253252
}
254253
}
255254

255+
private static bool IsHyperLink(object value)
256+
{
257+
return value is MiniWordHyperLink ||
258+
value is IEnumerable<MiniWordHyperLink>;
259+
}
260+
256261
private static void AddHyperLink(WordprocessingDocument docx, Run run, object value)
257262
{
258263
List<MiniWordHyperLink> links = new List<MiniWordHyperLink>();

tests/MiniWordTests/IssueTests.cs

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public void TestIssue5()
531531
}
532532

533533
[Fact]
534-
public void TestIssue_MiniWordHyperLinkList()
534+
public void MiniWordHyperLink_List()
535535
{
536536
var path = PathHelper.GetTempFilePath();
537537
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
@@ -543,7 +543,7 @@ public void TestIssue_MiniWordHyperLinkList()
543543
Text = "測試連結22!!"
544544
},
545545
new MiniWordHyperLink(){
546-
Url = "https://google.com",
546+
Url = "https://google1.com",
547547
Text = "測試連結11!!"
548548
}
549549
},
@@ -555,11 +555,88 @@ public void TestIssue_MiniWordHyperLinkList()
555555
};
556556
MiniWord.SaveAsByTemplate(path, templatePath, value);
557557
//Console.WriteLine(path);
558-
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
559-
Assert.DoesNotContain("Jack Demo APP Account Data", xml);
560-
Assert.Contains("MiniSofteware Demo APP Account Data", xml);
561-
Assert.Contains("MiniSofteware Demo APP Account Data", xml);
562-
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", xml);
558+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
559+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
560+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
561+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
562+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
563+
564+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
565+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
566+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
567+
}
568+
569+
570+
[Fact]
571+
public void MiniWordHyperLink_Array()
572+
{
573+
var path = PathHelper.GetTempFilePath();
574+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
575+
var value = new Dictionary<string, object>()
576+
{
577+
["Name"] = new MiniWordHyperLink[]{
578+
new MiniWordHyperLink(){
579+
Url = "https://google.com",
580+
Text = "測試連結22!!"
581+
},
582+
new MiniWordHyperLink(){
583+
Url = "https://google1.com",
584+
Text = "測試連結11!!"
585+
}
586+
},
587+
["Company_Name"] = "MiniSofteware",
588+
["CreateDate"] = new DateTime(2021, 01, 01),
589+
["VIP"] = true,
590+
["Points"] = 123,
591+
["APP"] = "Demo APP",
592+
};
593+
MiniWord.SaveAsByTemplate(path, templatePath, value);
594+
595+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
596+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
597+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
598+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
599+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
600+
601+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
602+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
603+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
604+
}
605+
606+
[Fact]
607+
public void MiniWordHyperLink_AnonymousArray()
608+
{
609+
var path = PathHelper.GetTempFilePath();
610+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
611+
var value = new Dictionary<string, object>()
612+
{
613+
["Name"] = new []{
614+
new MiniWordHyperLink(){
615+
Url = "https://google.com",
616+
Text = "測試連結22!!"
617+
},
618+
new MiniWordHyperLink(){
619+
Url = "https://google1.com",
620+
Text = "測試連結11!!"
621+
}
622+
},
623+
["Company_Name"] = "MiniSofteware",
624+
["CreateDate"] = new DateTime(2021, 01, 01),
625+
["VIP"] = true,
626+
["Points"] = 123,
627+
["APP"] = "Demo APP",
628+
};
629+
MiniWord.SaveAsByTemplate(path, templatePath, value);
630+
631+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
632+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
633+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
634+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
635+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
636+
637+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
638+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
639+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
563640
}
564641

565642
/// <summary>

0 commit comments

Comments
 (0)