-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathMarkdownJavaScriptTests.cs
More file actions
317 lines (259 loc) · 8.35 KB
/
MarkdownJavaScriptTests.cs
File metadata and controls
317 lines (259 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using JSTest;
using JSTest.ScriptLibraries;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Threading.Tasks;
using Waher.Content.Emoji.Emoji1;
using Waher.Content.Markdown.JavaScript;
using Waher.Runtime.Console;
using Waher.Runtime.IO;
namespace Waher.Content.Markdown.Test
{
[TestClass]
public class MarkdownJavaScriptTests
{
private static Task DoTest(string MarkdownFileName, string JavaScriptFileName)
{
return DoTest(MarkdownFileName, JavaScriptFileName, true);
}
private static Task DoTest(string MarkdownFileName, string JavaScriptFileName, bool ExecuteJavaScript)
{
return DoTest(MarkdownFileName, JavaScriptFileName, ExecuteJavaScript, "<body>", "</body>");
}
private static async Task DoTest(string MarkdownFileName, string JavaScriptFileName, bool ExecuteJavaScript,
string HtmlStartTag, string HtmlEndTag)
{
string Markdown = await Files.ReadAllTextAsync("Markdown/Syntax/" + MarkdownFileName);
string ExpectedJavaScript = await Files.ReadAllTextAsync("JavaScript/" + JavaScriptFileName);
string ExpectedHtml = await Files.ReadAllTextAsync("HTML/" + Path.ChangeExtension(JavaScriptFileName, ".html"));
Emoji1LocalFiles Emoji1LocalFiles = new(Emoji1SourceFileType.Svg, 24, 24, "/emoji1/%FILENAME%", Path.Combine("Graphics", "Emoji1.zip"), "Graphics");
MarkdownSettings Settings = new(Emoji1LocalFiles, true, [])
{
HttpxProxy = "/HttpxProxy/%URL%"
};
Assert.IsTrue(Emoji1LocalFiles.WaitUntilInitialized(60000));
MarkdownDocument Doc = await MarkdownDocument.CreateAsync(Markdown, Settings);
string GeneratedJavaScript = await Doc.GenerateJavaScript();
ConsoleOut.WriteLine(GeneratedJavaScript);
ConsoleOut.WriteLine();
ConsoleOut.WriteLine();
ConsoleOut.WriteLine();
ExpectedJavaScript = ExpectedJavaScript.Replace("\\r\\n", "\\n").Replace("\\r", "\\n");
GeneratedJavaScript = GeneratedJavaScript.Replace("\\r\\n", "\\n").Replace("\\r", "\\n");
MarkdownHtmlTests.AssertEqual(ExpectedJavaScript, GeneratedJavaScript, "Generated JavaScript does not match expected JavaScript.");
if (ExecuteJavaScript)
{
// Ref: https://github.com/cbaxter/JSTest.NET/wiki
TestScript Parsed = new();
Parsed.AppendBlock(new JsAssertLibrary());
string JsFileName = Path.ChangeExtension(Path.GetTempFileName(), ".js");
await Files.WriteAllTextAsync(JsFileName, GeneratedJavaScript, System.Text.Encoding.UTF8);
Parsed.AppendFile(JsFileName);
string JsonEncoded = Parsed.RunTest("return CreateHTML();");
// JavaScript engine not encoding quotes correctly:
JsonEncoded = JsonEncoded.
Replace("\"", "\\\"").
Replace("\\\\\"", "\\\"");
if (JsonEncoded.StartsWith("\\\""))
JsonEncoded = JsonEncoded[1..];
if (JsonEncoded.EndsWith("\\\""))
JsonEncoded = JsonEncoded[..^2] + "\"";
ConsoleOut.WriteLine("JSON Returned after executing JavaScript:");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine(JsonEncoded);
ConsoleOut.WriteLine();
object Decoded = JSON.Parse(JsonEncoded);
if (Decoded is not string GeneratedHtml)
throw new Exception("Expected a string result returned from the JavaScript");
ConsoleOut.WriteLine("Generated HTML by executing JavaScript:");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine(GeneratedHtml);
int i = ExpectedHtml.IndexOf(HtmlStartTag);
if (i > 0)
ExpectedHtml = ExpectedHtml[(i + HtmlStartTag.Length)..].TrimStart();
i = ExpectedHtml.IndexOf(HtmlEndTag);
if (i >= 0)
ExpectedHtml = ExpectedHtml[..i];
// JavaScript engine not honoring special characters:
ExpectedHtml = ExpectedHtml.
Replace('’', '\'').
Replace('—', '-').
Replace('“', '"').
Replace('”', '"').
Replace('™','T');
MarkdownHtmlTests.AssertEqual(ExpectedHtml, GeneratedHtml, "Generated HTML does not match expected HTML.");
}
}
[TestMethod]
public async Task Test_01_Paragraphs()
{
await DoTest("Test_01_Paragraphs.md", "Test_01_Paragraphs.js");
}
[TestMethod]
public async Task Test_02_Links()
{
await DoTest("Test_02_Links.md", "Test_02_Links.js");
}
[TestMethod]
public async Task Test_03_TextFormatting()
{
await DoTest("Test_03_TextFormatting.md", "Test_03_TextFormatting.js");
}
[TestMethod]
public async Task Test_04_Multimedia()
{
await DoTest("Test_04_Multimedia.md", "Test_04_Multimedia.js");
}
[TestMethod]
public async Task Test_05_HTML()
{
await DoTest("Test_05_HTML.md", "Test_05_HTML.js");
}
[TestMethod]
public async Task Test_06_CodeBlocks()
{
await DoTest("Test_06_CodeBlocks.md", "Test_06_CodeBlocks.js");
}
[TestMethod]
public async Task Test_07_BlockQuotes()
{
await DoTest("Test_07_BlockQuotes.md", "Test_07_BlockQuotes.js");
}
[TestMethod]
public async Task Test_08_Headers()
{
await DoTest("Test_08_Headers.md", "Test_08_Headers.js");
}
[TestMethod]
public async Task Test_09_UnorderedLists()
{
await DoTest("Test_09_UnorderedLists.md", "Test_09_UnorderedLists.js");
}
[TestMethod]
public async Task Test_10_LazyOrderedLists()
{
await DoTest("Test_10_LazyOrderedLists.md", "Test_10_LazyOrderedLists.js");
}
[TestMethod]
public async Task Test_11_OrderedLists()
{
await DoTest("Test_11_OrderedLists.md", "Test_11_OrderedLists.js");
}
[TestMethod]
public async Task Test_12_Typography()
{
await DoTest("Test_12_Typography.md", "Test_12_Typography.js");
}
[TestMethod]
public async Task Test_13_Tables()
{
await DoTest("Test_13_Tables.md", "Test_13_Tables.js");
}
[TestMethod]
public async Task Test_14_HorizontalRules()
{
await DoTest("Test_14_HorizontalRules.md", "Test_14_HorizontalRules.js");
}
[TestMethod]
public async Task Test_15_DefinitionLists()
{
await DoTest("Test_15_DefinitionLists.md", "Test_15_DefinitionLists.js");
}
[TestMethod]
public async Task Test_16_MetaData()
{
await DoTest("Test_16_MetaData.md", "Test_16_MetaData.js");
}
[TestMethod]
public async Task Test_17_Footnotes()
{
await DoTest("Test_17_Footnotes.md", "Test_17_Footnotes.js");
}
[TestMethod]
public async Task Test_18_Emojis()
{
await DoTest("Test_18_Emojis.md", "Test_18_Emojis.js");
}
[TestMethod]
public async Task Test_19_Sections()
{
await DoTest("Test_19_Sections.md", "Test_19_Sections.js");
}
[TestMethod]
public async Task Test_20_Script()
{
await DoTest("Test_20_Script.md", "Test_20_Script.js", false);
}
[TestMethod]
public async Task Test_21_Httpx()
{
await DoTest("Test_21_Httpx.md", "Test_21_Httpx.js");
}
[TestMethod]
public async Task Test_22_TaskLists()
{
await DoTest("Test_22_TaskLists.md", "Test_22_TaskLists.js");
}
[TestMethod]
public async Task Test_23_Superscript()
{
await DoTest("Test_23_Superscript.md", "Test_23_Superscript.js");
}
[TestMethod]
public async Task Test_24_Subscript()
{
await DoTest("Test_24_Subscript.md", "Test_24_Subscript.js");
}
[TestMethod]
public async Task Test_25_HashTags()
{
await DoTest("Test_25_HashTags.md", "Test_25_HashTags.js");
}
[TestMethod]
public async Task Test_26_Comments()
{
await DoTest("Test_26_Comments.md", "Test_26_Comments.js");
}
[TestMethod]
public async Task Test_27_Contract()
{
await DoTest("Test_27_Contract.md", "Test_27_Contract.js");
}
[TestMethod]
public async Task Test_28_Nesting()
{
await DoTest("Test_28_Nesting.md", "Test_28_Nesting.js");
}
[TestMethod]
public async Task Test_29_Justification()
{
await DoTest("Test_29_Justification.md", "Test_29_Justification.js");
}
[TestMethod]
public async Task Test_30_Incomplete()
{
await DoTest("Test_30_Incomplete.md", "Test_30_Incomplete.js");
}
[TestMethod]
public async Task Test_31_Justification2()
{
await DoTest("Test_31_Justification2.md", "Test_31_Justification2.js");
}
[TestMethod]
public async Task Test_32_TablesAndNotes()
{
await DoTest("Test_32_TablesAndNotes.md", "Test_32_TablesAndNotes.js");
}
[TestMethod]
public async Task Test_33_SingleNoHeaderTable()
{
await DoTest("Test_33_SingleNoHeaderTable.md", "Test_33_SingleNoHeaderTable.js", true, "<tbody>", "</tbody>");
}
[TestMethod]
public async Task Test_34_SpecifiedViewportHeader()
{
await DoTest("Test_34_SpecifiedViewportHeader.md", "Test_34_SpecifiedViewportHeader.js");
}
}
}