|
| 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 } 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 | +}); |
0 commit comments