Skip to content

Commit fdf559e

Browse files
authored
test: add test for findOffsets() util (#420)
1 parent 8b77e69 commit fdf559e

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

tests/util.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @fileoverview Tests for util.js
3+
* @author 루밀LuMir(lumirlumir)
4+
*/
5+
6+
//------------------------------------------------------------------------------
7+
// Imports
8+
//------------------------------------------------------------------------------
9+
10+
import assert from "node:assert";
11+
import { findOffsets } from "../src/util.js";
12+
13+
//------------------------------------------------------------------------------
14+
// Tests
15+
//------------------------------------------------------------------------------
16+
17+
describe("util", () => {
18+
describe("findOffsets()", () => {
19+
it("should return correct offsets for a simple string", () => {
20+
const text = "Hello world!";
21+
const offset = 6; // 'w' in "world"
22+
const result = findOffsets(text, offset);
23+
assert.deepStrictEqual(result, { lineOffset: 0, columnOffset: 6 });
24+
});
25+
26+
it("should handle line breaks correctly", () => {
27+
const text = "Hello\nworld!";
28+
const offset = 6; // 'w' in "world"
29+
const result = findOffsets(text, offset);
30+
assert.deepStrictEqual(result, { lineOffset: 1, columnOffset: 0 });
31+
});
32+
33+
it("should handle Windows-style line endings", () => {
34+
const text = "Hello\r\nworld!";
35+
const offset = 7; // 'w' in "world"
36+
const result = findOffsets(text, offset);
37+
assert.deepStrictEqual(result, { lineOffset: 1, columnOffset: 0 });
38+
});
39+
40+
it("should handle offsets at the start of the string", () => {
41+
const text = "Hello, world!";
42+
const offset = 0; // Start of the string
43+
const result = findOffsets(text, offset);
44+
assert.deepStrictEqual(result, { lineOffset: 0, columnOffset: 0 });
45+
});
46+
47+
it("should handle offsets at the end of the string", () => {
48+
const text = "Hello, world!";
49+
const offset = text.length - 1; // Last character '!'
50+
const result = findOffsets(text, offset);
51+
assert.deepStrictEqual(result, {
52+
lineOffset: 0,
53+
columnOffset: text.length - 1,
54+
});
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)