Class: JavaScriptTestTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/newjs/jstest.rb

Instance Method Summary collapse

Constructor Details

#initialize(name = :test, port = 4711) {|_self| ... } ⇒ JavaScriptTestTask

Returns a new instance of JavaScriptTestTask.

Yields:

  • (_self)

Yield Parameters:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/newjs/jstest.rb', line 271

def initialize(name=:test, port=4711)
  @name = name
  @tests = []
  @browsers = []
  @port = port
  @queue = Queue.new

  @server = WEBrick::HTTPServer.new(:Port => @port) # TODO: make port configurable
  @server.mount_proc("/results") do |req, res|
    @queue.push({
      :tests => req.query['tests'].to_i,
      :assertions => req.query['assertions'].to_i,
      :failures => req.query['failures'].to_i,
      :errors => req.query['errors'].to_i
    })
    res.body = "OK"
  end
  @server.mount("/response", BasicServlet)
  @server.mount("/slow", SlowServlet)
  @server.mount("/down", DownServlet)
  @server.mount("/inspect", InspectionServlet)
  yield self if block_given?
  define
end

Instance Method Details

#browser(browser) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/newjs/jstest.rb', line 363

def browser(browser)
  browser =
    case(browser)
      when :firefox
        FirefoxBrowser.new
      when :safari
        SafariBrowser.new
      when :ie
        IEBrowser.new
      when :konqueror
        KonquerorBrowser.new
      when :opera
        OperaBrowser.new
      else
        browser
    end

  @browsers<<browser
end

#defineObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/newjs/jstest.rb', line 296

def define
  task @name do
    trap("INT") { @server.shutdown }
    t = Thread.new { @server.start }
    
    # run all combinations of browsers and tests
    @browsers.each do |browser|
      if browser.supported?
        t0 = Time.now
        results = {:tests => 0, :assertions => 0, :failures => 0, :errors => 0}
        errors = []
        failures = []
        browser.setup
        puts "\nStarted tests in #{browser}"
        @tests.each do |test|
          params = "resultsURL=http://localhost:#{@port}/results&t=" + ("%.6f" % Time.now.to_f)
          if test.is_a?(Hash)
            params << "&tests=#{test[:testcases]}" if test[:testcases]
            test = test[:url]
          end
          browser.visit("http://localhost:#{@port}#{test}?#{params}")
 
          result = @queue.pop
          result.each { |k, v| results[k] += v }
          value = "."
          
          if result[:failures] > 0
            value = "F"
            failures.push(test)
          end
          
          if result[:errors] > 0
            value = "E"
            errors.push(test)
          end
          
          print value
        end
        
        puts "\nFinished in #{(Time.now - t0).round.to_s} seconds."
        puts "  Failures: #{failures.join(', ')}" unless failures.empty?
        puts "  Errors:   #{errors.join(', ')}" unless errors.empty?
        puts "#{results[:tests]} tests, #{results[:assertions]} assertions, #{results[:failures]} failures, #{results[:errors]} errors"
        browser.teardown
      else
        puts "\nSkipping #{browser}, not supported on this OS"
      end
    end

    @server.shutdown
    t.join
  end
end

#mount(path, dir = nil) ⇒ Object



350
351
352
353
354
355
# File 'lib/newjs/jstest.rb', line 350

def mount(path, dir=nil)
  dir = Dir.pwd + path unless dir

  # don't cache anything in our tests
  @server.mount(path, NonCachingFileHandler, dir)
end

#run(test) ⇒ Object

test should be specified as a url or as a hash of the form => “url”, :testcases => “testFoo,testBar”



359
360
361
# File 'lib/newjs/jstest.rb', line 359

def run(test)
  @tests<<test
end