Class: Jasnode::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/jasnode/core.rb

Class Method Summary collapse

Class Method Details

.build_jasmine_test_file(jasmine, specs, verbose) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/jasnode/core.rb', line 22

def self.build_jasmine_test_file(jasmine, specs, verbose)
  spec_list = ""
  specs.each do |spec|
    spec_list += "  process.compile(fs.readFileSync('#{spec}'), '#{spec}');\n"
  end
  <<-eos
// This file is autogenerated by jasnode
// It was inspired by node-jasmine (c) 2010 Adam Abrons and Misko Hevery http://getangular.com
// Copyright (c) 2010 Cory Ondrejka. All rights reserved.
// See MIT.LICENSE for licensing details.

var fs = require('fs');
var sys = require('sys');

var filename = '#{jasmine}';
var src = fs.readFileSync(filename);

// give jasmine values to bind
global.window = {
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval
};

var jasmine = process.compile(src + '\\njasmine;', filename);

delete global.window;

jasmine.run_specs = function() {
  var log = [];
  var columnCounter = 0;
  var verbose = #{verbose};
  
  // process in specs collected by jasnode
#{spec_list}
  
  // override jasmine reporter base class
  var jasmineEnv = jasmine.getEnv();
  jasmineEnv.reporter = {
log: function(str){
},

reportRunnerStarting: function(runner) {
  date = new Date();
  sys.puts('Starting spec run at ' + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
  start = date.getTime();
},

reportSuiteResults: function(suite) {
  var spec_results = suite.results();
  var path = [];
  while(suite) {
    path.unshift(suite.description);
    suite = suite.parentSuite;
  }
  var description = path.join(' -> ');

  spec_results.items_.forEach(function(spec){
    if (spec.failedCount > 0 && spec.description) {
      log.push(description + '\\n');
      log.push(' it ' + spec.description + '\\n');
      spec.items_.forEach(function(result){
        log.push(' ' + result.trace.stack + '\\n');
      });
    }
  });
},

reportSpecResults: function(spec) {
  sys.print(spec.results().failedCount ? "F" : ".");
  if (columnCounter++ < 50) return;
  columnCounter = 0;
  sys.print('\\n');
},

reportRunnerResults: function(runner) {
  elapsed = (Number(new Date) - start) / 1000;
  sys.puts('\\n');
  if (verbose)
    log.forEach(function(log){ sys.puts(log); });
    
  sys.puts('\\n');
  sys.puts('Finished in ' + elapsed + ' seconds');
  sys.puts(jasmine.results(runner));
  process.exit(runner.results().failedCount);
}
  };
  jasmineEnv.execute();
};

// print out the results
jasmine.results = function(runner){
  var results = runner.results();
  var suites = runner.suites();
  var msg = '';
  msg += "\\033[1m" + results.totalCount + ' example' + ((results.totalCount === 1) ? '' : 's') + '\\033[22m, ';
  if (0 == results.failedCount)
msg += "\\033[32m" + results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\\033[39m\\n';
  else
msg += "\\033[31m" + results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\\033[39m\\n';
  return msg;
};

// create a busy wait to allow for async testing
jasmine.async_spec_wait = function() {
  var wait = jasmine.async_spec_wait;
  wait.start = new Date().getTime();
  wait.done = false;
  (function inner_wait(){
waits(10);
runs(function() {
  if (wait.start + wait.timeout < new Date().getTime()) {
    expect('timeout waiting for spec').toBeNull();
  } else if (wait.done) {
    wait.done = false;
  } else {
    inner_wait();
  }
});
  })();  
};

// timeout after 10 seconds of waiting
jasmine.async_spec_wait.timeout = 10 * 1000;

jasmine.async_spec_done = function() {
  jasmine.async_spec_wait.done = true;
};

// make sure all the jasmine methods are available
for ( var key in jasmine) {
  exports[key] = jasmine[key];
}
  eos
end

.find_jasmineObject



17
18
19
20
# File 'lib/jasnode/core.rb', line 17

def self.find_jasmine
  dir = File.join(File.dirname(__FILE__),"..","..","jasmine","**","jasmine-*.js")
  Dir[dir]
end

.find_specs(root_dir) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/jasnode/core.rb', line 7

def self.find_specs(root_dir)
  dir = File.join(root_dir, "**", "*_spec.js")
  files = Dir[dir]
  dir = File.join(root_dir, "**", "spec.js")
  files += Dir[dir]
  dir = File.join(root_dir, "**", "spec_*.js")
  files += Dir[dir]
  files.uniq
end

.spec(verbose, logfile) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/jasnode/core.rb', line 159

def self.spec(verbose, logfile)
  File.open(File.expand_path(File.join("lib", "jasnode.js")), "w") {|f| f.write(Jasnode::Spec.build_jasmine_test_file(Jasnode::Spec.find_jasmine, Jasnode::Spec.find_specs(Dir.pwd), true)) }
  Dir.chdir("spec")
  output = ""
  if logfile
    output = %x[node runner.js > #{logfile}]
  else
    output = %x[node runner.js]
  end
  Dir.chdir("..")
  output
end