-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathCodeViewer.test.tsx
More file actions
83 lines (66 loc) · 2.62 KB
/
CodeViewer.test.tsx
File metadata and controls
83 lines (66 loc) · 2.62 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
import { describe, it, expect } from 'vitest';
// Test the escapeHtml and string regex patterns used in CodeViewer
// We test the logic directly since the component uses internal functions
describe('CodeViewer escapeHtml', () => {
// Replicate the escapeHtml function from CodeViewer
const escapeHtml = (str: string) => str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
it('escapes double quotes for attribute safety', () => {
expect(escapeHtml('class="foo"')).toBe('class="foo"');
});
it('escapes single quotes for attribute safety', () => {
expect(escapeHtml("class='foo'")).toBe("class='foo'");
});
it('escapes HTML tags', () => {
expect(escapeHtml('<div>')).toBe('<div>');
});
it('escapes ampersands', () => {
expect(escapeHtml('a && b')).toBe('a && b');
});
});
describe('CodeViewer string regex patterns', () => {
// Test the improved string patterns
const doubleQuotePattern = /"(?:[^"\\]|\\.)*"/;
const singleQuotePattern = /'(?:[^'\\]|\\.)*'/;
const backtickPattern = /`(?:[^`\\]|\\.)*`/;
describe('double-quoted strings', () => {
it('matches simple double-quoted strings', () => {
expect('"hello"'.match(doubleQuotePattern)?.[0]).toBe('"hello"');
});
it('matches strings with escaped quotes', () => {
expect('"He said \\"hello\\""'.match(doubleQuotePattern)?.[0]).toBe('"He said \\"hello\\""');
});
it('matches strings with escaped backslashes', () => {
expect('"path\\\\to\\\\file"'.match(doubleQuotePattern)?.[0]).toBe('"path\\\\to\\\\file"');
});
it('matches empty strings', () => {
expect('""'.match(doubleQuotePattern)?.[0]).toBe('""');
});
});
describe('single-quoted strings', () => {
it('matches simple single-quoted strings', () => {
expect("'hello'".match(singleQuotePattern)?.[0]).toBe("'hello'");
});
it('matches strings with escaped quotes', () => {
expect("'It\\'s fine'".match(singleQuotePattern)?.[0]).toBe("'It\\'s fine'");
});
it('matches empty strings', () => {
expect("''".match(singleQuotePattern)?.[0]).toBe("''");
});
});
describe('backtick strings', () => {
it('matches simple backtick strings', () => {
expect('`hello`'.match(backtickPattern)?.[0]).toBe('`hello`');
});
it('matches strings with escaped backticks', () => {
expect('`use \\`code\\``'.match(backtickPattern)?.[0]).toBe('`use \\`code\\``');
});
it('matches empty strings', () => {
expect('``'.match(backtickPattern)?.[0]).toBe('``');
});
});
});