Skip to content

Instantly share code, notes, and snippets.

@Inviz
Created July 16, 2015 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inviz/ed68b86c2389df674b73 to your computer and use it in GitHub Desktop.
Save Inviz/ed68b86c2389df674b73 to your computer and use it in GitHub Desktop.
expectations = ($, result, block, page) ->
return unless result
for property, value of result
if property == 'textual'
if block.text
$.expect(block).to.have.a('text')
else
$.expect(block).to.have.a('description')
else if property == 'type'
if value == 'text'
value = 'textual'
$.expect(block).to.be.subtypeOf value
else if property.indexOf('.') > -1# || property == 'subtitle'
if value
$.expect(block).to.have.a(property).defined
else
$.expect(block).to.not.have.a(property).defined
else
if property != 'condition'
if value
expectation = $.expect(block).with.a(property)
if property is "cover"
expectation.with.src
expectation.with.width
expectation.with.height
else
unless page.relaxed
expectation = $.expect(block).not.with.a(property)
else
expectation = undefined
if typeof value == 'function'
value.call(expectation, $, block)
module.exports = (rules, page, recursive, result) ->
consume = rules.consume
consume ?= rules.extends?.consume
return consume if typeof consume is "function"
result = rules.consumers || (rules.extends?.consumers) || (rules.extends?.extends?.consumers) || {}
return ($) ->
if page.ordered
type = 'block'
else
type = 'item'
$.inlet type, (item, attempt) ->
if values = (rules.consume or rules.extends?.consume)?[type]?($, item)
return values
if result.required
blocks = [item.id]
else
blocks = []
if result.blocks
for config in result.blocks
$.inlet 'block', item, attempt, (block) ->
expectations($, config, block, page)
blocks.push(block.id)
return block
expectations($, result.required, item.block || item, page)
return blocks.join(':')
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment