1. adamschmideg
  2. depresso

Source

depresso / test / resolve.coffee

{testing,resolve} = require '../depresso'
{data} = require './sample/data'
spahql = require 'spahql'
_ = require 'underscore'

@resolve = (assert) ->
  deps = [
    target: '/products//price'
    depends: 
      netPrice: '../netPrice'
      tax: '../tax'
    calculate: ->
      @netPrice * (1 + @tax)
  ,
    target: '/general/price'
    depends:
      prices: '/products//price'
    context:
      discount: 1
    calculate: ->
      sum = _.reduce(
        @prices
        (x,y) -> x+y
        0)
      sum - @discount
  ]

  result = resolve data, deps, ['/general/price', '/products/*/price']
  expected =
    general:
      discount: 0.1
      price: 33
    products: [
      name: 'Bag'
      netPrice: 20
      tax: 0.1
      price: 22
    ,
      name: 'Beer'
      netPrice: 10
      tax: 0.2
      price: 12
    ] 
  assert.deepEqual expected, result
  assert.done()

@fibonacci = (assert) ->
  data = {}
  deps = [
    target: '/nums/0/index'
    calculate: -> 1
  ,
    target: '/nums/1/index'
    calculate: -> 1
  ,
    target: '/nums/*/index'
    depends:
      prev: '../</index'
      prev2: '../</</index'
    calculate: ->
      @prev + @prev2 
  ]
  result = resolve data, deps, ['/nums/6/index']
  assert.deepEqual [1,1,2,3,5,8,13], _(result.nums).map (x) -> x.index
  assert.done()