@@ -2,10 +2,13 @@ import anyTest, { TestFn } from 'ava';
22
33import {
44 CommodityText ,
5+ INDENT ,
6+ InlineCommentText ,
57 JournalNumber ,
68 NEWLINE ,
79 PDirective ,
810 PDirectiveCommodityText ,
11+ SemicolonComment ,
912 SimpleDate
1013} from '../../lib/lexer/tokens' ;
1114import HLedgerParser from '../../lib/parser' ;
@@ -88,3 +91,169 @@ test('does not parse a price directive line without a date', (t) => {
8891
8992 t . falsy ( HLedgerParser . priceDirective ( ) , '<priceDirective!> P € $1.50\\n' ) ;
9093} ) ;
94+
95+ test ( 'parses a price directive with an inline comment' , ( t ) => {
96+ t . context . lexer
97+ . addToken ( PDirective , 'P' )
98+ . addToken ( SimpleDate , '2024.01.01' )
99+ . addToken ( PDirectiveCommodityText , '€' )
100+ . addToken ( CommodityText , '¥' )
101+ . addToken ( JournalNumber , '15000' )
102+ . addToken ( SemicolonComment , ';' )
103+ . addToken ( InlineCommentText , 'a comment' )
104+ . addToken ( NEWLINE , '\n' ) ;
105+ HLedgerParser . input = t . context . lexer . tokenize ( ) ;
106+
107+ t . deepEqual (
108+ simplifyCst ( HLedgerParser . priceDirective ( ) ) ,
109+ {
110+ PDirective : 1 ,
111+ SimpleDate : 1 ,
112+ PDirectiveCommodityText : 1 ,
113+ NEWLINE : 1 ,
114+ amount : [
115+ {
116+ CommodityText : 1 ,
117+ Number : 1
118+ }
119+ ] ,
120+ inlineComment : [
121+ {
122+ SemicolonComment : 1 ,
123+ inlineCommentItem : [
124+ {
125+ InlineCommentText : 1
126+ }
127+ ]
128+ }
129+ ]
130+ } ,
131+ '<priceDirective> P 2024.01.01 € ¥15000 ; a comment\\n'
132+ ) ;
133+ } ) ;
134+
135+ test ( 'parses a price directive with an inline comment and subdirective comment' , ( t ) => {
136+ t . context . lexer
137+ . addToken ( PDirective , 'P' )
138+ . addToken ( SimpleDate , '2024.01.01' )
139+ . addToken ( PDirectiveCommodityText , '€' )
140+ . addToken ( CommodityText , '¥' )
141+ . addToken ( JournalNumber , '15000' )
142+ . addToken ( SemicolonComment , ';' )
143+ . addToken ( InlineCommentText , 'a comment' )
144+ . addToken ( NEWLINE , '\n' )
145+ . addToken ( INDENT , ' ' )
146+ . addToken ( SemicolonComment , ';' )
147+ . addToken ( InlineCommentText , 'subdirective comment' )
148+ . addToken ( NEWLINE , '\n' ) ;
149+ HLedgerParser . input = t . context . lexer . tokenize ( ) ;
150+
151+ t . deepEqual (
152+ simplifyCst ( HLedgerParser . priceDirective ( ) ) ,
153+ {
154+ PDirective : 1 ,
155+ SimpleDate : 1 ,
156+ PDirectiveCommodityText : 1 ,
157+ NEWLINE : 1 ,
158+ amount : [
159+ {
160+ CommodityText : 1 ,
161+ Number : 1
162+ }
163+ ] ,
164+ inlineComment : [
165+ {
166+ SemicolonComment : 1 ,
167+ inlineCommentItem : [
168+ {
169+ InlineCommentText : 1
170+ }
171+ ]
172+ }
173+ ] ,
174+ priceDirectiveContentLine : [
175+ {
176+ INDENT : 1 ,
177+ NEWLINE : 1 ,
178+ inlineComment : [
179+ {
180+ SemicolonComment : 1 ,
181+ inlineCommentItem : [
182+ {
183+ InlineCommentText : 1
184+ }
185+ ]
186+ }
187+ ]
188+ }
189+ ]
190+ } ,
191+ '<priceDirective> P 2024.01.01 € ¥15000 ; a comment\\n ; subdirective comment\\n'
192+ ) ;
193+ } ) ;
194+
195+ test ( 'parses a price directive with several subdirective comments' , ( t ) => {
196+ t . context . lexer
197+ . addToken ( PDirective , 'P' )
198+ . addToken ( SimpleDate , '2024.01.01' )
199+ . addToken ( PDirectiveCommodityText , '€' )
200+ . addToken ( CommodityText , '¥' )
201+ . addToken ( JournalNumber , '15000' )
202+ . addToken ( NEWLINE , '\n' )
203+ . addToken ( INDENT , ' ' )
204+ . addToken ( SemicolonComment , ';' )
205+ . addToken ( InlineCommentText , 'subdirective comment' )
206+ . addToken ( NEWLINE , '\n' )
207+ . addToken ( INDENT , ' ' )
208+ . addToken ( SemicolonComment , ';' )
209+ . addToken ( InlineCommentText , 'another comment' )
210+ . addToken ( NEWLINE , '\n' ) ;
211+ HLedgerParser . input = t . context . lexer . tokenize ( ) ;
212+
213+ t . deepEqual (
214+ simplifyCst ( HLedgerParser . priceDirective ( ) ) ,
215+ {
216+ PDirective : 1 ,
217+ SimpleDate : 1 ,
218+ PDirectiveCommodityText : 1 ,
219+ NEWLINE : 1 ,
220+ amount : [
221+ {
222+ CommodityText : 1 ,
223+ Number : 1
224+ }
225+ ] ,
226+ priceDirectiveContentLine : [
227+ {
228+ INDENT : 1 ,
229+ NEWLINE : 1 ,
230+ inlineComment : [
231+ {
232+ SemicolonComment : 1 ,
233+ inlineCommentItem : [
234+ {
235+ InlineCommentText : 1
236+ }
237+ ]
238+ }
239+ ]
240+ } ,
241+ {
242+ INDENT : 1 ,
243+ NEWLINE : 1 ,
244+ inlineComment : [
245+ {
246+ SemicolonComment : 1 ,
247+ inlineCommentItem : [
248+ {
249+ InlineCommentText : 1
250+ }
251+ ]
252+ }
253+ ]
254+ }
255+ ]
256+ } ,
257+ '<priceDirective> P 2024.01.01 € ¥15000\\n ; subdirective comment\\n ; another comment\\n'
258+ ) ;
259+ } ) ;
0 commit comments