Skip to content

Commit

Permalink
Merge pull request getodk#5 from clint-tseng/cxlt/fix-empty-constraints
Browse files Browse the repository at this point in the history
fix two cases where "()" constraint/relevant would be produced
  • Loading branch information
issa-tseng authored Mar 6, 2019
2 parents 1f198e6 + f71c08b commit cbb7fb5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
30 changes: 20 additions & 10 deletions lib/convert.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions spec/src/convert-question-spec.ls
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ describe \constraint ->
result = { type: \inputNumber, range: { min: 3, max: 9, maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. > 3) and (. <= 9)')

test 'build number/date range constraint generation (min-only)' ->
result = { type: \inputNumber, range: { min: 3, minInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. >= 3)')

test 'build number/date range constraint generation (max)' ->
result = { type: \inputNumber, range: { max: 3, maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. <= 3)')

test 'build number/date range false pruning' ->
result = { type: \inputText, range: false } |> convert-simple
expect(result.constraint).toBe(undefined)
Expand Down Expand Up @@ -254,6 +262,12 @@ describe 'followup question' ->
expect(result.relevant).toBe("(selected(testquestion, 'testvalue'))")
expect(context.successor-relevance).toBe(undefined)

test 'blank values are ignored' ->
context = new-context() with successor-relevance: ""
result = convert-question({ type: \inputText }, context)
expect(result.relevant).toBe(undefined)
expect(context.successor-relevance).toBe(undefined)

test 'context is cleared at the end of group scope' ->
context = new-context()
convert-question({ type: \group, children: [{ type: \inputSelectOne, name: \test, other: [ \test ] }] }, context)
Expand Down
11 changes: 8 additions & 3 deletions src/convert.ls
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ new-context = -> { seen-fields: {}, choices: {}, warnings: [] }

# creates a range expression.
gen-range = (range, self = \.) ->
[ "#self >#{if range.minInclusive is true then \= else ''} #{expr-value(range.min)}",
"#self <#{if range.maxInclusive is true then \= else ''} #{expr-value(range.max)}" ]
result = []
unless is-nonsense(range.min)
result.push("#self >#{if range.minInclusive is true then \= else ''} #{expr-value(range.min)}")
unless is-nonsense(range.max)
result.push("#self <#{if range.maxInclusive is true then \= else ''} #{expr-value(range.max)}")
result

# returns an intermediate-formatted question clone (purely functional), but mutates context.
convert-question = (question, context, prefix = []) ->
Expand Down Expand Up @@ -118,7 +122,8 @@ convert-question = (question, context, prefix = []) ->

## merge in convenience relevant logic definitions:
if (successor-relevance = delete context.successor-relevance)?
question.relevant = (question.relevant ? []) ++ [ successor-relevance ] |> map(-> "(#it)") |> (.join(' and '))
unless is-nonsense(successor-relevance)
question.relevant = (question.relevant ? []) ++ [ successor-relevance ] |> map(-> "(#it)") |> (.join(' and '))

## drop successor information into context:
if (other = delete question.other)?
Expand Down

0 comments on commit cbb7fb5

Please sign in to comment.