Skip to content

Commit 5b0d2f1

Browse files
committed
Moved three test methods from main project into test project
1 parent d34e408 commit 5b0d2f1

File tree

3 files changed

+107
-86
lines changed

3 files changed

+107
-86
lines changed

Source/RtfDomParser.Tests/Helper.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace RtfDomParser.Tests
2+
{
3+
internal static class Helper
4+
{
5+
/// <summary>
6+
/// Test to generate a little rtf document
7+
/// </summary>
8+
/// <param name="w">RTF text writer</param>
9+
internal static void TestBuildRTF(RTFWriter w)
10+
{
11+
w.Encoding = System.Text.Encoding.GetEncoding(936);
12+
// write header
13+
w.WriteStartGroup();
14+
w.WriteKeyword("rtf1");
15+
w.WriteKeyword("ansi");
16+
w.WriteKeyword("ansicpg" + w.Encoding.CodePage);
17+
// wirte font table
18+
w.WriteStartGroup();
19+
w.WriteKeyword("fonttbl");
20+
w.WriteStartGroup();
21+
w.WriteKeyword("f0");
22+
w.WriteText("Arial;");
23+
w.WriteEndGroup();
24+
w.WriteStartGroup();
25+
w.WriteKeyword("f1");
26+
w.WriteText("Times New Roman;");
27+
w.WriteEndGroup();
28+
w.WriteEndGroup();
29+
// write color table
30+
w.WriteStartGroup();
31+
w.WriteKeyword("colortbl");
32+
w.WriteText(";");
33+
w.WriteKeyword("red0");
34+
w.WriteKeyword("green0");
35+
w.WriteKeyword("blue255");
36+
w.WriteText(";");
37+
w.WriteEndGroup();
38+
// write content
39+
w.WriteKeyword("qc"); // set alignment center
40+
w.WriteKeyword("f0"); // set font
41+
w.WriteKeyword("fs30"); // set font size
42+
w.WriteText("This is the first paragraph text ");
43+
w.WriteKeyword("cf1"); // set text color
44+
w.WriteText("Arial ");
45+
w.WriteKeyword("cf0"); // set default color
46+
w.WriteKeyword("f1"); // set font
47+
w.WriteText("Align center ABC12345");
48+
w.WriteKeyword("par"); // new paragraph
49+
w.WriteKeyword("pard"); // clear format
50+
w.WriteKeyword("f1"); // set font
51+
w.WriteKeyword("fs20"); // set font size
52+
w.WriteKeyword("cf1");
53+
w.WriteText("This is the secend paragraph Arial left alignment ABC12345");
54+
// finish
55+
w.WriteEndGroup();
56+
}
57+
}
58+
}

Source/RtfDomParser.Tests/RtfTest.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
using System.Threading;
3+
using NUnit.Framework;
4+
5+
namespace RtfDomParser.Tests
6+
{
7+
[TestFixture]
8+
public class RtfTest
9+
{
10+
[SetUp]
11+
public void Setup()
12+
{
13+
Defaults.FontName = System.Windows.Forms.Control.DefaultFont.Name;
14+
}
15+
16+
/// <summary>
17+
/// Test generate rtf file
18+
/// after execute this function you can open c:\a.rtf
19+
/// </summary>
20+
[Test]
21+
public void TestWriteFile()
22+
{
23+
string file = Path.GetFullPath("a.rtf");
24+
RTFWriter w = new RTFWriter(file);
25+
Helper.TestBuildRTF(w);
26+
w.Close();
27+
System.Windows.Forms.MessageBox.Show($"OK, you can open file {file} now.");
28+
}
29+
30+
/// <summary>
31+
/// Test generate rtf text and copy to windows clipboard
32+
/// after execute this function , you can paste rtf text in MS Word
33+
/// </summary>
34+
[Test]
35+
[RequiresThread(ApartmentState.STA)]
36+
public void TestClipboard()
37+
{
38+
System.IO.StringWriter myStr = new System.IO.StringWriter();
39+
RTFWriter w = new RTFWriter(myStr);
40+
Helper.TestBuildRTF(w);
41+
w.Close();
42+
System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
43+
data.SetData(System.Windows.Forms.DataFormats.Rtf, myStr.ToString());
44+
System.Windows.Forms.Clipboard.SetDataObject(data, true);
45+
System.Windows.Forms.MessageBox.Show("OK, you can paste words in MS Word.");
46+
}
47+
}
48+
}

Source/RtfDomParser/RTFWriter.cs

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,94 +19,9 @@ namespace RtfDomParser
1919
/// </summary>
2020
public class RTFWriter : System.IDisposable
2121
{
22-
23-
#region test ******************************************************
24-
25-
/// <summary>
26-
/// Test generate rtf file
27-
/// after execute this function you can open c:\a.rtf
28-
/// </summary>
29-
internal static void TestWriteFile( )
30-
{
31-
RTFWriter w = new RTFWriter( "c:\\a.rtf" ) ;
32-
TestBuildRTF( w );
33-
w.Close();
34-
System.Windows.Forms.MessageBox.Show("OK , you can open file c:\\a.rtf 了.");
35-
}
36-
37-
/// <summary>
38-
/// Test generate rtf text and copy to windows clipboard
39-
/// after execute this function , you can paste rtf text in MS Word
40-
/// </summary>
41-
internal static void TestClipboard()
42-
{
43-
System.IO.StringWriter myStr = new System.IO.StringWriter();
44-
RTFWriter w = new RTFWriter( myStr );
45-
TestBuildRTF( w );
46-
w.Close();
47-
System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
48-
data.SetData( System.Windows.Forms.DataFormats.Rtf , myStr.ToString());
49-
System.Windows.Forms.Clipboard.SetDataObject( data , true );
50-
System.Windows.Forms.MessageBox.Show("OK, you can paste words in MS Word.");
51-
}
5222
static RTFWriter() => Defaults.LoadEncodings();
5323

54-
/// <summary>
55-
/// Test to generate a little rtf document
56-
/// </summary>
57-
/// <param name="w">RTF text writer</param>
58-
private static void TestBuildRTF( RTFWriter w )
59-
{
60-
w.Encoding = System.Text.Encoding.GetEncoding( 936 );
61-
// write header
62-
w.WriteStartGroup();
63-
w.WriteKeyword("rtf1");
64-
w.WriteKeyword("ansi");
65-
w.WriteKeyword("ansicpg" + w.Encoding.CodePage );
66-
// wirte font table
67-
w.WriteStartGroup();
68-
w.WriteKeyword("fonttbl");
69-
w.WriteStartGroup();
70-
w.WriteKeyword("f0");
71-
w.WriteText("Arial;");
72-
w.WriteEndGroup();
73-
w.WriteStartGroup();
74-
w.WriteKeyword("f1");
75-
w.WriteText("Times New Roman;");
76-
w.WriteEndGroup();
77-
w.WriteEndGroup();
78-
// write color table
79-
w.WriteStartGroup();
80-
w.WriteKeyword("colortbl");
81-
w.WriteText(";");
82-
w.WriteKeyword("red0");
83-
w.WriteKeyword("green0");
84-
w.WriteKeyword("blue255");
85-
w.WriteText(";");
86-
w.WriteEndGroup();
87-
// write content
88-
w.WriteKeyword("qc"); // set alignment center
89-
w.WriteKeyword("f0"); // set font
90-
w.WriteKeyword("fs30"); // set font size
91-
w.WriteText("This is the first paragraph text ");
92-
w.WriteKeyword("cf1"); // set text color
93-
w.WriteText("Arial ");
94-
w.WriteKeyword("cf0"); // set default color
95-
w.WriteKeyword("f1"); // set font
96-
w.WriteText("Align center ABC12345");
97-
w.WriteKeyword("par"); // new paragraph
98-
w.WriteKeyword("pard"); // clear format
99-
w.WriteKeyword("f1"); // set font
100-
w.WriteKeyword("fs20"); // set font size
101-
w.WriteKeyword("cf1");
102-
w.WriteText("This is the secend paragraph Arial left alignment ABC12345");
103-
// finish
104-
w.WriteEndGroup();
105-
}
106-
107-
#endregion
108-
109-
/// <summary>
24+
/// <summary>
11025
/// Initialize instance
11126
/// </summary>
11227
/// <param name="w">text writer</param>

0 commit comments

Comments
 (0)