Skip to content

Commit

Permalink
Fix conditional tag parsing; Add unit tests for conditional tag parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zlant committed Mar 17, 2022
1 parent 82dc820 commit 6b381a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/test/conditional-tag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { parseConditionalTag } from '../utils/conditional-tag'

describe('#parseConditionalTag()', () => {
test('one value', async() => {
const values = parseConditionalTag('free @ (Mo-Fr 09:00-19:00)')
expect(values).toStrictEqual([
{ value: 'free', condition: 'Mo-Fr 09:00-19:00' },
])
})
test('two value; without brackets', async() => {
const values = parseConditionalTag('free @ (Mo-Fr 09:00-19:00); ticket @ 19:00-20:00')
expect(values).toStrictEqual([
{ value: 'free', condition: 'Mo-Fr 09:00-19:00' },
{ value: 'ticket', condition: '19:00-20:00' },
])
})
test('two values; value without condition', async() => {
const values = parseConditionalTag('free @ (Mo-Fr 09:00-19:00); ticket')
expect(values).toStrictEqual([
{ value: 'free', condition: 'Mo-Fr 09:00-19:00' },
{ value: 'ticket', condition: null },
])
})
test('free values; without brackets; condition with semicolons; ends with semicolon', async() => {
const values = parseConditionalTag('free @ (Mo-Fr 09:00-19:00);ticket@19:00-20:00 ; residents @ (Mo-Fr 07:00-09:00,15:00-18:00; Sa 09:00-15:00);')
expect(values).toStrictEqual([
{ value: 'free', condition: 'Mo-Fr 09:00-19:00' },
{ value: 'ticket', condition: '19:00-20:00' },
{ value: 'residents', condition: 'Mo-Fr 07:00-09:00,15:00-18:00; Sa 09:00-15:00' },
])
})
})
6 changes: 3 additions & 3 deletions src/test/parking-area.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { getConditions } from '../parking/parking-area'
import { OsmTags } from '../utils/types/osm-data'

describe('#getConditions()', () => {
test('should return no intervals and default as free when no tags', async() => {
test('should return no conditional values and default as free when no tags', async() => {
const tags: OsmTags = {}
const conditions = getConditions(tags)
expect(conditions).toStrictEqual({ default: 'free', intervals: [] })
expect(conditions).toStrictEqual({ default: 'free', conditionalValues: [] })
})
test('should return ticket when fee=yes', async() => {
const tags: OsmTags = { fee: 'yes' }
const conditions = getConditions(tags)
expect(conditions).toStrictEqual({ default: 'ticket', intervals: [] })
expect(conditions).toStrictEqual({ default: 'ticket', conditionalValues: [] })
})
})
9 changes: 5 additions & 4 deletions src/utils/conditional-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function parseConditionalTag(tag: string) {
case ';': {
if (bracketStack.length === 0) {
const startPosition = prevConditionEndPosition ? prevConditionEndPosition + 1 : 0
parsedConditionalTag.push(parseConditionalValue(tag.substring(startPosition, i - 1)))
parsedConditionalTag.push(parseConditionalValue(tag.substring(startPosition, i)))
prevConditionEndPosition = i
}
}
Expand All @@ -42,9 +42,10 @@ function parseConditionalValue(rawConditionalValue: string) {
}

if (tokens.length > 1) {
conditionalValue.condition = tokens[1]
.trim()
.substring(1, tokens[1].length - 1)
const condition = tokens[1].trim()
conditionalValue.condition = condition.substring(
condition.startsWith('(') ? 1 : 0,
condition.endsWith(')') ? condition.length - 1 : condition.length)
}

return conditionalValue
Expand Down

0 comments on commit 6b381a8

Please sign in to comment.