Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Sep 11, 2024
1 parent 3bb4bde commit 807fbfe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
5 changes: 3 additions & 2 deletions plugins/dates/scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ txt = 'only in 2018 and 2020'
txt = '2024/02/05 and 2024/03/09'
txt = 'in 03/28'
txt = 'in 28/28'
txt = '24/24'
txt = '1 to 5 weeks ago'
txt = 'in 1 to 5 weeks'

// nlp.verbose('tagger')
let doc = nlp(txt).debug()
Expand All @@ -55,7 +56,7 @@ let doc = nlp(txt).debug()

// console.log(doc.times(context).json())
let found = doc.dates(context).json()
// console.log(found[0].dates)
console.log(found[0].dates)
found.forEach((o) => {
console.log('start: ', fmt(o.dates.start))
console.log(' end: ', fmt(o.dates.end))
Expand Down
82 changes: 38 additions & 44 deletions plugins/dates/tests/duration-range.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
import test from 'tape'
import nlp from './_lib.js'

// Duration ranges
const durArr = [
{
text: ['2 to 4 days', '2-4 days', 'two to four days'],
duration: { years: 0, months: 0, days: 3, hours: 0, minutes: 0 },
},
{
text: ['1 to 2 weeks', '1-2 weeks', 'one to two weeks'],
duration: { years: 0, months: 0, days: 14, hours: 0, minutes: 0 },
},
{
text: ['1 to 5 months', '1-5 months', 'one to five months'],
duration: { years: 0, months: 5, days: 0, hours: 0, minutes: 0 },
},
{
text: ['2 to 4 years', '2-4 years', 'two to four years'],
duration: { years: 3, months: 0, days: 0, hours: 0, minutes: 0 },
},
]

const context = {
today: '2024-01-01',
today: '2024-09-24',
}

test('future duration-ranges', function (t) {
durArr.forEach(obj => {
obj.text.forEach(str => {
let text = `in ${str}`
const doc = nlp(text)
const { duration, start, end } = doc.dates(context).get()[0]
t.deepEqual(duration, obj.duration, text)
t.ok(start > context.today, 'start date')
t.ok(end > start, 'end date')
})
})
t.end()
})
let arr = [
{ str: 'in 2 to 4 days', start: '2024-09-26', end: '2024-09-28' },
{ str: 'in 2-4 days', start: '2024-09-26', end: '2024-09-28' },
{ str: 'in two to four days', start: '2024-09-26', end: '2024-09-28' },
{ str: '2 to 4 days ago', start: '2024-09-20', end: '2024-09-22' },
{ str: '2-4 days ago', start: '2024-09-20', end: '2024-09-22' },
{ str: 'two to four days ago', start: '2024-09-20', end: '2024-09-22' },

{ str: 'in 1 to 2 weeks', start: '2024-10-01', end: '2024-10-07' },
{ str: 'in 1-2 weeks', start: '2024-10-01', end: '2024-10-07' },
{ str: 'in one to two weeks', start: '2024-10-01', end: '2024-10-07' },
{ str: '1 to 2 weeks ago', start: '2024-09-10', end: '2024-09-17' },
{ str: '1-2 weeks ago', start: '2024-09-10', end: '2024-09-17' },
{ str: 'one to two weeks ago', start: '2024-09-10', end: '2024-09-17' },

test('past duration-ranges', function (t) {
durArr.forEach(obj => {
obj.text.forEach(str => {
let text = `${str} ago`
const doc = nlp(text)
const { duration, start, end } = doc.dates(context).get()[0]
t.deepEqual(duration, obj.duration, text)
t.ok(start < context.today, 'start date')
t.ok(end > start, 'end date')
})
{ str: 'in 1 to 5 months', start: '2024-10-24', end: '2025-02-24' },
{ str: 'in 1-5 months', start: '2024-10-24', end: '2025-02-24' },
{ str: 'in one to five months', start: '2024-10-24', end: '2025-02-24' },
{ str: '1 to 5 months ago', start: '2024-04-24', end: '2024-08-24' },
{ str: '1-5 months ago', start: '2024-04-24', end: '2024-08-24' },
{ str: 'one to five months ago', start: '2024-04-24', end: '2024-08-24' },

{ str: 'in 2 to 4 years', start: '2026-09-24', end: '2028-09-24' },
{ str: 'in 2-4 years', start: '2026-09-24', end: '2028-09-24' },
{ str: 'in two to four years', start: '2026-09-24', end: '2028-09-24' },
{ str: '2 to 4 years ago', start: '2020-09-24', end: '2022-09-24' },
{ str: '2-4 years ago', start: '2020-09-24', end: '2022-09-24' },
{ str: 'two to four years ago', start: '2020-09-24', end: '2022-09-24' },
]


test('duration-ranges', function (t) {
arr.forEach(obj => {
const doc = nlp(obj.str)
const { start, end } = doc.dates(context).get()[0]
t.equal(start.replace(/T.*/, ''), obj.start, obj.str + ':start')
t.equal(end.replace(/T.*/, ''), obj.end, obj.str + ':end')
})
t.end()
})

0 comments on commit 807fbfe

Please sign in to comment.