opal-spec 0.0.1 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ module Opal
2
+ module Spec
3
+ class PhantomFormatter
4
+ def initialize
5
+ @examples = []
6
+ @failed_examples = []
7
+ end
8
+
9
+ def log_green(str)
10
+ `console.log('\\033[32m' + str + '\\033[0m')`
11
+ end
12
+
13
+ def log_red(str)
14
+ `console.log('\\033[31m' + str + '\\033[0m')`
15
+ end
16
+
17
+ def log(str)
18
+ `console.log(str)`
19
+ end
20
+
21
+ def start
22
+ @start_time = Time.now.to_f
23
+ end
24
+
25
+ def finish
26
+ time = Time.now.to_f - @start_time
27
+ if @failed_examples.empty?
28
+ log "\nFinished"
29
+ log_green "#{example_count} examples, 0 failures (time taken: #{time})"
30
+ finish_with_code(0)
31
+ else
32
+ log "\nFailures:"
33
+ @failed_examples.each_with_index do |example, idx|
34
+ log "\n #{idx+1}. #{example.example_group.description} #{example.description}"
35
+
36
+ exception = example.exception
37
+ case exception
38
+ when Opal::Spec::ExpectationNotMetError
39
+ output = exception.message
40
+ else
41
+ output = "#{exception.class.name}: #{exception.message}\n"
42
+ output += " #{exception.backtrace.join "\n "}\n"
43
+ end
44
+ log_red " #{output}"
45
+ end
46
+
47
+ log "\nFinished"
48
+ log_red "#{example_count} examples, #{@failed_examples.size} failures (time taken: #{time})"
49
+ finish_with_code(1)
50
+ end
51
+ end
52
+
53
+ def finish_with_code(code)
54
+ %x{
55
+ if (typeof(phantom) !== 'undefined') {
56
+ return phantom.exit(code);
57
+ }
58
+ else {
59
+ window.OPAL_SPEC_CODE = code;
60
+ }
61
+ }
62
+ end
63
+
64
+ def example_group_started group
65
+ @example_group = group
66
+ @example_group_failed = false
67
+ log "\n#{group.description}"
68
+ end
69
+
70
+ def example_group_finished group
71
+ end
72
+
73
+ def example_started example
74
+ @examples << example
75
+ @example = example
76
+ end
77
+
78
+ def example_failed example
79
+ @failed_examples << example
80
+ @example_group_failed = true
81
+ log_red " #{example.description}"
82
+ end
83
+
84
+ def example_passed example
85
+ log_green " #{example.description}"
86
+ end
87
+
88
+ def example_count
89
+ @examples.size
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,80 @@
1
+ module Opal
2
+ module Spec
3
+ class Runner
4
+ def self.in_browser?
5
+ %x{
6
+ if (typeof(window) !== 'undefined' && typeof(document) !== 'undefined') {
7
+ return true;
8
+ }
9
+
10
+ return false;
11
+ }
12
+ end
13
+
14
+ def self.in_phantom?
15
+ %x{
16
+ if (typeof(phantom) !== 'undefined' || typeof(OPAL_SPEC_PHANTOM) !== 'undefined') {
17
+ return true;
18
+ }
19
+
20
+ return false;
21
+ }
22
+ end
23
+
24
+ def self.autorun
25
+ if in_browser?
26
+ %x{
27
+ setTimeout(function() {
28
+ #{ Runner.new.run };
29
+ }, 0);
30
+ }
31
+ else
32
+ Runner.new.run
33
+ end
34
+ end
35
+
36
+ def initialize
37
+ if Runner.in_phantom?
38
+ @formatter = PhantomFormatter.new
39
+ elsif Runner.in_browser?
40
+ @formatter = BrowserFormatter.new
41
+ end
42
+ end
43
+
44
+ def run
45
+ @groups = ExampleGroup.example_groups.dup
46
+ @formatter.start
47
+ run_next_group
48
+ end
49
+
50
+ def run_next_group
51
+ if @groups.empty?
52
+ @formatter.finish
53
+ else
54
+ @groups.shift.run self
55
+ end
56
+ end
57
+
58
+ def example_group_started group
59
+ @formatter.example_group_started group
60
+ end
61
+
62
+ def example_group_finished group
63
+ @formatter.example_group_finished group
64
+ run_next_group
65
+ end
66
+
67
+ def example_started example
68
+ @formatter.example_started example
69
+ end
70
+
71
+ def example_passed example
72
+ @formatter.example_passed example
73
+ end
74
+
75
+ def example_failed example
76
+ @formatter.example_failed example
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,13 @@
1
+ require 'opal'
2
+ require 'opal/spec/example'
3
+ require 'opal/spec/example_group'
4
+ require 'opal/spec/matchers'
5
+ require 'opal/spec/runner'
6
+ require 'opal/spec/scratch_pad'
7
+ require 'opal/spec/expectations'
8
+ require 'opal/spec/browser_formatter'
9
+ require 'opal/spec/phantom_formatter'
10
+ require 'opal/spec/kernel'
11
+
12
+ # Compatibility with older versions
13
+ OpalSpec = Opal::Spec
@@ -0,0 +1 @@
1
+ require 'opal/spec'
@@ -0,0 +1,52 @@
1
+ require 'opal/spec'
2
+
3
+ module Opal
4
+ module Spec
5
+ class RakeTask
6
+ include Rake::DSL if defined? Rake::DSL
7
+
8
+ attr_accessor :name
9
+ attr_writer :runner_path, :url_path, :port
10
+
11
+ def initialize(name = 'opal:spec')
12
+ @name = name
13
+ yield self if block_given?
14
+
15
+ define_tasks
16
+ end
17
+
18
+ def define_tasks
19
+ desc "Run opal specs in phantomjs"
20
+ task @name do
21
+ require 'rack'
22
+ require 'webrick'
23
+
24
+ server = fork do
25
+ Rack::Server.start(:config => 'config.ru', :Port => port,
26
+ :Logger => WEBrick::Log.new("/dev/null"), :AccessLog => [])
27
+ end
28
+
29
+ system "phantomjs #{runner_path} \"#{url_path}\""
30
+ success = $?.success?
31
+
32
+ Process.kill(:SIGINT, server)
33
+ Process.wait
34
+
35
+ exit 1 unless success
36
+ end
37
+ end
38
+
39
+ def runner_path
40
+ @runner_path || File.join(File.dirname(__FILE__), '..', '..', '..', 'vendor', 'runner.js')
41
+ end
42
+
43
+ def url_path
44
+ @url_path || "http://localhost:9999/"
45
+ end
46
+
47
+ def port
48
+ @port || 9999
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,38 +1,12 @@
1
- module OpalSpec
2
- class Runner
3
- def self.autorun
4
- at_exit { OpalSpec::Runner.new.run }
5
- end
6
-
7
- def initialize
8
- @formatters = [ConsoleFormatter.new]
9
- end
10
-
11
- def run
12
- groups = ExampleGroup.example_groups
13
- @formatters.each { |f| f.start }
14
- groups.each { |group| group.run self }
15
- @formatters.each { |f| f.finish }
16
- end
17
-
18
- def example_group_started group
19
- @formatters.each { |f| f.example_group_started group }
20
- end
21
-
22
- def example_group_finished group
23
- @formatters.each { |f| f.example_group_finished group }
24
- end
25
-
26
- def example_started example
27
- @formatters.each { |f| f.example_started example }
28
- end
29
-
30
- def example_passed example
31
- @formatters.each { |f| f.example_passed example }
32
- end
33
-
34
- def example_failed example
35
- @formatters.each { |f| f.example_failed example }
1
+ module Opal
2
+ module Spec
3
+
4
+ # Run tests on the command line using phantomjs. Phantomjs **must** be
5
+ # installed, and the runner assumes you have an HTML file at
6
+ # ./spec/index.html which will be run.
7
+ def self.runner
8
+ runner = File.join File.dirname(__FILE__), '..', '..', '..', 'vendor', 'runner.js'
9
+ system "phantomjs #{runner} spec/index.html"
36
10
  end
37
11
  end
38
12
  end
@@ -1,3 +1,5 @@
1
- module OpalSpec
2
- VERSION = "0.0.1"
1
+ module Opal
2
+ module Spec
3
+ VERSION = '0.2.8'
4
+ end
3
5
  end
data/lib/opal/spec.rb CHANGED
@@ -1,8 +1,9 @@
1
- require 'opal/spec/kernel'
1
+ require 'opal'
2
2
  require 'opal/spec/runner'
3
- require 'opal/spec/example_group'
4
- require 'opal/spec/example'
5
- require 'opal/spec/expectations'
6
- require 'opal/spec/matchers'
7
- require 'opal/spec/formatter'
8
- require 'opal/spec/scratch_pad'
3
+ require 'opal/spec/version'
4
+
5
+ # Compatibility with old namespace
6
+ OpalSpec = Opal::Spec
7
+
8
+ # Just register our opal code path with opal build tools
9
+ Opal.append_path File.join(File.dirname(__FILE__), '..', 'assets', 'javascripts')
data/lib/opal-spec.rb ADDED
@@ -0,0 +1 @@
1
+ require 'opal/spec'
data/opal-spec.gemspec CHANGED
@@ -1,15 +1,15 @@
1
- $:.push File.expand_path('../lib', __FILE__)
2
- require 'opal/spec/version'
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/opal/spec/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'opal-spec'
6
- s.version = OpalSpec::VERSION
7
- s.authors = ['Adam Beynon']
8
- s.email = ['adam@adambeynon.com']
9
- s.homepage = 'http://opalscript.org'
10
- s.summary = 'Opal spec lib'
11
- s.description = 'Opal spec lib'
6
+ s.version = Opal::Spec::VERSION
7
+ s.author = 'Adam Beynon'
8
+ s.email = 'adam.beynon@gmail.com'
9
+ s.homepage = 'http://opalrb.org'
10
+ s.summary = 'Opal compatible spec'
11
+ s.description = 'Opal compatible spec library'
12
12
 
13
- s.files = `git ls-files`.split("\n")
14
- s.require_paths = ['lib']
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ['lib']
15
15
  end
data/spec/index.html ADDED
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>opal-spec specs</title>
5
+ </head>
6
+ <body>
7
+ <script src="../build/specs.js"></script>
8
+ </body>
9
+ </html>
data/spec/specs.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'opal'
2
+ require 'opal-spec'
3
+
4
+ @passed = 0
5
+ @failures = []
6
+
7
+ def assert(value, message='Not true')
8
+ if value
9
+ @passed += 1
10
+ else
11
+ @failures << message
12
+ end
13
+ end
14
+
15
+ def assert_equal(got, expected, message='Not equal')
16
+ assert(got == expected, message)
17
+ end
18
+
19
+ describe 'Normal group' do
20
+ it 'this should fail' do
21
+ 1.should == 2
22
+ end
23
+ end
24
+
25
+ describe "New eql" do
26
+ it "these should both pass" do
27
+ 1.should eq(1)
28
+ 1.should_not eq(2)
29
+ end
30
+
31
+ it "and this should fail" do
32
+ 1.should eq(:adam)
33
+ end
34
+ end
35
+
36
+ describe Object do
37
+ it "should output a nice name for classes" do
38
+ 1.should eq(1)
39
+ end
40
+ end
41
+
42
+ describe 'Another group' do
43
+ it 'this should pass' do
44
+ 1.should == 1
45
+ end
46
+
47
+ it 'this should fail' do
48
+ raise "whatever error you like"
49
+ end
50
+
51
+ it 'this should pass' do
52
+ true.should be_true
53
+ false.should be_false
54
+ nil.should be_nil
55
+ end
56
+
57
+ async 'this should pass (in 1 second time)' do
58
+ set_timeout(1000) do
59
+ run_async {
60
+ 1.should == 1
61
+ }
62
+ end
63
+ end
64
+
65
+ async 'this should fail (in 1 second time)' do
66
+ set_timeout(1000) do
67
+ run_async {
68
+ 1.should == 5
69
+ }
70
+ end
71
+ end
72
+ end
73
+
74
+ Opal::Spec::Runner.autorun
75
+
76
+ puts "Assertions: #{@passed + @failures.length}, Passed: #{@passed}, Failures: #{@failures.length}"
data/vendor/runner.js ADDED
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Test runner for phantomjs
3
+ */
4
+ var args = phantom.args;
5
+ var page = require('webpage').create();
6
+
7
+ page.onConsoleMessage = function(msg) {
8
+ console.log(msg);
9
+ };
10
+
11
+ page.onInitialized = function() {
12
+ page.evaluate(function () {
13
+ window.OPAL_SPEC_PHANTOM = true;
14
+ });
15
+ };
16
+
17
+ page.open(args[0], function(status) {
18
+ if (status !== 'success') {
19
+ console.error("Cannot load: " + args[0]);
20
+ phantom.exit(1);
21
+ } else {
22
+ var timeout = parseInt(args[1] || 60000, 10);
23
+ var start = Date.now();
24
+ var interval = setInterval(function() {
25
+ if (Date.now() > start + timeout) {
26
+ console.error("Specs timed out");
27
+ phantom.exit(124);
28
+ } else {
29
+ var code = page.evaluate(function() {
30
+ return window.OPAL_SPEC_CODE;
31
+ });
32
+
33
+ if (code === 0 || code === 1) {
34
+ clearInterval(interval);
35
+ phantom.exit(code);
36
+ }
37
+ }
38
+ }, 500);
39
+ }
40
+ });
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,30 +9,39 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-21 00:00:00.000000000Z
12
+ date: 2013-02-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Opal spec lib
15
- email:
16
- - adam@adambeynon.com
14
+ description: Opal compatible spec library
15
+ email: adam.beynon@gmail.com
17
16
  executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
20
  - .gitignore
21
+ - Gemfile
22
22
  - README.md
23
+ - Rakefile
24
+ - lib/assets/javascripts/opal-spec.rb
25
+ - lib/assets/javascripts/opal/spec.rb
26
+ - lib/assets/javascripts/opal/spec/browser_formatter.rb
27
+ - lib/assets/javascripts/opal/spec/example.rb
28
+ - lib/assets/javascripts/opal/spec/example_group.rb
29
+ - lib/assets/javascripts/opal/spec/expectations.rb
30
+ - lib/assets/javascripts/opal/spec/kernel.rb
31
+ - lib/assets/javascripts/opal/spec/matchers.rb
32
+ - lib/assets/javascripts/opal/spec/phantom_formatter.rb
33
+ - lib/assets/javascripts/opal/spec/runner.rb
34
+ - lib/assets/javascripts/opal/spec/scratch_pad.rb
35
+ - lib/opal-spec.rb
23
36
  - lib/opal/spec.rb
24
- - lib/opal/spec/autorun.rb
25
- - lib/opal/spec/example.rb
26
- - lib/opal/spec/example_group.rb
27
- - lib/opal/spec/expectations.rb
28
- - lib/opal/spec/formatter.rb
29
- - lib/opal/spec/kernel.rb
30
- - lib/opal/spec/matchers.rb
37
+ - lib/opal/spec/rake_task.rb
31
38
  - lib/opal/spec/runner.rb
32
- - lib/opal/spec/scratch_pad.rb
33
39
  - lib/opal/spec/version.rb
34
40
  - opal-spec.gemspec
35
- homepage: http://opalscript.org
41
+ - spec/index.html
42
+ - spec/specs.rb
43
+ - vendor/runner.js
44
+ homepage: http://opalrb.org
36
45
  licenses: []
37
46
  post_install_message:
38
47
  rdoc_options: []
@@ -52,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
61
  version: '0'
53
62
  requirements: []
54
63
  rubyforge_project:
55
- rubygems_version: 1.8.11
64
+ rubygems_version: 1.8.23
56
65
  signing_key:
57
66
  specification_version: 3
58
- summary: Opal spec lib
67
+ summary: Opal compatible spec
59
68
  test_files: []
@@ -1,30 +0,0 @@
1
- require 'opal/spec'
2
-
3
- ##
4
- # If running in the browser then capture test output straight to the
5
- # page.
6
-
7
- if RUBY_ENGINE =~ /opal-browser/
8
- raise "Body not loaded. Add tests into <body> element" unless `document.body`
9
-
10
- def $stdout.puts(*a)
11
- a.each do |str|
12
- `var elem = document.createElement('pre');
13
- elem.textContent == null ? elem.innerText = str : elem.textContent = str;
14
- elem.style.margin = "0px";
15
- document.body.appendChild(elem);`
16
- end
17
- end # $stdout
18
- end
19
-
20
- ##
21
- # If autorun is the main file (entry point), then automatically load all
22
- # specs from spec/
23
-
24
- if $0 == __FILE__
25
- Dir['spec/**/*.rb'].each do |spec|
26
- require spec
27
- end
28
- end
29
-
30
- OpalSpec::Runner.autorun
@@ -1,45 +0,0 @@
1
- module OpalSpec
2
- class Example
3
- attr_reader :description, :example_group, :exception
4
-
5
- def initialize(group, desc, block)
6
- @example_group = group
7
- @description = desc
8
- @__block__ = block
9
- end
10
-
11
- def run_before_hooks
12
- @example_group.before_hooks.each do |before|
13
- instance_eval &before
14
- end
15
- end
16
-
17
- def run_after_hooks
18
- @example_group.after_hooks.each do |after|
19
- instance_eval &after
20
- end
21
- end
22
-
23
- def run runner
24
- begin
25
- runner.example_started self
26
- run_before_hooks
27
- instance_eval &@__block__
28
- rescue => e
29
- @exception = e
30
- ensure
31
- begin
32
- run_after_hooks
33
- rescue => e
34
- @exception = e
35
- end
36
- end
37
-
38
- if @exception
39
- runner.example_failed self
40
- else
41
- runner.example_passed self
42
- end
43
- end
44
- end
45
- end
@@ -1,55 +0,0 @@
1
- module OpalSpec
2
- class ExampleGroup
3
- @example_groups = []
4
- def self.example_groups
5
- @example_groups
6
- end
7
-
8
- def self.create desc, block
9
- @example_groups << self.new(desc, block)
10
- end
11
-
12
- def initialize desc, block
13
- @desc = desc
14
- @examples = []
15
- @before_hooks = []
16
- @after_hooks = []
17
- instance_eval &block
18
- end
19
-
20
- def it desc, &block
21
- @examples << Example.new(self, desc, block)
22
- end
23
-
24
- def it_behaves_like(*objs)
25
- end
26
-
27
- def before type = :each, &block
28
- raise "unsupported before type: #{type}" unless type == :each
29
- @before_hooks << block
30
- end
31
-
32
- def after type = :each, &block
33
- raise "unsupported after type: #{type}" unless type == :each
34
- @after_hooks << block
35
- end
36
-
37
- def before_hooks
38
- @before_hooks
39
- end
40
-
41
- def after_hooks
42
- @after_hooks
43
- end
44
-
45
- def run runner
46
- runner.example_group_started self
47
- @examples.each { |example| example.run runner }
48
- runner.example_group_finished self
49
- end
50
-
51
- def description
52
- @desc
53
- end
54
- end
55
- end