@@ -7,6 +7,7 @@ import { createContext } from 'node:vm';
7
7
import { handleTemplateFile , parseFilterExpression , template } from '../../src/transforms/template-data.js' ;
8
8
import { SissiConfig } from '../../src/sissi-config.js' ;
9
9
import md from '../../src/md.js' ;
10
+ import * as builtinFilters from '../../src/builtin-filters.js' ;
10
11
11
12
const TEST_DATA = {
12
13
'title' : 'This is a title' ,
@@ -71,56 +72,65 @@ describe('parseFilterExpression function', () => {
71
72
} ) ;
72
73
73
74
describe ( 'template function' , ( ) => {
74
- it ( 'should insert data into the placeholders wrapped in double curly brackets' , ( ) => {
75
- assert . equal ( template ( TEST_TEMPLATE ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED ) ;
75
+ it ( 'should insert data into the placeholders wrapped in double curly brackets' , async ( ) => {
76
+ assert . equal ( await template ( TEST_TEMPLATE ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED ) ;
76
77
} ) ;
77
78
78
- it ( 'should be able to invoke functions' , ( ) => {
79
- assert . equal ( template ( TEST_TEMPLATE_2 ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED_2 ) ;
80
- assert . equal ( template ( TEST_TEMPLATE_3 ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED_3 ) ;
79
+ it ( 'should be able to invoke functions' , async ( ) => {
80
+ assert . equal ( await template ( TEST_TEMPLATE_2 ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED_2 ) ;
81
+ assert . equal ( await template ( TEST_TEMPLATE_3 ) ( TEST_DATA ) , TEST_TEMPLATE_EXPECTED_3 ) ;
81
82
} ) ;
82
83
83
- it ( 'should be able to apply a filter' , ( ) => {
84
+ it ( 'should be able to apply a filter' , async ( ) => {
84
85
const filters = new Map ( ) ;
85
86
filters . set ( 'shout' , ( str ) => ( str || '' ) . toUpperCase ( ) ) ;
86
- const result = template ( '{{greeting | shout }}' ) ( { greeting : "Hello" } , filters ) ;
87
+ const result = await template ( '{{greeting | shout }}' ) ( { greeting : "Hello" } , filters ) ;
87
88
88
89
assert . equal ( result , "HELLO" ) ;
89
90
} ) ;
90
91
91
- it ( 'should escape angle brackets and ampersands by default' , ( ) => {
92
- const result = template ( '{{ content }}' ) ( { content : '<h1>Hello</h1>' } ) ;
92
+ it ( 'should escape angle brackets and ampersands by default' , async ( ) => {
93
+ const result = await template ( '{{ content }}' ) ( { content : '<h1>Hello</h1>' } ) ;
93
94
94
95
assert . equal ( result , '<h1>Hello</h1>' )
95
96
} ) ;
96
97
97
- it ( 'should not escape angle brackets and ampersands when marked safe' , ( ) => {
98
- const result = template ( '{{ content | safe }}' ) ( { content : '<h1>Hello</h1>' } ) ;
98
+ it ( 'should not escape angle brackets and ampersands when marked safe' , async ( ) => {
99
+ const result = await template ( '{{ content | safe }}' ) ( { content : '<h1>Hello</h1>' } ) ;
99
100
100
101
assert . equal ( result , '<h1>Hello</h1>' )
101
102
} ) ;
102
103
103
- it ( 'should be able to apply a filter with additional parameters' , ( ) => {
104
+ it ( 'should be able to apply a filter with additional parameters' , async ( ) => {
104
105
const data = { greeting : 'Hello Lea' }
105
106
const filters = new Map ( ) ;
106
107
filters . set ( 'piratify' , ( str , prefix = 'Yo-ho-ho' , suffix = 'yarrr' ) => `${ prefix } ! ${ str } , ${ suffix } !` ) ;
107
108
108
- assert . equal ( template ( '{{ greeting | piratify }}' ) ( data , filters ) , 'Yo-ho-ho! Hello Lea, yarrr!' ) ;
109
- assert . equal ( template ( '{{ greeting | piratify: "AYE" }}' ) ( data , filters ) , 'AYE! Hello Lea, yarrr!' ) ;
110
- assert . equal ( template ( '{{ greeting | piratify: "Ahoy", "matey" }}' ) ( data , filters ) , 'Ahoy! Hello Lea, matey!' ) ;
109
+ assert . equal ( await template ( '{{ greeting | piratify }}' ) ( data , filters ) , 'Yo-ho-ho! Hello Lea, yarrr!' ) ;
110
+ assert . equal ( await template ( '{{ greeting | piratify: "AYE" }}' ) ( data , filters ) , 'AYE! Hello Lea, yarrr!' ) ;
111
+ assert . equal ( await template ( '{{ greeting | piratify: "Ahoy", "matey" }}' ) ( data , filters ) , 'Ahoy! Hello Lea, matey!' ) ;
111
112
} ) ;
112
113
113
- it ( 'should be able to chain filters' , ( ) => {
114
+ it ( 'should be able to chain filters' , async ( ) => {
114
115
const filters = new Map ( ) ;
115
116
filters . set ( 'shout' , ( str ) => ( str || '' ) . toUpperCase ( ) ) ;
116
117
filters . set ( 'piratify' , ( str , prefix = 'Yo-ho-ho' , suffix = 'yarrr' ) => `${ prefix } ! ${ str } , ${ suffix } !` ) ;
117
118
118
119
const data = { greeting : 'Hello Lea' } ;
119
- assert . equal ( template ( '{{ greeting | piratify | shout }}' ) ( data , filters ) , 'YO-HO-HO! HELLO LEA, YARRR!' ) ;
120
+ assert . equal ( await template ( '{{ greeting | piratify | shout }}' ) ( data , filters ) , 'YO-HO-HO! HELLO LEA, YARRR!' ) ;
120
121
121
122
// order matters
122
- assert . equal ( template ( '{{ greeting | shout | piratify }}' ) ( data , filters ) , 'Yo-ho-ho! HELLO LEA, yarrr!' ) ;
123
+ assert . equal ( await template ( '{{ greeting | shout | piratify }}' ) ( data , filters ) , 'Yo-ho-ho! HELLO LEA, yarrr!' ) ;
123
124
} ) ;
125
+
126
+ it ( 'should be able to do async operations' , async ( ) => {
127
+ const filters = new Map ( ) ;
128
+ filters . set ( 'async' , builtinFilters . async ) ;
129
+ const data = { answer : ( ) => Promise . resolve ( 42 ) } ;
130
+
131
+ assert . equal ( await ( template ( '{{ answer | async }}' ) ( data , filters ) ) , '42' ) ;
132
+ } ) ;
133
+
124
134
} ) ;
125
135
126
136
describe ( 'handleTemplateFile function' , ( ) => {
0 commit comments