Skip to content

Commit 6ce3761

Browse files
committed
4.5.0: test/ts: adds more test cases for ts
1 parent b875787 commit 6ce3761

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/ts/test-ts.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
CommentArray,
77
CommentObject,
88
assign,
9+
moveComments,
10+
removeComments,
911
CommentDescriptor,
1012
CommentSymbol
1113
} from '../..'
@@ -46,3 +48,83 @@ const commentSrc = `[
4648

4749
assert((parse(commentSrc) as CommentArray<string>)[Symbol.for(commentDescriptor) as CommentSymbol][0].value === comment, 'comment parse')
4850
commentDescriptor = "before";
51+
52+
// Test moveComments function
53+
const moveCommentsTest = parse(`{
54+
"foo": 1, // comment on foo
55+
"bar": 2
56+
}`) as CommentObject
57+
58+
moveComments(moveCommentsTest, moveCommentsTest,
59+
{ where: 'after', key: 'foo' },
60+
{ where: 'before', key: 'bar' }
61+
)
62+
63+
const moveResult = stringify(moveCommentsTest, null, 2)
64+
assert(moveResult.includes('// comment on foo'), 'moveComments basic functionality')
65+
66+
// Test moveComments with non-property comments
67+
const moveNonPropTest = parse(`// top comment
68+
{
69+
"foo": 1
70+
}`) as CommentObject
71+
72+
moveComments(moveNonPropTest, moveNonPropTest,
73+
{ where: 'before-all' },
74+
{ where: 'after-all' }
75+
)
76+
77+
const moveNonPropResult = stringify(moveNonPropTest, null, 2)
78+
assert(moveNonPropResult.includes('// top comment'), 'moveComments non-property comments')
79+
80+
// Test moveComments between different objects
81+
const sourceObj = parse(`{
82+
"source": 1 // source comment
83+
}`) as CommentObject
84+
85+
const targetObj = parse('{"target": 2}') as CommentObject
86+
87+
moveComments(sourceObj, targetObj,
88+
{ where: 'after', key: 'source' },
89+
{ where: 'before', key: 'target' }
90+
)
91+
92+
const crossObjectResult = stringify(targetObj, null, 2)
93+
assert(crossObjectResult.includes('// source comment'), 'moveComments cross-object')
94+
95+
// Test removeComments function
96+
const removeTest = parse(`{
97+
// comment before foo
98+
"foo": 1, // comment after foo
99+
"bar": 2
100+
}`) as CommentObject
101+
102+
removeComments(removeTest, { where: 'before', key: 'foo' })
103+
104+
const removeResult = stringify(removeTest, null, 2)
105+
assert(!removeResult.includes('// comment before foo'), 'removeComments removes comment')
106+
assert(removeResult.includes('// comment after foo'), 'removeComments preserves other comments')
107+
108+
// Test removeComments with non-property comments
109+
const removeNonPropTest = parse(`// top comment
110+
{
111+
"foo": 1
112+
}
113+
// bottom comment`) as CommentObject
114+
115+
removeComments(removeNonPropTest, { where: 'before-all' })
116+
removeComments(removeNonPropTest, { where: 'after-all' })
117+
118+
const removeNonPropResult = stringify(removeNonPropTest, null, 2)
119+
assert(!removeNonPropResult.includes('// top comment'), 'removeComments removes before-all')
120+
assert(!removeNonPropResult.includes('// bottom comment'), 'removeComments removes after-all')
121+
122+
// Test TypeScript type safety for CommentPosition
123+
const validPosition: { where: 'before', key?: string } = { where: 'before', key: 'test' }
124+
const validNonPropPosition: { where: 'before-all', key?: string } = { where: 'before-all' }
125+
126+
// These should compile without errors
127+
moveComments(moveCommentsTest, moveCommentsTest, validPosition, validNonPropPosition)
128+
removeComments(removeTest, validPosition)
129+
130+
console.log('All TypeScript tests passed!')

0 commit comments

Comments
 (0)