Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 18th, 2012  |  syntax: None  |  size: 9.31 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. "use strict"
  2.  
  3. testing= dit.scope('testing')
  4. log= dit.get('log')
  5.  
  6. _assertion_count= 0
  7.  
  8. class ConsoleTestRunner
  9.   log.for @
  10.  
  11.   runForScope:(@scope)->
  12.     @pass=[]
  13.     @fail=[]
  14.     @errs= []
  15.     _assertion_count= 0
  16.     @test_count= 0
  17.     for test_name in @test_names()
  18.       @test_count++
  19.       try
  20.         @scope.get test_name
  21.         @pass.push test_name
  22.       catch ex
  23.         log.start("#{@scope._name} > #{test_name}")
  24.         if ex.name is 'AssertionError'
  25.           log.warn "Expectation failed:", ex.message
  26.           log.error ex.stack
  27.           @fail.push test_name
  28.         else
  29.           log.error ex.message, ex.stack, ex
  30.           @errs.push test_name
  31.         log.end()
  32.     @assertion_count= _assertion_count
  33.     log.start("#{ @scope._name } #{_assertion_count} assertions in #{@test_count} test(s):")
  34.     @summary = "#{@pass.length} passed, #{@fail.length} failed, #{@errs.length} errors."
  35.     if @fail.length or @errs.length
  36.       log.warn @summary
  37.     else
  38.       log @summary
  39.     # log @pass.length, "passed", @pass
  40.     log.warn @fail.length, "failed", @fail if @fail.length
  41.     log.warn @errs.length, "errors", @errs if @errs.length
  42.     log.end()
  43.     @test_count
  44.  
  45.   test_names: ->
  46.     names=[]
  47.     items= @scope._registeredNames()
  48.     for name in items
  49.       names.push name if name.slice(-4) is 'test'
  50.     names
  51.  
  52. testing.service 'ConsoleTestRunner', -> ConsoleTestRunner
  53. testing.factory 'consoleTestRunner', (ConsoleTestRunner)-> ConsoleTestRunner
  54.  
  55. testing.factory 'testRunner', (scope, ConsoleTestRunner, log)->
  56.   test_runner= ->
  57.     test_count= 0
  58.     assert_count= 0
  59.     log.start("test suite")
  60.     for name in scope._definedScopes()
  61.       if name.slice(-5) is 'tests' #name.slice(4) is 'test' or
  62.         ctr= new ConsoleTestRunner
  63.         ctr.runForScope scope.scope(name)
  64.         test_count += ctr.test_count
  65.         assert_count += ctr.assertion_count
  66.     log "Done."
  67.     log.end()
  68.     [test_count, assert_count]
  69.   ->
  70.     [@test_count, @assert_count]= test_runner()
  71.     this
  72.  
  73. # Assertions
  74.  
  75. testing.service 'assert', (new_error)->
  76.   (args...) ->
  77.     switch args.length
  78.       when 1
  79.         expression=args[0]
  80.         msg="expression evaluated to falsy when truthy was expected."
  81.       when 2
  82.         if typeof(args[0]) is 'string'
  83.           [msg, expression]= args
  84.         else
  85.           [expression, msg]= args
  86.      
  87.     _assertion_count++
  88.     unless expression
  89.       throw new_error msg, "AssertionError"
  90.  
  91. testing.service 'assert_equal', (assert)->
  92.   (msg, actual, expected) ->
  93.     msg= "#{msg} Expected #{String(actual)} to equal #{String(expected)}"
  94.     assert (expected == actual), msg
  95.  
  96. testing.service 'deny', (assert)->
  97.   (expression, msg="expression evaluated to truthy when falsy was expected.") ->
  98.     assert !expression, msg
  99.  
  100. testing.service 'deny_equal', (deny)->
  101.   (expr_a, expr_b, msg=false) ->
  102.     msg= "Expected #{String(expr_a)} to equal #{String(expr_b)}"
  103.     deny (expr_a == expr_b), msg
  104.  
  105. testing.service 'did_throw', (assert, is_function)->
  106.   (fn, type)->
  107.     thrown_ex= false
  108.     try
  109.       fn()
  110.     catch ex
  111.       thrown_ex= ex
  112.     if type?
  113.       if is_function(type)
  114.         thrown_ex instanceof type
  115.       else
  116.         thrown_ex?.name == type
  117.     else
  118.       thrown_ex
  119.  
  120.  
  121. dit.scope('root').include('testing')
  122.  
  123.  
  124. # Some self tests for dit
  125.  
  126. dit_tests= dit.scope('dit_tests')
  127.  
  128. dit_tests.define 'is_array test', (is_array, assert)->
  129.   assert "arrays return true", is_array([])
  130.   assert "objects return false", !is_array({})
  131.   assert "nulls return false", !is_array(null)
  132.   assert "numerics return false", !is_array(1)
  133.   assert "string return false", !is_array("string")
  134.   assert "regexp return false", !is_array(/test/)
  135.   assert "functions return false", !is_array(/test/)
  136.  
  137. dit_tests.define 'is_function test', (is_function, assert)->
  138.   assert "functions return true", is_function(->)
  139.   assert "arrays return false", !is_function([])
  140.   assert "objects return false", !is_function({})
  141.   assert "nulls return false", !is_function(null)
  142.   assert "numerics return false", !is_function(1)
  143.   assert "string return false", !is_function("string")
  144.   assert "regexp return false", !is_function(/test/)
  145.  
  146. dit_tests.define 'is_string test', (is_string, assert)->
  147.   assert "functions return false", !is_string(->)
  148.   assert "arrays return false", !is_string([])
  149.   assert "objects return false", !is_string({})
  150.   assert "nulls return false", !is_string(null)
  151.   assert "numerics return false", !is_string(1)
  152.   assert "string return true", is_string("string")
  153.   assert "regexp return false", !is_string(/test/)
  154.  
  155. dit_tests.define 'is_object test', (is_object, assert)->
  156.   assert "functions return false", !is_object(->)
  157.   assert "arrays return false", !is_object([])
  158.   assert "objects return true", is_object({})
  159.   assert "nulls return false", !is_object(null)
  160.   assert "numerics return false", !is_object(1)
  161.   assert "string return true", !is_object("string")
  162.   assert "regexp return false", !is_object(/test/)
  163.  
  164. dit_tests.define 'flatten test', (flatten, assert)->
  165.   assert "length of [1,2,3] is 3", flatten([1,2,3]).length is 3
  166.   assert "length of [1,[2,3]] is 3", flatten([1,[2,3]]).length is 3
  167.   assert "length of [1,[2,[3]] is 3", flatten([1,[2,[3]]]).length is 3
  168.  
  169. dit_tests.define 'scope test', (scope, assert, assert_equal, is_object)->
  170.   tmp= scope.scope('temp')
  171.   define_count=0
  172.   # Define injected once per scope
  173.   tmp.define('name', ->
  174.     define_count += 1
  175.     "Matt"
  176.   )
  177.   # Instantiated once for every injection
  178.   factory_count=0
  179.   class MyClass
  180.     constructor: ->
  181.       factory_count += 1
  182.  
  183.   tmp.factory('myClass', -> MyClass)
  184.   # Only inject once, for lifetime of page
  185.   service_count=0
  186.   tmp.service('url', ->
  187.     service_count +=1
  188.     "http://test.com"
  189.   )
  190.  
  191.   assert_equal "#scope.get('name') returns 'Matt'", tmp.get('name'), 'Matt'
  192.   assert_equal "#scope.get('name') returns 'Matt' a second time", tmp.get('name'), "Matt"
  193.   assert_equal "definition of name was only called once", define_count, 1
  194.  
  195.   assert_equal "#scope.get('url') returns 'http://test.com'", tmp.get('url'), "http://test.com"
  196.   assert_equal "#scope.get('url') returns 'http://test.com' a second time", tmp.get('url'), "http://test.com"
  197.   assert_equal "definition of name was only called once", service_count, 1
  198.  
  199.   assert "#scope.get('myClass') is an object", is_object(tmp.get('myClass'))
  200.   assert_equal "definition of name was only called once", factory_count, 1
  201.   assert "#scope.get('myClass') is an object a second time", is_object(tmp.get('myClass'))
  202.   assert_equal "definition of name was called twice", factory_count, 2
  203.  
  204.   sub= scope.scope('temp_sub', ['temp'])
  205.   assert_equal "#sub_scope.get('name') returns 'Matt'", sub.get('name'), "Matt"
  206.   assert_equal "#sub_scope.get('name') returns 'Matt' a second time", sub.get('name'), "Matt"
  207.   assert_equal "definition of name was only called twice", define_count, 2
  208.   sub.define('name', -> "Dan")
  209.   assert_equal "#sub_scope.get('name') returns 'Matt'", sub.get('name'), "Dan"
  210.  
  211.  
  212.   assert_equal "#sub_scope.get('url') returns 'http://test.com'", sub.get('url'), "http://test.com"
  213.   assert_equal "#sub_scope.get('url') returns 'http://test.com' a second time", sub.get('url'), "http://test.com"
  214.   assert_equal "definition of url service was only called once", service_count, 1
  215.  
  216.   scope.scope('temp_sub')._detachScope()
  217.   scope.scope('temp')._detachScope()
  218.  
  219.   assert "temp scope removed from scope list", scope._definedScopes().indexOf('temp') == -1
  220.  
  221.  
  222. dit_tests.define 'injection test', (scope, assert, assert_equal, did_throw, is_object, is_string, is_function, log)->
  223.   tmp= scope.scope('temp')
  224.  
  225.   # should inject a string
  226.   tmp.service 'name', -> "Matt"
  227.   # should inject a function
  228.   tmp.service 'url', ->
  229.     -> "http://test.com"
  230.   # should inject an object
  231.   tmp.service 'user', ->
  232.     username:"inkwellian"
  233.   # should inject an instance
  234.   class Connection
  235.     @count=0
  236.     constructor: ->
  237.       @count= Connection.count = Connection.count + 1
  238.       @type="connection"
  239.   tmp.factory 'conn', ->
  240.     Connection
  241.  
  242.   tmp.inject( (name)->
  243.     assert "result of #scope.injection is string", is_string(name)
  244.     assert_equal "result of #scope.injection is 'Matt'", name, "Matt"
  245.   )
  246.   tmp.inject( (url)->
  247.     assert "result of #scope.injection is function", is_function(url)
  248.     assert_equal "result of #scope.injection is 'http://test.com'", url(), "http://test.com"
  249.   )
  250.   tmp.inject( (user)->
  251.     assert "result of #scope.injection is object", is_object(user)
  252.     assert_equal "result.username of #scope.injection is 'inkwellian'", user.username, "inkwellian"
  253.   )
  254.   tmp.inject( (conn)->
  255.     assert "result of #scope.injection is object", is_object(conn)
  256.     assert "result of #scope.injection is instance of Connection", (conn instanceof Connection)
  257.     assert_equal "result.type of #scope.injection is 'connection'", conn.type, "connection"
  258.     assert_equal "result.count of #scope.injection is 1", conn.count, 1
  259.   )
  260.   tmp.inject( (conn)->
  261.     assert "result of #scope.injection is object", is_object(conn)
  262.     assert "result of #scope.injection is instance of Connection", (conn instanceof Connection)
  263.     assert_equal "result.type of #scope.injection is 'connection'", conn.type, "connection"
  264.     assert_equal "result.count of #scope.injection is 2", conn.count, 2
  265.   )
  266.  
  267.   inline_injection= -> tmp.inject( (missing)-> )
  268.   assert "missing injectionables should throw an instance of Error", did_throw(inline_injection, Error)
  269.   assert "missing injectionables should throw an InjectionError", did_throw(inline_injection, 'InjectionError')
  270.    
  271.   tmp._detachScope()