Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/core/lib/text.test.ts
6456 views
1
/*
2
* core/lib/text.test.ts
3
*
4
* Copyright (C) 2025 Posit Software, PBC
5
*/
6
7
import { unitTest } from "../../../test.ts";
8
import { assertEquals } from "testing/asserts";
9
import { getEndingNewlineCount, lines } from "../../../../src/core/lib/text.ts";
10
11
unitTest("core/lib/text.ts - getEndingNewlineCount", async () => {
12
// Test case 1: No trailing newlines
13
assertEquals(getEndingNewlineCount(["content without newlines"]), 0);
14
assertEquals(getEndingNewlineCount(["line1", "line2", "line3"]), 0);
15
16
// Test case 2: Single line with trailing newlines
17
assertEquals(getEndingNewlineCount(["content\n"]), 1);
18
assertEquals(getEndingNewlineCount(["content\n\n\n"]), 3);
19
20
// Test case 3: Multiple lines with the last line having trailing newlines
21
assertEquals(getEndingNewlineCount(["line1", "line2", "line3\n\n"]), 2);
22
assertEquals(getEndingNewlineCount(["line1\n", "line2\n", "line3\n\n\n"]), 3);
23
24
// Test case 4: Empty lines at the end
25
assertEquals(getEndingNewlineCount(["content", "", ""]), 0);
26
27
// Test case 5: Lines with only newlines at the end
28
assertEquals(getEndingNewlineCount(["content", "\n", "\n\n"]), 3);
29
assertEquals(getEndingNewlineCount(["content", "\n\n\n"]), 3);
30
31
// Test case 6: Mixed scenario with empty lines and lines with only newlines
32
assertEquals(getEndingNewlineCount(["content\n", "", "\n\n", ""]), 3);
33
assertEquals(getEndingNewlineCount(["content", "", "\n", "\n\n", ""]), 3);
34
35
// Test case 7: Edge case with only empty strings
36
assertEquals(getEndingNewlineCount(["", "", ""]), 0);
37
38
// Test case 8: Edge case with an empty array
39
assertEquals(getEndingNewlineCount([]), 0);
40
41
// Test case 9: Content after newlines breaks the counting
42
assertEquals(getEndingNewlineCount(["line1\n\n", "line2"]), 0);
43
assertEquals(getEndingNewlineCount(["line1", "line2\n\n", "line3"]), 0);
44
45
// Test case 10: Complex scenario with mixed content
46
assertEquals(
47
getEndingNewlineCount([
48
"some content",
49
"more content\n",
50
"\n",
51
"",
52
"final line\n\n",
53
]),
54
2,
55
);
56
assertEquals(
57
getEndingNewlineCount([
58
"line1",
59
"line2",
60
"line3\nwith content",
61
"\n",
62
"\n\n",
63
]),
64
3,
65
);
66
});
67
68
// Test for lines() function with different line endings
69
// See: https://github.com/quarto-dev/quarto-cli/issues/13998
70
unitTest("core/lib/text.ts - lines() with different line endings", async () => {
71
// LF (Unix/Linux)
72
assertEquals(lines("a\nb\nc"), ["a", "b", "c"]);
73
74
// CRLF (Windows)
75
assertEquals(lines("a\r\nb\r\nc"), ["a", "b", "c"]);
76
77
// CR-only (old Mac) - the fix for #13998
78
assertEquals(lines("a\rb\rc"), ["a", "b", "c"]);
79
80
// Mixed endings
81
assertEquals(lines("a\rb\nc\r\nd"), ["a", "b", "c", "d"]);
82
83
// YAML front matter with CR-only
84
const yaml = "---\rtitle: \"Test\"\r---";
85
assertEquals(lines(yaml), ["---", "title: \"Test\"", "---"]);
86
87
// Empty string
88
assertEquals(lines(""), [""]);
89
90
// Single line without newline
91
assertEquals(lines("single"), ["single"]);
92
93
// Trailing newlines
94
assertEquals(lines("a\n"), ["a", ""]);
95
assertEquals(lines("a\r"), ["a", ""]);
96
assertEquals(lines("a\r\n"), ["a", ""]);
97
});
98
99