All files / src/elm conditional.coffee

100% Statements 21/21
0% Branches 0/2
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4513x 1x 1x     1x   2x 2x 2x 2x             8x 8x   1x     4x 4x 8x   4x           3x 6x   2x       6x   2x    
{ Expression } = require './expression'
{ build } = require './builder'
{ equals } = require '../util/comparison'
 
# TODO: Spec lists "Conditional", but it's "If" in the XSD
module.exports.If = class If extends Expression
  constructor: (json) ->
    super
    @condition = build json.condition
    @th = build json.then
    @els = build json.else
 
  exec: (ctx) ->
    if @condition.execute(ctx) then @th.execute(ctx) else @els.execute(ctx)
 
module.exports.CaseItem = CaseItem = class CaseItem
  constructor:(json) ->
    @when = build json.when
    @then = build json.then
 
module.exports.Case = class Case extends Expression
 
  constructor: (json) ->
    super
    @comparand = build json.comparand
    @caseItems = for ci in json.caseItem
                   new CaseItem(ci)
    @els = build json.else
 
  exec: (ctx) ->
    if @comparand then @exec_selected(ctx) else @exec_standard(ctx)
 
  exec_selected: (ctx) ->
    val = @comparand.execute(ctx)
    for ci in @caseItems
      if equals ci.when.execute(ctx), val
       return ci.then.execute(ctx)
    @els.execute(ctx)
 
  exec_standard: (ctx) ->
    for ci in @caseItems
      if ci.when.execute(ctx)
       return ci.then.execute(ctx)
    @els.execute(ctx)