visionmedia-jspec 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/History.rdoc +5 -0
  2. data/bin/jspec +1 -1
  3. data/jspec.gemspec +4 -4
  4. data/lib/jspec.js +56 -43
  5. data/spec/spec.js +23 -0
  6. metadata +3 -3
data/History.rdoc CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ === 2.0.1 / 2009-05-01
3
+
4
+ * Added better failure messages for throw_error matcher
5
+ * Renamed print() to puts() [#108]
6
+
2
7
  === 2.0.0 / 2009-04-27
3
8
 
4
9
  * Added DOM loading indicator [#105]
data/bin/jspec CHANGED
@@ -10,7 +10,7 @@ require 'fileutils'
10
10
  RHINO = 'java org.mozilla.javascript.tools.shell.Main'
11
11
 
12
12
  program :name, 'JSpec'
13
- program :version, '2.0.0'
13
+ program :version, '2.0.1'
14
14
  program :description, 'JavaScript BDD Testing Framework'
15
15
  default_command :bind
16
16
 
data/jspec.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{jspec}
5
- s.version = "2.0.0"
5
+ s.version = "2.0.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-04-27}
9
+ s.date = %q{2009-05-01}
10
10
  s.default_executable = %q{jspec}
11
11
  s.description = %q{JavaScript BDD Testing Framework}
12
12
  s.email = %q{tj@vision-media.ca}
@@ -18,12 +18,12 @@ Gem::Specification.new do |s|
18
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Jspec", "--main", "README.rdoc"]
19
19
  s.require_paths = ["lib"]
20
20
  s.rubyforge_project = %q{jspec}
21
- s.rubygems_version = %q{1.3.1}
21
+ s.rubygems_version = %q{1.3.2}
22
22
  s.summary = %q{JavaScript BDD Testing Framework}
23
23
 
24
24
  if s.respond_to? :specification_version then
25
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
- s.specification_version = 2
26
+ s.specification_version = 3
27
27
 
28
28
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
29
  s.add_runtime_dependency(%q<visionmedia-commander>, [">= 3.2.9"])
data/lib/jspec.js CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  var JSpec = {
7
7
 
8
- version : '2.0.0',
8
+ version : '2.0.1',
9
9
  suites : [],
10
10
  allSuites : [],
11
11
  matchers : {},
@@ -81,13 +81,13 @@
81
81
  */
82
82
 
83
83
  DOM : function(results, options) {
84
- id = option('reportToId') || 'jspec'
85
- report = document.getElementById(id)
86
- failuresOnly = option('failuresOnly')
87
- classes = results.stats.failures ? 'has-failures' : ''
84
+ var id = option('reportToId') || 'jspec'
85
+ var report = document.getElementById(id)
86
+ var failuresOnly = option('failuresOnly')
87
+ var classes = results.stats.failures ? 'has-failures' : ''
88
88
  if (!report) throw 'JSpec requires the element #' + id + ' to output its reports'
89
89
 
90
- markup =
90
+ var markup =
91
91
  '<div id="jspec-report" class="' + classes + '"><div class="heading"> \
92
92
  <span class="passes">Passes: <em>' + results.stats.passes + '</em></span> \
93
93
  <span class="failures">Failures: <em>' + results.stats.failures + '</em></span> \
@@ -101,7 +101,7 @@
101
101
  }
102
102
 
103
103
  renderSuite = function(suite) {
104
- displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
104
+ var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
105
105
  if (displaySuite && suite.hasSpecs()) {
106
106
  markup += '<tr class="description"><td colspan="2">' + suite.description + '</td></tr>'
107
107
  each(suite.specs, function(i, spec){
@@ -150,7 +150,7 @@
150
150
  if (displaySuite && suite.hasSpecs()) {
151
151
  puts(color(' ' + suite.description, 'bold'))
152
152
  each(suite.specs, function(spec){
153
- assertionsGraph = inject(spec.assertions, '', function(graph, assertion){
153
+ var assertionsGraph = inject(spec.assertions, '', function(graph, assertion){
154
154
  return graph + color('.', assertion.passed ? 'green' : 'red')
155
155
  })
156
156
  if (spec.requiresImplementation())
@@ -189,7 +189,7 @@
189
189
  if (suite.ran) {
190
190
  console.group(suite.description)
191
191
  each(suite.specs, function(spec){
192
- assertionCount = spec.assertions.length + ':'
192
+ var assertionCount = spec.assertions.length + ':'
193
193
  if (spec.requiresImplementation())
194
194
  console.warn(spec.description)
195
195
  else if (spec.passed())
@@ -233,9 +233,9 @@
233
233
  run : function() {
234
234
  // TODO: remove unshifting of expected
235
235
  expected.unshift(actual == null ? null : actual.valueOf())
236
- this.result = matcher.match.apply(JSpec, expected)
236
+ this.result = matcher.match.apply(this, expected)
237
237
  this.passed = negate ? !this.result : this.result
238
- if (!this.passed) this.message = matcher.message(actual, expected, negate, matcher.name)
238
+ if (!this.passed) this.message = matcher.message.call(this, actual, expected, negate, matcher.name)
239
239
  return this
240
240
  }
241
241
  })
@@ -330,8 +330,9 @@
330
330
  // Run the assertion
331
331
 
332
332
  run : function() {
333
- methodString = 'expected ' + object.toString() + '.' + method + '()'
334
- times = function(n) {
333
+ var methodString = 'expected ' + object.toString() + '.' + method + '()'
334
+
335
+ function times(n) {
335
336
  return n > 2 ? n + ' times' : { 1 : 'once', 2 : 'twice' }[n]
336
337
  }
337
338
 
@@ -340,12 +341,12 @@
340
341
  ', but ' + (this.calls.length == 0 ? ' was not called' : ' was called ' + times(this.calls.length))
341
342
 
342
343
  if (this.expectedResult && this.anyResultsFail())
343
- this.message = methodString + ' to return ' + print(this.expectedResult) +
344
- ' but got ' + print(this.failingResult())
344
+ this.message = methodString + ' to return ' + puts(this.expectedResult) +
345
+ ' but got ' + puts(this.failingResult())
345
346
 
346
347
  if (this.expectedArgs && this.anyArgsFail())
347
- this.message = methodString + ' to be called with ' + print.apply(this, this.expectedArgs) +
348
- ' but was called with ' + print.apply(this, this.failingArgs())
348
+ this.message = methodString + ' to be called with ' + puts.apply(this, this.expectedArgs) +
349
+ ' but was called with ' + puts.apply(this, this.failingArgs())
349
350
 
350
351
  if (!this.message.length)
351
352
  this.passed = true
@@ -376,7 +377,7 @@
376
377
  // Add a spec to the suite
377
378
 
378
379
  addSpec : function(description, body) {
379
- spec = new JSpec.Spec(description, body)
380
+ var spec = new JSpec.Spec(description, body)
380
381
  this.specs.push(spec)
381
382
  JSpec.stats.specs++ // TODO: abstract
382
383
  spec.suite = this
@@ -391,7 +392,7 @@
391
392
  // Add a nested suite
392
393
 
393
394
  addSuite : function(description, body) {
394
- suite = new JSpec.Suite(description, body)
395
+ var suite = new JSpec.Suite(description, body)
395
396
  JSpec.allSuites.push(suite)
396
397
  suite.name = suite.description
397
398
  suite.description = this.description + ' ' + suite.description
@@ -554,7 +555,7 @@
554
555
 
555
556
  shareBehaviorsOf : function(description) {
556
557
  if (suite = this.findSuite(description)) this.copySpecs(suite, this.currentSuite)
557
- else throw 'failed to share behaviors. ' + print(description) + ' is not a valid Suite name'
558
+ else throw 'failed to share behaviors. ' + puts(description) + ' is not a valid Suite name'
558
559
  },
559
560
 
560
561
  /**
@@ -581,7 +582,7 @@
581
582
  */
582
583
 
583
584
  argumentsToArray : function(arguments, offset) {
584
- args = []
585
+ var args = []
585
586
  for (i = 0; i < arguments.length; i++) args.push(arguments[i])
586
587
  return args.slice(offset || 0)
587
588
  },
@@ -616,10 +617,10 @@
616
617
  */
617
618
 
618
619
  defaultMatcherMessage : function(actual, expected, negate, name) {
619
- return 'expected ' + print(actual) + ' to ' +
620
+ return 'expected ' + puts(actual) + ' to ' +
620
621
  (negate ? 'not ' : '') +
621
622
  name.replace(/_/g, ' ') +
622
- ' ' + print.apply(this, expected.slice(1))
623
+ ' ' + puts.apply(this, expected.slice(1))
623
624
  },
624
625
 
625
626
  /**
@@ -730,10 +731,10 @@
730
731
  * @api public
731
732
  */
732
733
 
733
- print : function(object) {
734
+ puts : function(object) {
734
735
  if (arguments.length > 1) {
735
736
  return map(argumentsToArray(arguments), function(arg){
736
- return print(arg)
737
+ return puts(arg)
737
738
  }).join(', ')
738
739
  }
739
740
  if (object === undefined) return ''
@@ -741,7 +742,7 @@
741
742
  if (object === true) return 'true'
742
743
  if (object === false) return 'false'
743
744
  if (object.an_instance_of) return 'an instance of ' + object.an_instance_of.name
744
- if (object.jquery && object.selector.length > 0) return 'selector ' + print(object.selector) + ''
745
+ if (object.jquery && object.selector.length > 0) return 'selector ' + puts(object.selector) + ''
745
746
  if (object.jquery) return escape(object.html())
746
747
  if (object.nodeName) return escape(object.outerHTML)
747
748
  switch (object.constructor) {
@@ -749,11 +750,11 @@
749
750
  case Number: return object
750
751
  case Array :
751
752
  return inject(object, '[', function(b, v){
752
- return b + ', ' + print(v)
753
+ return b + ', ' + puts(v)
753
754
  }).replace('[,', '[') + ' ]'
754
755
  case Object:
755
756
  return inject(object, '{', function(b, k, v) {
756
- return b + ', ' + print(k) + ' : ' + print(v)
757
+ return b + ', ' + puts(k) + ' : ' + puts(v)
757
758
  }).replace('{,', '{') + ' }'
758
759
  default:
759
760
  return escape(object.toString())
@@ -794,7 +795,7 @@
794
795
  */
795
796
 
796
797
  does : function(actual, matcher, expected) {
797
- assertion = new JSpec.Assertion(JSpec.matchers[matcher], actual, argumentsToArray(arguments, 2))
798
+ var assertion = new JSpec.Assertion(JSpec.matchers[matcher], actual, argumentsToArray(arguments, 2))
798
799
  return assertion.run().result
799
800
  },
800
801
 
@@ -812,7 +813,7 @@
812
813
 
813
814
  expect : function(actual) {
814
815
  assert = function(matcher, args, negate) {
815
- expected = []
816
+ var expected = []
816
817
  for (i = 1; i < args.length; i++) expected.push(args[i])
817
818
  assertion = new JSpec.Assertion(matcher, actual, expected, negate)
818
819
  if (matcher.defer) assertion.run()
@@ -1000,7 +1001,7 @@
1000
1001
  */
1001
1002
 
1002
1003
  describe : function(description, body) {
1003
- suite = new JSpec.Suite(description, body)
1004
+ var suite = new JSpec.Suite(description, body)
1004
1005
  this.allSuites.push(suite)
1005
1006
  this.suites.push(suite)
1006
1007
  },
@@ -1027,10 +1028,10 @@
1027
1028
  */
1028
1029
 
1029
1030
  evalBody : function(body, errorMessage) {
1030
- dsl = this.DSL || this.DSLs.snake
1031
- matchers = this.matchers
1032
- context = this.context || this.defaultContext
1033
- contents = this.contentsOf(body)
1031
+ var dsl = this.DSL || this.DSLs.snake
1032
+ var matchers = this.matchers
1033
+ var context = this.context || this.defaultContext
1034
+ var contents = this.contentsOf(body)
1034
1035
  try { eval('with (dsl){ with (context) { with (matchers) { ' + contents + ' }}}') }
1035
1036
  catch(e) { error(errorMessage, e) }
1036
1037
  },
@@ -1067,7 +1068,7 @@
1067
1068
  */
1068
1069
 
1069
1070
  range : function(start, end) {
1070
- current = parseInt(start), end = parseInt(end), values = [current]
1071
+ var current = parseInt(start), end = parseInt(end), values = [current]
1071
1072
  if (end > current) while (++current <= end) values.push(current)
1072
1073
  else while (--current >= end) values.push(current)
1073
1074
  return '[' + values + ']'
@@ -1212,7 +1213,7 @@
1212
1213
  */
1213
1214
 
1214
1215
  query : function(key, queryString) {
1215
- queryString = (queryString || (main.location ? main.location.search : null) || '').substring(1)
1216
+ var queryString = (queryString || (main.location ? main.location.search : null) || '').substring(1)
1216
1217
  return inject(queryString.split('&'), null, function(value, pair){
1217
1218
  parts = pair.split('=')
1218
1219
  return parts[0] == key ? parts[1].replace(/%20|\+/gmi, ' ') : value
@@ -1241,7 +1242,7 @@
1241
1242
  */
1242
1243
 
1243
1244
  post : function(url, data) {
1244
- request = this.xhr()
1245
+ var request = this.xhr()
1245
1246
  request.open('POST', url, false)
1246
1247
  request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
1247
1248
  request.send(data)
@@ -1294,7 +1295,7 @@
1294
1295
 
1295
1296
  load : function(file) {
1296
1297
  if (this.hasXhr()) {
1297
- request = this.xhr()
1298
+ var request = this.xhr()
1298
1299
  request.open('GET', file, false)
1299
1300
  request.send(null)
1300
1301
  if (request.readyState == 4) return request.responseText
@@ -1322,7 +1323,6 @@
1322
1323
  // --- Utility functions
1323
1324
 
1324
1325
  var main = this
1325
- var puts = main.print
1326
1326
  var map = JSpec.map
1327
1327
  var any = JSpec.any
1328
1328
  var find = JSpec.any
@@ -1336,7 +1336,7 @@
1336
1336
  var error = JSpec.error
1337
1337
  var escape = JSpec.escape
1338
1338
  var extend = JSpec.extend
1339
- var print = JSpec.print
1339
+ var puts = JSpec.puts
1340
1340
  var hash = JSpec.hash
1341
1341
  var query = JSpec.query
1342
1342
  var strip = JSpec.strip
@@ -1407,9 +1407,10 @@
1407
1407
  return true
1408
1408
  },
1409
1409
 
1410
- throw_error : function(actual, expected) {
1410
+ throw_error : { match : function(actual, expected) {
1411
1411
  try { actual() }
1412
1412
  catch (e) {
1413
+ this.e = e
1413
1414
  if (expected == undefined) return true
1414
1415
  switch (expected.constructor) {
1415
1416
  case RegExp : return expected.test(e)
@@ -1417,7 +1418,19 @@
1417
1418
  case String : return expected == e.toString()
1418
1419
  }
1419
1420
  }
1420
- },
1421
+ }, message : function(actual, expected, negate) {
1422
+ // TODO: fix when expected is ONLY expected values
1423
+ expected = (function(){
1424
+ if (expected[1] == undefined) return 'exception'
1425
+ switch (expected[1].constructor) {
1426
+ case RegExp : return 'exception matching ' + puts(expected[1])
1427
+ case Function : return 'instance of ' + expected[1].name
1428
+ case String : return 'exception of ' + puts(expected[1])
1429
+ }
1430
+ })()
1431
+ return 'expected ' + (negate ? 'no ' : 'an ' ) + expected +
1432
+ ' to be thrown, but ' + (this.e ? 'got ' + puts(this.e) : 'nothing was')
1433
+ }},
1421
1434
 
1422
1435
  have : function(actual, length, property) {
1423
1436
  return actual[property].length == length
data/spec/spec.js CHANGED
@@ -41,6 +41,29 @@ describe 'Negative specs'
41
41
  -{ throw 'foo' }.should.not.throw_error
42
42
  end
43
43
 
44
+ it 'should fail saying which error has been thrown'
45
+ -{ throw 'foo' }.should.throw_error 'bar'
46
+ end
47
+
48
+ it 'should fail saying no error was thrown'
49
+ -{ }.should.throw_error 'foo'
50
+ end
51
+
52
+ it 'should fail saying no error matching was thrown'
53
+ -{ throw 'bar' }.should.throw_error(/foo/)
54
+ end
55
+
56
+ it 'should fail saying no error matching foo should be thrown'
57
+ -{ throw 'foo' }.should.not.throw_error(/foo/)
58
+ end
59
+
60
+ it 'should fail with constructor name'
61
+ function Foo(){}
62
+ function Bar(){}
63
+ Bar.prototype.toString = function(){ return 'Bar error: oh no' }
64
+ -{ throw new Bar }.should.throw_error Foo
65
+ end
66
+
44
67
  it 'should fail with message of first failure'
45
68
  true.should.be_true
46
69
  'bar'.should.match(/foo/gm)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-jspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-27 00:00:00 -07:00
12
+ date: 2009-05-01 00:00:00 -07:00
13
13
  default_executable: jspec
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -114,7 +114,7 @@ requirements: []
114
114
  rubyforge_project: jspec
115
115
  rubygems_version: 1.2.0
116
116
  signing_key:
117
- specification_version: 2
117
+ specification_version: 3
118
118
  summary: JavaScript BDD Testing Framework
119
119
  test_files: []
120
120