Skip to content

Commit

Permalink
split tests out into individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
collin committed Feb 17, 2012
1 parent dd7a3bd commit efb354c
Show file tree
Hide file tree
Showing 43 changed files with 2,126 additions and 2,074 deletions.
15 changes: 12 additions & 3 deletions autotest.watchr
@@ -1,17 +1,26 @@
def run_all_tests
print `clear`
puts "Tests run #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
puts `nodeunit test/`
test_files = `find test |grep coffee | tr "\\n" " "`
cleaned = test_files.split(" ").reject{|path| path["helper"]}.join(" ")
puts "nodeunit #{cleaned}"
puts `nodeunit #{cleaned}`
end

def run_tests(m)
return if m.to_s["helper"]
print `clear`
puts "Tests run @ #{m} an #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
puts `nodeunit #{m}`
end

run_all_tests
watch("(lib)(/.*)+.coffee") { |m| run_all_tests }
watch("(lib)(/.*)+.coffee") { |m|
hit = m.to_s
console.log hit
hit.gsub!(/^lib\/alpha_simprini/, "test")
run_tests(hit)
}
watch("(test)(/.*)+.coffee") { |m| run_tests(m) }

@interrupted = false
Expand All @@ -28,4 +37,4 @@ Signal.trap "INT" do
run_all_tests
@interrupted = false
end
end
end
2 changes: 1 addition & 1 deletion lib/alpha_simprini/client/application.coffee
@@ -1,7 +1,7 @@
AS = require("alpha_simprini")
_ = require "underscore"
jwerty = require("jwerty").jwerty
domready = $ = require("jQuery")
domready = $ = require("jquery")

class AS.Application
AS.Event.extends(this)
Expand Down
45 changes: 38 additions & 7 deletions lib/alpha_simprini/client/binding.coffee
Expand Up @@ -45,7 +45,7 @@ class AS.Binding.Model
do (property, options) =>
if _.isArray(options)
@styles[property] = => @model.read_path(options)
painter = ->
painter = -> _.defer =>
value = @styles[property]()
@content.css property, value

Expand All @@ -54,7 +54,7 @@ class AS.Binding.Model
@context.binds @model, binding_path, painter, this
else
@styles[property] = => options.fn(@model)
painter = -> @content.css property, @styles[property]()
painter = -> _.defer => @content.css property, @styles[property]()
for field in options.field.split(" ")
@context.binds @model, "change:#{field}", painter, this

Expand All @@ -71,7 +71,8 @@ class AS.Binding.Model
else
value

painter = -> @content.attr property, @attrs[property]()
painter = -> _.defer =>
@content.attr property, @attrs[property]()

binding_path = AS.deep_clone(options)
binding_path[options.length - 1] = "change:#{_(options).last()}"
Expand All @@ -83,7 +84,8 @@ class AS.Binding.Model
else
if @model[options.field]() then "yes" else "no"

painter = -> @content.attr property, @attrs[property]()
painter = -> _.defer =>
@content.attr property, @attrs[property]()

for field in options.field.split(" ")
@context.binds @model, "change:#{field}", painter, this
Expand Down Expand Up @@ -160,7 +162,7 @@ class AS.Binding.Select extends AS.Binding.Input

set_content: () =>
path_value = @path_value()
path_value = @path_value.id if path_value?.id
path_value = path_value.id if path_value?.id
@content.val path_value

set_field: =>
Expand Down Expand Up @@ -288,13 +290,23 @@ class AS.Binding.HasMany extends AS.Binding

@contents = {}
@bindings = {}
@sorting = @sorted_models()

@collection.each @make_content
@initial_content()

@context.binds @collection, "add", @insert_item, this
@context.binds @collection, "remove", @remove_item, this
@context.binds @collection, "change", @change_item, this

initial_content: ->
@sorted_models().each @make_content

sorted_models: ->
if sort_field = @options.order_by
@collection.models.sortBy((item) -> item[sort_field]())
else
@collection.models

skip_item: (item) ->
return false unless @options.filter

Expand All @@ -309,14 +321,16 @@ class AS.Binding.HasMany extends AS.Binding
insert_item: (item) =>
return if @skip_item(item)
content = @context.dangling_content => @make_content(item)
index = @collection.indexOf(item).value?()
index = @sorted_models().indexOf(item).value?()
index ?= 0
siblings = @container.children()
if siblings.get(0) is undefined or siblings.get(index) is undefined
@container.append(content)
else
@context.$(siblings.get(index)).before(content)

@sorting = @sorted_models()

remove_item: (item) =>
if @contents[item.cid]
@contents[item.cid].remove()
Expand All @@ -325,7 +339,24 @@ class AS.Binding.HasMany extends AS.Binding
@bindings[item.cid].unbind()
delete @bindings[item.cid]

@sorting = @sorted_models()

move_item: (item) ->
content = @contents[item.cid]
current_index = content.index()
new_index = @sorted_models().indexOf(item).value()
siblings = content.parent().children()

if current_index < new_index
@context.$(siblings[new_index]).after(content)
else if new_index < current_index
@context.$(siblings[new_index]).before(content)

change_item: (item) =>
if @options.order_by and @sorting.indexOf(item).value() isnt @sorted_models().indexOf(item).value()
@move_item(item)
@sorting = @sorted_models()

if @skip_item(item)
@remove_item(item)
else if @contents[item.cid] is undefined
Expand Down
4 changes: 2 additions & 2 deletions lib/alpha_simprini/client/dom.coffee
@@ -1,6 +1,6 @@
AS = require("alpha_simprini")
_ = require "underscore"
jQuery = require "jQuery"
$ = require "jquery"

SVG =
ns: "http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -32,7 +32,7 @@ class AS.DOM
constructor: (args) ->
# body...

$: jQuery
$: $

text: (text_content) ->
# createTextNode creates a text node, no DOM injection here
Expand Down
44 changes: 22 additions & 22 deletions lib/alpha_simprini/client/view.coffee
Expand Up @@ -5,11 +5,11 @@ fleck = require("fleck")
class AS.View extends AS.DOM
AS.Event.extends(this)
AS.StateMachine.extends(this)

tag_name: "div"

_ensure_element: -> @el ?= @$(@build_element())

constructor: (config={}) ->
@cid = _.uniqueId("c")

Expand All @@ -23,11 +23,11 @@ class AS.View extends AS.DOM
@_ensure_element()
@delegateEvents()
@initialize()

initialize: ->

append: (view) -> @el.append view.el

process_attr: (node, key, value) ->
if value instanceof Function
# switch value
Expand All @@ -37,19 +37,19 @@ class AS.View extends AS.DOM
# false
else
node.setAttribute(key, value)

group_bindings: (fn) ->
@within_binding_group @binding_group.add_child(), fn

within_binding_group: (binding_group, fn) ->
current_group = @binding_group
@binding_group = binding_group
content = fn.call(this, binding_group)
@binding_group = current_group
content

binds: -> @binding_group.binds.apply(@binding_group, arguments)

klass_string: (parts=[]) ->
if @constructor is AS.View
# parts.push "ASView"
Expand All @@ -61,28 +61,28 @@ class AS.View extends AS.DOM
base_attributes: ->
attrs =
class: @klass_string()

build_element: ->
@current_node = @[@tag_name](@base_attributes())

delegateEvents: () ->
if @events
@standard_events = new AS.ViewEvents(this, @events)
@standard_events.apply_bindings()
state_events = _(@constructor::).chain().keys().filter (key) ->

state_events = _(@constructor::).chain().keys().filter (key) ->
_(key).endsWith("_events")
@state_events = {}
for key in state_events.value()
state = key.replace(/_events$/, '')
do (key, state) =>
@state_events[state] = new AS.ViewEvents(this, @[key])

@["exit_#{state}"] = ->
@trigger("exitstate:#{state}")
@state_events[state].revoke_bindings()
@["enter_#{state}"] = ->

@["enter_#{state}"] = ->
@trigger("enterstate:#{state}")
@state_events[state].apply_bindings()

Expand All @@ -91,7 +91,7 @@ class AS.View extends AS.DOM
fleck.singularize(thing)
else
fleck.pluralize(thing)

reset_cycle: (args...) ->
delete @_cycles[args.join()] if @_cycles

Expand All @@ -104,22 +104,22 @@ class AS.View extends AS.DOM
toggle: ->
@button class:"toggle expand"
@button class:"toggle collapse"

field: (_label, options = {}, fn = ->) ->
if _.isFunction options
fn = options
options = {}

@div ->
@label _label
@input(options)
fn?.call(this)

choice: (_label, options = {}, fn = ->) ->
if _.isFunction options
fn = options
options = {}
options.type = "checkbox"

@field _label, options, fn

30 changes: 15 additions & 15 deletions lib/alpha_simprini/client/view_events.coffee
Expand Up @@ -7,25 +7,25 @@ class AS.ViewEvents
PARSE_GUARD = (guard="{}") ->
guard = guard.replace(/(\w+):/g, (__, match) -> "\"#{match}\":")
guard = JSON.parse(guard)

constructor: (@view, events) ->
@namespace = _.uniqueId ".ve"
@events = @unify_options(events)
@validate_options()
@cache_handlers()

unify_options: (events) ->
for key, options of events
if _.isString options
options = events[key] = method_name: options

[__, event_name, guard, selector] = key.match EVENT_SPLITTER

options.event_name = event_name + @namespace
options.guard = PARSE_GUARD(guard)
options.selector = selector
options.method = @view[options.method_name]


return events

Expand All @@ -37,38 +37,38 @@ class AS.ViewEvents
Specified both method and transition for event #{key}.
Use before/after hooks for transitions instead.
"""

if !options.method and !options.transition
throw new Error """
Event Binding Error in #{@view.constructor.name}!
Specified neither method or transition for event #{key}.
Specify what to do when handling this error.
Do you need to define the method: `#{options.method_name}'?
"""

if options.method and !_.isFunction(options.method)
console.error options.method, "was given instead of a function."
throw new Error """
Event Binding Error in #{@view.constructor.name}!
Specified method for event #{key} that is not a function.
Specify only a function as a method for an event handler.
"""

cache_handlers: ->
for key, options of @events
do (key, options) =>
options.handler = (event) =>
for key, value of options.guard
for key, value of options.guard
return unless event[key] is value

if options.method
options.method.apply(@view, arguments)
else if options.transition
@view.transition_state options.transition

revoke_bindings: ->
@revoke_binding(options) for key, options of @events

revoke_binding: (options) ->
[selector, event_name] = [options.selector, options.event_name]
if selector is ''
Expand All @@ -81,10 +81,10 @@ class AS.ViewEvents
target = @view.$(selector, @view.el[0])
target.die @namespace
target.click() # bug with drag/drop allows for one last drag after revoking bindings :(

apply_bindings: ->
@apply_binding(options) for key, options of @events

apply_binding: (options) ->
[selector, event_name, handler] = [options.selector, options.event_name, options.handler]
if selector is ''
Expand All @@ -100,4 +100,4 @@ class AS.ViewEvents
else
emitter.bind event_name, handler, @view
else
@view.$(selector, @view.el[0]).live event_name, handler
@view.el.delegate selector, event_name, handler

0 comments on commit efb354c

Please sign in to comment.