11const BodyParser = require ( '../lib/snippet-body-parser' ) ;
22
33describe ( "Snippet Body Parser" , ( ) => {
4- function t ( snippetBody ) {
5- return BodyParser . parse ( snippetBody ) ;
4+ function expectMatch ( input , tree ) {
5+ expect ( BodyParser . parse ( input ) ) . toEqual ( tree ) ;
66 }
77
8- it ( "breaks a snippet body into lines, with each line containing tab stops at the appropriate position" , ( ) => {
9- const bodyTree = BodyParser . parse ( `\
10- the quick brown $1fox \${2:jumped \${3:over}
11- }the \${4:lazy} dog\
12- `
13- ) ;
8+ it ( "parses snippets with no special behaviour as plain text" , ( ) => {
9+ const plainSnippets = [
10+ "foo $ bar" ,
11+ "$% $ 1 ${/upcase} \n ${|world|} ${3foo}" ,
12+ ] ;
1413
15- expect ( bodyTree ) . toEqual ( [
14+ for ( const plain of plainSnippets ) {
15+ expectMatch ( plain , [ plain ] ) ;
16+ }
17+ } ) ;
18+
19+ it ( "parses simple tab stops" , ( ) => {
20+ expectMatch ( "hello$1world${2}" , [
21+ "hello" , { index : 1 , content : [ ] } , "world" , { index : 2 , content : [ ] } ,
22+ ] ) ;
23+ } ) ;
24+
25+ it ( "doesn't find escaped tab stops" , ( ) => {
26+ expectMatch ( "\\$1" , [ "$1" ] ) ;
27+ } )
28+
29+ it ( "parses simple variables" , ( ) => {
30+ expectMatch ( "hello$foo2__bar&baz${abc}d" , [
31+ "hello" , { variable : "foo2__bar" } , "&baz" , { variable : "abc" } , "d"
32+ ] ) ;
33+ } ) ;
34+
35+ describe ( "only escapes a select few characters" , ( ) => {
36+ const escapeTest = "\\$ \\\\ \\} \\% \\* \\, \\| \\{ \\n \\r \\:" ;
37+
38+ const escapeResolveTop = "$ \\ } \\% \\* \\, \\| \\{ \\n \\r \\:" ;
39+
40+ const escapeResolveChoice = "\\$ \\ \\} \\% \\* , | \\{ \\n \\r \\:" ;
41+
42+ it ( "only escapes '$', '\\', and '}' in top level text" , ( ) => {
43+ expectMatch ( escapeTest , [
44+ escapeResolveTop
45+ ] ) ;
46+ } ) ;
47+
48+ it ( "escapes the same characters inside tab stop placeholders as in top level text" , ( ) => {
49+ expectMatch ( `\${1:${ escapeTest } }` , [
50+ { index : 1 , content : [ escapeResolveTop ] } ,
51+ ] ) ;
52+ } ) ;
53+
54+ it ( "escapes the same characters inside variable placeholders as in top level text" , ( ) => {
55+ expectMatch ( `\${foo:${ escapeTest } }` , [
56+ { variable : "foo" , content : [ escapeResolveTop ] } ,
57+ ] ) ;
58+ } ) ;
59+
60+ it ( "escapes ',', '|', and '\\' in choice text" , ( ) => {
61+ expectMatch ( `\${1|${ escapeTest } |}` , [
62+ { index : 1 , choices : [ escapeResolveChoice ] } ,
63+ ] ) ;
64+ } ) ;
65+ } ) ;
66+
67+ it ( "does not recognise a tab stop with transformation if the transformation is invalid regex" , ( ) => {
68+ expectMatch ( "${1/foo/bar/a}" , [ "${1/foo/bar/a}" ] ) ; // invalid flag
69+ expectMatch ( "${1/fo)o/bar/}" , [ "${1/fo)o/bar/}" ] ) ; // invalid body
70+ } ) ;
71+
72+ it ( "breaks a snippet body into lines, with each line containing tab stops at the appropriate position" , ( ) => {
73+ expectMatch ( "the quick brown $1fox ${2:jumped ${3:over}\n}the ${4:lazy} dog" , [
1674 "the quick brown " ,
1775 { index : 1 , content : [ ] } ,
1876 "fox " ,
@@ -31,35 +89,29 @@ the quick brown $1fox \${2:jumped \${3:over}
3189 } ) ;
3290
3391 it ( "removes interpolated variables in placeholder text (we don't currently support it)" , ( ) => {
34- const bodyTree = BodyParser . parse ( "module ${1:ActiveRecord::${TM_FILENAME/(?:\\A|_)([A-Za-z0-9]+)(?:\\.rb)?/(?2::\\u$1)/g}}" ) ;
35- expect ( bodyTree ) . toEqual ( [
92+ expect ( t ( "module ${1:ActiveRecord::${TM_FILENAME/(?:\\A|_)([A-Za-z0-9]+)(?:\\.rb)?/(?2::\\u$1)/g}}" ) ) . toEqual ( [
3693 "module " ,
3794 {
38- " index" : 1 ,
39- " content" : [ "ActiveRecord::" , "" ]
95+ index : 1 ,
96+ content : [ "ActiveRecord::" , "" ]
4097 }
4198 ] ) ;
4299 } ) ;
43100
44101 it ( "skips escaped tabstops" , ( ) => {
45- const bodyTree = BodyParser . parse ( "snippet $1 escaped \\$2 \\\\$3" ) ;
46- expect ( bodyTree ) . toEqual ( [
47- "snippet " ,
48- {
49- index : 1 ,
50- content : [ ]
51- } ,
52- " escaped $2 \\" ,
53- {
54- index : 3 ,
55- content : [ ]
56- }
102+ expectMatch ( "$1 \\$2 $3 \\\\$4 \\\\\\$5 $6" , [
103+ { index : 1 , content : [ ] } ,
104+ ' $2 ' ,
105+ { index : 3 , content : [ ] } ,
106+ ' \\' ,
107+ { index : 4 , content : [ ] } ,
108+ ' \\$5 ' ,
109+ { index : 6 , content : [ ] }
57110 ] ) ;
58111 } ) ;
59112
60113 it ( "includes escaped right-braces" , ( ) => {
61- const bodyTree = BodyParser . parse ( "snippet ${1:{\\}}" ) ;
62- expect ( bodyTree ) . toEqual ( [
114+ expectMatch ( "snippet ${1:{\\}}" , [
63115 "snippet " ,
64116 {
65117 index : 1 ,
0 commit comments