-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented the year (Y) directive
The year directive can now also be used in addition to the system date to complete shorthand dates in the cooked journal. closes #13
- Loading branch information
1 parent
f68166c
commit 1414277
Showing
15 changed files
with
760 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/__tests__/cst_to_raw_visitor/year_directive.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import test from 'ava'; | ||
|
||
import { parseLedgerToCST } from '../../index'; | ||
import CstToRawVisitor from '../../lib/visitors/cst_to_raw'; | ||
import * as Raw from '../../lib/visitors/raw_types'; | ||
import { assertNoLexingOrParsingErrors } from '../utils'; | ||
|
||
test('returns a year directive object', (t) => { | ||
const cstResults = [ | ||
parseLedgerToCST('Y2024\n'), | ||
parseLedgerToCST('Y 2024\n'), | ||
parseLedgerToCST('year2024\n'), | ||
parseLedgerToCST('year 2024\n'), | ||
parseLedgerToCST('apply year2024\n'), | ||
parseLedgerToCST('apply year 2024\n') | ||
]; | ||
|
||
for (const cstResult of cstResults) { | ||
assertNoLexingOrParsingErrors(t, cstResult); | ||
|
||
const result = CstToRawVisitor.journal(cstResult.cstJournal.children); | ||
|
||
t.is(result.length, 1, 'should modify a year directive'); | ||
t.is(result[0].type, 'yearDirective', 'should be a yearDirective object'); | ||
t.deepEqual( | ||
(result[0] as Raw.YearDirective).value, | ||
{ | ||
year: '2024', | ||
comments: undefined, | ||
contentLines: [] | ||
}, | ||
'should correctly return a year directive object' | ||
); | ||
} | ||
}); | ||
|
||
test('returns a year directive object with a comment', (t) => { | ||
const cstResult = parseLedgerToCST('Y 2024 ; a comment\n'); | ||
|
||
assertNoLexingOrParsingErrors(t, cstResult); | ||
|
||
const result = CstToRawVisitor.journal(cstResult.cstJournal.children); | ||
|
||
t.is(result.length, 1, 'should modify a year directive'); | ||
t.is(result[0].type, 'yearDirective', 'should be a yearDirective object'); | ||
t.truthy( | ||
(result[0] as Raw.YearDirective).value.comments, | ||
'should contain a comment field' | ||
); | ||
t.is( | ||
(result[0] as Raw.YearDirective).value.comments?.value.length, | ||
1, | ||
'should contain a single comment item' | ||
); | ||
}); | ||
|
||
test('returns a year directive a content line', (t) => { | ||
const cstResult = parseLedgerToCST(`Y 2024 | ||
; content line | ||
`); | ||
|
||
assertNoLexingOrParsingErrors(t, cstResult); | ||
|
||
const result = CstToRawVisitor.journal(cstResult.cstJournal.children); | ||
|
||
t.is( | ||
result.length, | ||
1, | ||
'should modify a year directive with a sub-directive comment' | ||
); | ||
t.is(result[0].type, 'yearDirective', 'should be a yearDirective object'); | ||
t.is( | ||
(result[0] as Raw.YearDirective).value.contentLines.length, | ||
1, | ||
'should contain a year directive content line' | ||
); | ||
}); | ||
|
||
test('return a year directive with an inline comment and content line', (t) => { | ||
const cstResult = parseLedgerToCST(`Y 2024 ; inline comment | ||
; content line | ||
`); | ||
|
||
assertNoLexingOrParsingErrors(t, cstResult); | ||
|
||
const result = CstToRawVisitor.journal(cstResult.cstJournal.children); | ||
|
||
t.is( | ||
result.length, | ||
1, | ||
'should modify a year directive with a sub-directive comment' | ||
); | ||
t.is(result[0].type, 'yearDirective', 'should be a yearDirective object'); | ||
t.truthy( | ||
(result[0] as Raw.YearDirective).value.comments, | ||
'should contain a comment field' | ||
); | ||
t.is( | ||
(result[0] as Raw.YearDirective).value.comments?.value.length, | ||
1, | ||
'should contain a single comment item' | ||
); | ||
t.is( | ||
(result[0] as Raw.YearDirective).value.contentLines.length, | ||
1, | ||
'should contain a year directive content line' | ||
); | ||
}); | ||
|
||
test('return a year directive with several content lines', (t) => { | ||
const cstResult = parseLedgerToCST(`Y 2024 ; inline comment | ||
; content line | ||
; another line | ||
; yet another line | ||
`); | ||
|
||
assertNoLexingOrParsingErrors(t, cstResult); | ||
|
||
const result = CstToRawVisitor.journal(cstResult.cstJournal.children); | ||
|
||
t.is( | ||
result.length, | ||
1, | ||
'should modify a year directive with a sub-directive comment' | ||
); | ||
t.is(result[0].type, 'yearDirective', 'should be a yearDirective object'); | ||
t.is( | ||
(result[0] as Raw.YearDirective).value.contentLines.length, | ||
3, | ||
'should contain several year directive content lines' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import test from 'ava'; | ||
|
||
import { macro } from './utils'; | ||
|
||
test('recognizes a year directive', macro, 'Y2024 ', [ | ||
'YearDirective', | ||
'YearDirectiveValue' | ||
]); | ||
|
||
test('recognizes a year directive with an optional space', macro, 'Y 2024 ', [ | ||
'YearDirective', | ||
'YearDirectiveValue' | ||
]); | ||
|
||
test( | ||
'recognizes a year directive with a newline at the end', | ||
macro, | ||
'Y 2024\n', | ||
['YearDirective', 'YearDirectiveValue', 'NEWLINE'] | ||
); | ||
|
||
test( | ||
'recognizes a year directive with a comment at the end', | ||
macro, | ||
'Y 2024 ; a comment\n', | ||
[ | ||
'YearDirective', | ||
'YearDirectiveValue', | ||
'SemicolonComment', | ||
'InlineCommentText', | ||
'NEWLINE' | ||
] | ||
); | ||
|
||
test('recognizes deprecated year directive form', macro, 'year 2024 ', [ | ||
'YearDirective', | ||
'YearDirectiveValue' | ||
]); | ||
|
||
test( | ||
'recognizes alternative deprecated year directive form', | ||
macro, | ||
'apply year 2024 ', | ||
['YearDirective', 'YearDirectiveValue'] | ||
); | ||
|
||
test( | ||
'recognizes deprecated year directive form without optional space', | ||
macro, | ||
'year2024 ', | ||
['YearDirective', 'YearDirectiveValue'] | ||
); | ||
|
||
test( | ||
'recognizes alternative deprecated year directive form without optional space', | ||
macro, | ||
'apply year2024 ', | ||
['YearDirective', 'YearDirectiveValue'] | ||
); | ||
|
||
test('does not recognize an invalid year directive value', macro, 'Y2024a ', [ | ||
'YearDirective' | ||
]); | ||
|
||
test('recognizes a five digit year in year directive', macro, 'Y12024 ', [ | ||
'YearDirective', | ||
'YearDirectiveValue' | ||
]); |
Oops, something went wrong.