newjs 1.5.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/History.txt +11 -0
  2. data/Manifest.txt +162 -14
  3. data/{README.txt → README.rdoc} +1 -1
  4. data/Rakefile +25 -3
  5. data/app_generators/newjs_iphone/templates/Html/test/assets/jsunittest.js +41 -13
  6. data/app_generators/newjs_iphone/templates/Html/test/assets/unittest.css +4 -0
  7. data/features/development.feature +13 -0
  8. data/features/imported_files_for_generators.feature +19 -0
  9. data/features/steps/common.rb +205 -0
  10. data/features/steps/env.rb +6 -0
  11. data/javascript_test_generators/functional_test/functional_test_generator.rb +9 -1
  12. data/javascript_test_generators/functional_test/templates/test/assets/jshoulda.js +186 -0
  13. data/javascript_test_generators/functional_test/templates/test/test.html.erb +15 -1
  14. data/javascript_test_generators/unit_test/templates/test/assets/jshoulda.js +186 -0
  15. data/javascript_test_generators/unit_test/templates/test/test.html.erb +18 -20
  16. data/javascript_test_generators/unit_test/templates/test/test_jshoulda.html.erb +45 -0
  17. data/javascript_test_generators/unit_test/unit_test_generator.rb +11 -3
  18. data/lib/newjs.rb +3 -2
  19. data/newjs_iphone_generators/page/USAGE +5 -0
  20. data/newjs_iphone_generators/page/page_generator.rb +36 -0
  21. data/newjs_iphone_generators/page/templates/src/name.css.erb +0 -0
  22. data/newjs_iphone_generators/page/templates/src/name.html.erb +13 -0
  23. data/newjs_iphone_generators/page/templates/src/name.js.erb +4 -0
  24. data/newjs_iphone_generators/page/templates/test/fixtures/name.js.erb +5 -0
  25. data/newjs_iphone_generators/page/templates/test/unit/name_test.html.erb +61 -0
  26. data/rack_generators/javascript_test/USAGE +5 -0
  27. data/rack_generators/javascript_test/javascript_test_generator.rb +88 -0
  28. data/rack_generators/javascript_test/templates/assets/jshoulda.js +186 -0
  29. data/rack_generators/javascript_test/templates/assets/jsunittest.js +1004 -0
  30. data/rack_generators/javascript_test/templates/assets/unittest.css +54 -0
  31. data/rack_generators/javascript_test/templates/ext/jquery.js +3408 -0
  32. data/{app_generators/newjs_iphone/templates/Html/test/assets → rack_generators/javascript_test/templates/ext}/prototype.js +56 -122
  33. data/rack_generators/javascript_test/templates/library.js.erb +3 -0
  34. data/rack_generators/javascript_test/templates/test.html.erb +59 -0
  35. data/rack_generators/javascript_test/templates/test_jshoulda.html.erb +48 -0
  36. data/rails_generators/javascript_test/javascript_test_generator.rb +15 -3
  37. data/rails_generators/javascript_test/templates/assets/jshoulda.js +186 -0
  38. data/rails_generators/javascript_test/templates/test_jshoulda.html.erb +48 -0
  39. data/script/console +10 -0
  40. data/script/destroy +1 -1
  41. data/tasks/vendor/update/jshoulda.rake +18 -0
  42. data/tasks/vendor/update/jsunittest.rake +18 -0
  43. data/test/test_functional_test_generator.rb +13 -0
  44. data/test/test_generator_helper.rb +10 -1
  45. data/test/test_page_generator.rb +50 -0
  46. data/test/test_rack_javascript_test.rb +166 -0
  47. data/test/{test_javascript_test_generator.rb → test_rails_javascript_test_generator.rb} +15 -1
  48. data/test/test_unit_test_generator.rb +13 -0
  49. data/vendor/jshoulda/History.txt +108 -0
  50. data/vendor/jshoulda/License.txt +20 -0
  51. data/vendor/jshoulda/README.mdown +34 -0
  52. data/vendor/jshoulda/Rakefile +120 -0
  53. data/vendor/jshoulda/config/javascript_test_autotest.yml.sample +15 -0
  54. data/vendor/jshoulda/dist/jshoulda-0.0.1.js +62 -0
  55. data/vendor/jshoulda/dist/jshoulda-0.0.2.js +72 -0
  56. data/vendor/jshoulda/dist/jshoulda-0.0.3.js +72 -0
  57. data/vendor/jshoulda/dist/jshoulda-1.0.1.js +74 -0
  58. data/vendor/jshoulda/dist/jshoulda-1.0.js +72 -0
  59. data/vendor/jshoulda/dist/jshoulda-1.1.js +110 -0
  60. data/vendor/jshoulda/dist/jshoulda-1.2.1.js +186 -0
  61. data/vendor/jshoulda/dist/jshoulda-1.2.js +141 -0
  62. data/vendor/jshoulda/dist/jshoulda-1.2.x.js +142 -0
  63. data/vendor/jshoulda/dist/jshoulda.js +186 -0
  64. data/vendor/jshoulda/jshoulda/test/basic_test.html +42 -0
  65. data/vendor/jshoulda/lib/jstest.rb +382 -0
  66. data/vendor/jshoulda/lib/protodoc.rb +36 -0
  67. data/vendor/jshoulda/script/destroy +14 -0
  68. data/vendor/jshoulda/script/generate +14 -0
  69. data/vendor/jshoulda/script/js_autotest +1 -0
  70. data/vendor/jshoulda/script/rstakeout +97 -0
  71. data/vendor/jshoulda/site/asset_defaults.yaml +6 -0
  72. data/vendor/jshoulda/site/config.yaml +3 -0
  73. data/vendor/jshoulda/site/content/content.html +27 -0
  74. data/vendor/jshoulda/site/content/content.yaml +4 -0
  75. data/vendor/jshoulda/site/content/tutorial/tutorial.html +268 -0
  76. data/vendor/jshoulda/site/content/tutorial/tutorial.yaml +4 -0
  77. data/vendor/jshoulda/site/css/blackboard.css +88 -0
  78. data/vendor/jshoulda/site/css/jshoulda_site.css +33 -0
  79. data/vendor/jshoulda/site/css/unittest.css +42 -0
  80. data/vendor/jshoulda/site/example/minimal.html +55 -0
  81. data/vendor/jshoulda/site/js/editable.js +42 -0
  82. data/vendor/jshoulda/site/layouts/default/default.html +29 -0
  83. data/vendor/jshoulda/site/layouts/default/default.yaml +4 -0
  84. data/vendor/jshoulda/site/layouts/home_test/home_test.js +18 -0
  85. data/vendor/jshoulda/site/layouts/home_test/home_test.yaml +4 -0
  86. data/vendor/jshoulda/site/lib/analytics.rb +12 -0
  87. data/vendor/jshoulda/site/lib/default.rb +2 -0
  88. data/vendor/jshoulda/site/lib/example.rb +10 -0
  89. data/vendor/jshoulda/site/lib/hash.rb +11 -0
  90. data/vendor/jshoulda/site/lib/highlight.rb +17 -0
  91. data/vendor/jshoulda/site/page_defaults.yaml +10 -0
  92. data/vendor/jshoulda/site/templates/default/default.html +1 -0
  93. data/vendor/jshoulda/site/templates/default/default.yaml +4 -0
  94. data/vendor/jshoulda/src/HEADER +8 -0
  95. data/vendor/jshoulda/src/jshoulda.js +177 -0
  96. data/vendor/jshoulda/src/jshoulda.js.erb +3 -0
  97. data/vendor/jshoulda/tasks/bundle.rake +12 -0
  98. data/vendor/jshoulda/tasks/demo.rake +9 -0
  99. data/vendor/jshoulda/tasks/deploy.rake +29 -0
  100. data/{tasks → vendor/jshoulda/tasks}/environment.rake +0 -0
  101. data/vendor/jshoulda/tasks/javascript_test_autotest_tasks.rake +45 -0
  102. data/vendor/jshoulda/tasks/site.rake +74 -0
  103. data/vendor/jshoulda/test/assets/jsunittest.js +1004 -0
  104. data/vendor/jshoulda/test/assets/prototype.js +4324 -0
  105. data/{app_generators/newjs_iphone/templates/Html/test/assets/unittest.js → vendor/jshoulda/test/assets/prototype_unittest.js} +15 -9
  106. data/vendor/jshoulda/test/assets/unittest.css +50 -0
  107. data/vendor/jshoulda/test/unit/factory_test.html +82 -0
  108. data/vendor/jshoulda/test/unit/jshoulda2_test.html +85 -0
  109. data/vendor/jshoulda/test/unit/jshoulda_prototype_test.html +87 -0
  110. data/vendor/jshoulda/test/unit/jshoulda_test.html +86 -0
  111. data/vendor/jshoulda/test/unit/merge_methods_test.html +77 -0
  112. data/vendor/jshoulda/test/unit/names_and_count.html +68 -0
  113. data/vendor/jshoulda/test/unit/no_config_test.html +57 -0
  114. data/vendor/jshoulda/test/unit/styles_test.html +77 -0
  115. data/vendor/jshoulda/test/unit/unify_runners.html +69 -0
  116. data/vendor/jshoulda/test/unit/unnamed_context_test.html +67 -0
  117. data/vendor/jsunittest/Capfile +4 -0
  118. data/vendor/jsunittest/History.txt +37 -0
  119. data/{License.txt → vendor/jsunittest/License.txt} +0 -0
  120. data/vendor/jsunittest/README.txt +21 -0
  121. data/vendor/jsunittest/Rakefile +118 -0
  122. data/vendor/jsunittest/config/deploy.rb +20 -0
  123. data/vendor/jsunittest/config/website.yml.sample +2 -0
  124. data/vendor/jsunittest/dist/jsunittest-0.7.2.js +1004 -0
  125. data/vendor/jsunittest/dist/jsunittest.js +1004 -0
  126. data/vendor/jsunittest/dist/unittest.css +54 -0
  127. data/vendor/jsunittest/lib/jstest.rb +390 -0
  128. data/vendor/jsunittest/lib/protodoc.rb +36 -0
  129. data/vendor/jsunittest/script/destroy +14 -0
  130. data/vendor/jsunittest/script/generate +14 -0
  131. data/vendor/jsunittest/script/js_autotest +1 -0
  132. data/vendor/jsunittest/script/rstakeout +97 -0
  133. data/vendor/jsunittest/script/txt2html +75 -0
  134. data/vendor/jsunittest/src/HEADER +8 -0
  135. data/vendor/jsunittest/src/ajax.js +121 -0
  136. data/vendor/jsunittest/src/assertions.js +244 -0
  137. data/vendor/jsunittest/src/common.js +191 -0
  138. data/vendor/jsunittest/src/jsunittest.js +1004 -0
  139. data/vendor/jsunittest/src/logger.js +86 -0
  140. data/vendor/jsunittest/src/message_template.js +17 -0
  141. data/vendor/jsunittest/src/orig/unittest.js +484 -0
  142. data/vendor/jsunittest/src/prototype/event.js +72 -0
  143. data/vendor/jsunittest/src/prototype/template.js +33 -0
  144. data/vendor/jsunittest/src/runner.js +120 -0
  145. data/vendor/jsunittest/src/test_case.js +106 -0
  146. data/vendor/jsunittest/tasks/deploy.rake +29 -0
  147. data/vendor/jsunittest/tasks/environment.rake +7 -0
  148. data/vendor/jsunittest/tasks/javascript_test_autotest_tasks.rake +44 -0
  149. data/vendor/jsunittest/tasks/tmbundle.rake +24 -0
  150. data/vendor/jsunittest/tasks/website.rake +53 -0
  151. data/vendor/jsunittest/test/assets/example_test.html +50 -0
  152. data/{app_generators/newjs/templates → vendor/jsunittest}/test/assets/prototype.js +0 -0
  153. data/vendor/jsunittest/test/assets/unittest.css +54 -0
  154. data/{app_generators/newjs/templates → vendor/jsunittest}/test/assets/unittest.js +0 -0
  155. data/vendor/jsunittest/test/functional/jsunittest_test.html +209 -0
  156. data/vendor/jsunittest/test/unit/assertions_test.html +202 -0
  157. data/vendor/jsunittest/test/unit/common_test.html +61 -0
  158. data/vendor/jsunittest/test/unit/logger_test.html +98 -0
  159. data/vendor/jsunittest/test/unit/message_template_test.html +44 -0
  160. data/vendor/jsunittest/test/unit/runner_test.html +77 -0
  161. data/vendor/jsunittest/test/unit/template_test.html +57 -0
  162. data/vendor/jsunittest/test/unit/test_case_test.html +97 -0
  163. data/vendor/jsunittest/website/dist/jsunittest-0.7.2.js +1004 -0
  164. data/vendor/jsunittest/website/dist/jsunittest-getting-started.tar.gz +0 -0
  165. data/vendor/jsunittest/website/dist/jsunittest-getting-started.zip +0 -0
  166. data/vendor/jsunittest/website/dist/jsunittest-getting-started/assets/jsunittest.js +1004 -0
  167. data/vendor/jsunittest/website/dist/jsunittest-getting-started/assets/unittest.css +54 -0
  168. data/vendor/jsunittest/website/dist/jsunittest-getting-started/example_test.html +50 -0
  169. data/vendor/jsunittest/website/dist/jsunittest.js +1004 -0
  170. data/vendor/jsunittest/website/images/logo_bundle.png +0 -0
  171. data/vendor/jsunittest/website/images/passing_prototypejs_unittest_tests.jpg +0 -0
  172. data/vendor/jsunittest/website/index.html +94 -0
  173. data/vendor/jsunittest/website/index.txt +80 -0
  174. data/vendor/jsunittest/website/javascripts/rounded_corners_lite.inc.js +285 -0
  175. data/vendor/jsunittest/website/stylesheets/screen.css +138 -0
  176. data/vendor/jsunittest/website/template.html.erb +46 -0
  177. data/vendor/jsunittest/website/tmbundle/JavaScript Unit Testing.tmbundle.tar.gz +0 -0
  178. metadata +189 -24
  179. data/config/hoe.rb +0 -75
  180. data/config/requirements.rb +0 -17
  181. data/lib/newjs/version.rb +0 -9
  182. data/tasks/deployment.rake +0 -34
  183. data/tasks/jsunittest/update.rake +0 -18
  184. data/tasks/website.rake +0 -17
@@ -0,0 +1,36 @@
1
+ require 'erb'
2
+
3
+ class String
4
+ def lines
5
+ split $/
6
+ end
7
+
8
+ def strip_whitespace_at_line_ends
9
+ lines.map {|line| line.gsub(/\s+$/, '')} * $/
10
+ end
11
+ end
12
+
13
+ module Protodoc
14
+ module Environment
15
+ def include(*filenames)
16
+ filenames.map {|filename| Preprocessor.new(filename).to_s}.join("\n")
17
+ end
18
+ end
19
+
20
+ class Preprocessor
21
+ include Environment
22
+
23
+ def initialize(filename)
24
+ @filename = File.expand_path(filename)
25
+ @template = ERB.new(IO.read(@filename), nil, '%')
26
+ end
27
+
28
+ def to_s
29
+ @template.result(binding).strip_whitespace_at_line_ends
30
+ end
31
+ end
32
+ end
33
+
34
+ if __FILE__ == $0
35
+ print Protodoc::Preprocessor.new(ARGV.first)
36
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:javascript, :javascript_test, :newjs, :newjs_theme]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:javascript, :javascript_test, :newjs, :newjs_theme]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1 @@
1
+ script/rstakeout "rake test:recent:javascript" test/unit/*_test.html src/*.js
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ config = File.dirname(__FILE__) + "/../config/javascript_test_autotest.yml"
4
+ unless File.exists?(config)
5
+ puts <<-EOS
6
+ Edit config/javascript_test_autotest.yml for the browser(s) to use for autotesting.
7
+ See config/javascript_test_autotest.yml.sample for examples.
8
+ EOS
9
+ exit
10
+ end
11
+
12
+ ##
13
+ # Originally by Mike Clark.
14
+ #
15
+ # From http://www.pragmaticautomation.com/cgi-bin/pragauto.cgi/Monitor/StakingOutFileChanges.rdoc
16
+ #
17
+ # Runs a user-defined command when files are modified.
18
+ #
19
+ # Like autotest, but more customizable. This is useful when you want to do
20
+ # something other than run tests. For example, generate a PDF book, run
21
+ # a single test, or run a legacy Test::Unit suite in an app that also
22
+ # has an rSpec suite.
23
+ #
24
+ # Can use Ruby's Dir[] to get file glob. Quote your args to take advantage of this.
25
+ #
26
+ # rstakeout 'rake test:recent' **/*.rb
27
+ # => Only watches Ruby files one directory down (no quotes)
28
+ #
29
+ # rstakeout 'rake test:recent' '**/*.rb'
30
+ # => Watches all Ruby files in all directories and subdirectories
31
+ #
32
+ # Modified (with permission) by Geoffrey Grosenbach to call growlnotify for
33
+ # rspec and Test::Unit output.
34
+ #
35
+ # See the PeepCode screencast on rSpec or other blog articles for instructions on
36
+ # setting up growlnotify.
37
+
38
+ def growl(title, msg, img, pri=0, sticky="")
39
+ system "growlnotify -n autotest --image ~/.autotest_images/#{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
40
+ end
41
+
42
+ def self.growl_fail(output)
43
+ growl "FAIL", "#{output}", "fail.png", 2
44
+ end
45
+
46
+ def self.growl_pass(output)
47
+ growl "Pass", "#{output}", "pass.png"
48
+ end
49
+
50
+ command = ARGV.shift
51
+ files = {}
52
+
53
+ ARGV.each do |arg|
54
+ Dir[arg].each { |file|
55
+ files[file] = File.mtime(file)
56
+ }
57
+ end
58
+
59
+ puts "Watching #{files.keys.join(', ')}\n\nFiles: #{files.keys.length}"
60
+
61
+ trap('INT') do
62
+ puts "\nQuitting..."
63
+ exit
64
+ end
65
+
66
+
67
+ loop do
68
+
69
+ sleep 1
70
+
71
+ changed_file, last_changed = files.find { |file, last_changed|
72
+ File.mtime(file) > last_changed
73
+ }
74
+
75
+ if changed_file
76
+ files[changed_file] = File.mtime(changed_file)
77
+ puts "=> #{changed_file} changed, running #{command}"
78
+ results = `#{command}`
79
+ puts results
80
+
81
+ if results.include? 'tests'
82
+ output = results.slice(/(\d+)\s+tests?,\s*(\d+)\s+assertions?,\s*(\d+)\s+failures?(,\s*(\d+)\s+errors)?/)
83
+ if output
84
+ $~[3].to_i + $~[5].to_i > 0 ? growl_fail(output) : growl_pass(output)
85
+ end
86
+ else
87
+ output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/)
88
+ if output
89
+ $~[2].to_i > 0 ? growl_fail(output) : growl_pass(output)
90
+ end
91
+ end
92
+ # TODO Generic growl notification for other actions
93
+
94
+ puts "=> done"
95
+ end
96
+
97
+ end
@@ -0,0 +1,6 @@
1
+ # Built-in
2
+ extension: dat
3
+ filters: []
4
+ binary: true
5
+
6
+ # Custom
@@ -0,0 +1,3 @@
1
+ output_dir: "output"
2
+ data_source: "filesystem"
3
+ router: "default"
@@ -0,0 +1,27 @@
1
+ <p><strong>jShoulda</strong> acts as a wrapper to the <a href="http://github.com/drnic/jsunittest/">JsUnitTest</a> library, allowing you to write JavaScript unit tests in a <a href="http://www.thoughtbot.com/projects/shoulda">Shoulda</a>-like language.</p>
2
+
3
+
4
+ <h2>What you write</h2>
5
+
6
+ <%= highlight(render('home_test'), 'javascript', {:id => 'editable-test'}) %>
7
+
8
+
9
+ <h2>... is what you test</h2>
10
+
11
+ <div id="testlog"><p>The tests results will be displayed here.</p></div>
12
+
13
+ <script type="text/javascript">
14
+ <%= render('home_test') %>
15
+ </script>
16
+
17
+
18
+ <p class="download"><a href="/dl/jshoulda.zip">Download jShoulda</a></p>
19
+
20
+ <p>Interested? Check our <a href="/tutorial/">tutorial on jShoulda</a> (or this <a href="http://www.vimeo.com/2324738">screencast with Spanish audio track</a>). </p>
21
+
22
+ <div class="new">
23
+ <p>Still writing too much? <a href="http://www.vimeo.com/2049956">Watch</a> and <a href="/dl/jshoulda-tmbundle.zip">get the jShoulda TextMate Bundle</a>.</p>
24
+ </div>
25
+
26
+ <p>jShoulda has been created by <a href="http://choangalvez.nom.es/">Choan Galvez</a> and is freely distributable under the terms of a MIT-style license. The source code resides in a <a href="http://github.com/choan/jshoulda/">Git repository at github</a>.</p>
27
+
@@ -0,0 +1,4 @@
1
+ # Built-in
2
+
3
+ # Custom
4
+ title: jShoulda - Shoulda for JavaScript
@@ -0,0 +1,268 @@
1
+ <p>jShoulda, just as the <a href="http://www.thoughtbot.com/projects/shoulda" title="thoughtbot: Shoulda testing plugin">Shoulda</a> testing plugin it's inspired on, <q>makes it easy to write elegant, understandable, and maintainable tests.</q> JavaScript.</p>
2
+
3
+
4
+ <h2>A minimal test document</h2>
5
+
6
+ <p>You need the following files to use jShoulda:</p>
7
+
8
+ <ul>
9
+ <li><a href="http://jsunittest.com/dist/jsunittest.js">jsunittest.js</a>, from the <a href="http://jsunittest.com/">JsUnitTest</a> project. If you're using <a href="http://www.prototypejs.org/" title="Prototype JavaScript framework: Easy Ajax and DOM manipulation for dynamic web applications">prototype</a>, you may use <a href="http://github.com/sstephenson/prototype/tree/master%2Ftest%2Flib%2Fassets%2Funittest.js?raw=true">unittest.js</a> instead.</li>
10
+ <li><a href="/js/jshoulda.js">jshoulda.js</a>, from the <a href="/">jShoulda</a> project.</li>
11
+ </ul>
12
+
13
+ <p>Create an HTML document which references both scripts. Include a <code>div</code> element with <code>id</code> <var>testlog</var>, then your testing code. <a href="/example/minimal.html">Run it</a>, enjoy.
14
+
15
+ <%= example('minimal.html', false) %>
16
+
17
+ <h2>jShoulda syntax</h2>
18
+
19
+ <p>By default, jShoulda exports two methods to the global space: <code>context</code> and <code>should</code>.</p>
20
+
21
+ <p>To create a test runner (which will be automatically executed on <code>window.onload</code>), invoke <code>context</code>, then execute the result:</p>
22
+
23
+ <%
24
+ code =<<-EOD
25
+ context('The name of the context')();
26
+ EOD
27
+ %>
28
+ <%= highlight(code, 'javascript') %>
29
+
30
+ <p>The first argument to <code>context</code> is the base name for actual tests derived from it. The second is an (optional) object which may include <code>setup</code> and <code>teardown</code> methods:</p>
31
+
32
+ <%
33
+ code =<<-EOD
34
+ context('The name of the context', {
35
+ setup: function() {
36
+ // `this` is a Test.Unit.Testcase instance
37
+ this.foo = 1;
38
+ },
39
+ teardown: function() {
40
+ // you don't need to clean up `this`'s properties,
41
+ // the Test.Unit.TestCase is brand new for every actual test
42
+ }
43
+ }
44
+ )();
45
+ EOD
46
+ %>
47
+ <%= highlight(code, 'javascript') %>
48
+
49
+ <p>Hint: You can use <code>before</code>/<code>after</code> instead of <code>setup</code>/<code>teardown</code> if it makes you feel better.</p>
50
+
51
+ <p>Include <code>should</code> calls after the (optional) configuration object to create actual tests:</p>
52
+
53
+ <%
54
+ code =<<-EOD
55
+ context('A context', {
56
+ setup: function() {
57
+ this.foo = 1;
58
+ }
59
+ },
60
+ should('run its setup function', function() {
61
+ this.assertEqual(1, this.foo);
62
+ })
63
+ )();
64
+
65
+ // if you don't need setup/teardown methods
66
+ context('A context',
67
+ should('run its setup function', function() {
68
+ this.assertEqual(1, this.foo);
69
+ })
70
+ )();
71
+ EOD
72
+ %>
73
+ <%= highlight(code, 'javascript') %>
74
+
75
+ <p>Every call to <code>should</code> must pass a name and a callback as arguments. Assertions are run inside the callback, where <code>this</code> points to a <code>Test.Unit</code> instance (the same you can manipulate on <code>setup</code>/<code>teardown</code>).</p>
76
+
77
+ <p>To create new contexts (on which every <code>setup</code>/<code>teardown</code> function from parent contexts is ran), pass a call to <code>context</code> as an argument:</p>
78
+
79
+ <%
80
+ code =<<-EOD
81
+ context('A context', {
82
+ setup: function() {
83
+ this.foo = 1;
84
+ }
85
+ },
86
+ should('run its setup function', function() {
87
+ this.assertEqual(1, this.foo);
88
+ }),
89
+ context('which is a "nested" context', {
90
+ setup: function() {
91
+ this.foo +=1;
92
+ }
93
+ },
94
+ should('run both setup functions', function() {
95
+ this.assertEqual(2, this.foo);
96
+ })
97
+ )
98
+ )();
99
+ EOD
100
+ %>
101
+ <%= highlight(code, 'javascript') %>
102
+
103
+ <p>You can nest as much contexts as you like.</p>
104
+
105
+ <h2>Assertions</h2>
106
+
107
+ <p>Assertions come from the JsUnitTest library. Almost every assertion accepts three arguments: the expected value, the actual one and an optional message. An intentionally incomplete list of possible assertions follows:</p>
108
+
109
+ <ul>
110
+ <li>assert</li>
111
+ <li>assertEqual</li>
112
+ <li>assertNotEqual</li>
113
+ <li>assertEnumEqual</li>
114
+ <li>assertEnumNotEqual</li>
115
+ <li>assertHashEqual</li>
116
+ <li>assertHashNotEqual</li>
117
+ <li>assertIdentical</li>
118
+ <li>assertNotIdentical</li>
119
+ <li>assertNull</li>
120
+ <li>assertNotNull</li>
121
+ <li>assertUndefined</li>
122
+ <li>assertNotUndefined</li>
123
+ </ul>
124
+
125
+
126
+ <h2>Customizing the Test Runner</h2>
127
+
128
+ <p>The progress and results of your tests are shown, by default, on a <code>div</code> element whose <code>id</code> equals <var>testlog</var>. If you want to modify the behaviour of the runner, pass a configuration object when invocating the root context call result:
129
+
130
+ <%
131
+ code =<<-EOD
132
+ context('A context',
133
+ should('run a test', function() {
134
+ this.assert('Yay!');
135
+ }),
136
+ )({ testLog: 'my_test_log_div_id' });
137
+ EOD
138
+ %>
139
+ <%= highlight(code, 'javascript') %>
140
+
141
+ <h2 id="aliasing">Aliasing</h2>
142
+
143
+ <p>You can easily create alias to <code>context</code> and <code>should</code> methods. In fact, they are alias themselves.</p>
144
+
145
+ <p>Say you wanted to use <code>some</code>/<code>must</code> instead of (actually, in addition) to <code>context</code>/<code>should</code>. You could just do:</p>
146
+
147
+ <%
148
+ code = <<-EOD
149
+ jShoulda
150
+ .setShouldAlias('must')
151
+ .setContextAlias('some');
152
+
153
+ some('context',
154
+ must('run a test', function() {
155
+ this.assert('Yay!');
156
+ })
157
+ )();
158
+ EOD
159
+ %>
160
+ <%= highlight(code, 'javascript') %>
161
+
162
+ <p>Both <code>setBlahAlias</code> create methods which works just as <code>context</code> and <code>should</code> do. But instead of getting a “should” connector, you get a connector named as the method, that is, the full name of the test we've created in the previous example, would be “A context must run a test”.</p>
163
+
164
+ <p>Pass a second argument to <code>setShouldAlias</code> if you want a different connector (or no connector at all). Pass a second argument to <code>setContextAlias</code> if you want a prefix for your context.</p>
165
+
166
+ <%
167
+ code = <<-EOD
168
+ jShoulda
169
+ .setShouldAlias('it', '')
170
+ .setContextAlias('describe');
171
+
172
+ describe('A context',
173
+ it('runs a test', function() {
174
+ this.assert('Yay!');
175
+ })
176
+ )();
177
+ EOD
178
+ %>
179
+ <%= highlight(code, 'javascript') %>
180
+
181
+
182
+ <p>or</p>
183
+
184
+ <%=
185
+ highlight(<<-EOD, 'javascript')
186
+ jShoulda
187
+ .setContextAlias('un', 'Un')
188
+ .setContextAlias('para', 'para')
189
+ .setShouldAlias('deberia', 'debería');
190
+
191
+ un("programador",
192
+ para("ser feliz",
193
+ deberia("poder comer melocotones de Calanda todo el año", function() {
194
+ })
195
+ )
196
+ )();
197
+ EOD
198
+ %>
199
+
200
+ <p>In this last case, the outputted name for our test will be “Un programador para ser feliz debería poder comer melocotones de Calanda todo el año”.</p>
201
+
202
+ <p>Of course, you can mix as much aliases as you like.</p>
203
+
204
+ <h2 id="extra-properties">Extending the test cases (new in 1.2)</h2>
205
+
206
+ <p>Properties in the configuration object (other than setup/teardown/before/after) are copied to each <code>Test.Unit.Testcase</code> inside the context—that is, are accesible through <code>this</code> inside the <code>should</code> callbacks.</p>
207
+
208
+ <%
209
+ code = <<-EOD
210
+ context("A 'should' execution", {
211
+ assertLocalYipiyay : function(actual, msg) {
212
+ this.assertEqual('yipiyay', actual, msg);
213
+ }
214
+ },
215
+ should("get access to extra properties defined on the configuration object", function() {
216
+ this.assertLocalYipiyay('yipiyay');
217
+ })
218
+ )();
219
+ EOD
220
+ %>
221
+ <%= highlight(code, 'javascript') %>
222
+
223
+ <h2 id="unifying-test-runners">Unifying test runners (new in 1.2)</h2>
224
+
225
+ <p>By default, each root contexts creates a new <code>Test.Unit.Runner</code> instance, so if you're not using different loggers, headers may report wrong results. And, if you are using the rake tasks provided with <strong>newjs</strong> or <strong>rails</strong>, only the results of one of the contexts would get logged.</p>
226
+
227
+ <p>Not a problem? Well... in addition, you can't load tests from multiple files! Isn't that a problem?</p>
228
+
229
+ <p>jShoulda provides the method <code>unifyRunners</code>. It saves the world.</p>
230
+
231
+ <p>There are three ways to use the method:</p>
232
+
233
+ <%
234
+ code = <<-EOD
235
+ // with no arguments
236
+ // every next root context inherits the runner
237
+ // from the previous (if any) root context
238
+ jShoulda
239
+ .unifyRunners();
240
+
241
+
242
+ // with option arguments
243
+ // every next root context uses a new
244
+ // runner created from the options
245
+ jShoulda
246
+ .unifyRunners({testLog: 'testlog2'});
247
+
248
+ // if you've created a testRunner through the
249
+ // traditional syntax, you can reuse it
250
+ var runner = new Test.Unit.Runner({
251
+ testDummy : function() {
252
+ this.assert(true);
253
+ }
254
+ });
255
+ jShoulda
256
+ .unifyRunners(runners);
257
+ EOD
258
+ %>
259
+ <%= highlight(code, 'javascript') %>
260
+
261
+
262
+
263
+
264
+ <h2>Questions?</h2>
265
+
266
+ <p>Feel free to ask on the <a href="http://groups.google.com/group/jshoulda">jShoulda Google Group</a>.</p>
267
+
268
+ <p><a href="/">jShoulda home page</a>.</p>