opal-spec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ doc
3
+ /pkg
4
+ /tmp
5
+ .yardoc
6
+ extras
7
+ /opal-test*.js
8
+
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ OpalSpec
2
+ =========
3
+
4
+ Spec framework for opal.
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,45 @@
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
@@ -0,0 +1,55 @@
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
@@ -0,0 +1,49 @@
1
+ module OpalSpec
2
+ class ExpectationNotMetError < StandardError; end
3
+
4
+ module Expectations
5
+ def should matcher = nil
6
+ if matcher
7
+ matcher.match self
8
+ else
9
+ OpalSpec::PositiveOperatorMatcher.new self
10
+ end
11
+ end
12
+
13
+ def should_not matcher = nil
14
+ if matcher
15
+ matcher.not_match self
16
+ else
17
+ OpalSpec::NegativeOperatorMatcher.new self
18
+ end
19
+ end
20
+
21
+ def be_kind_of expected
22
+ OpalSpec::BeKindOfMatcher.new expected
23
+ end
24
+
25
+ def be_nil
26
+ OpalSpec::BeNilMatcher.new nil
27
+ end
28
+
29
+ def be_true
30
+ OpalSpec::BeTrueMatcher.new true
31
+ end
32
+
33
+ def be_false
34
+ OpalSpec::BeFalseMatcher.new false
35
+ end
36
+
37
+ def equal expected
38
+ OpalSpec::EqualMatcher.new expected
39
+ end
40
+
41
+ def raise_error expected
42
+ OpalSpec::RaiseErrorMatcher.new expected, &@actual
43
+ end
44
+ end
45
+ end
46
+
47
+ class Object
48
+ include OpalSpec::Expectations
49
+ end
@@ -0,0 +1,58 @@
1
+ module OpalSpec
2
+ class Formatter
3
+ def initialize
4
+ @examples = []
5
+ @failed_examples = []
6
+ end
7
+
8
+ def start
9
+ end
10
+
11
+ def finish
12
+ end
13
+
14
+ def example_group_started group
15
+ @example_group = group
16
+ end
17
+
18
+ def example_group_finished group
19
+ end
20
+
21
+ def example_started example
22
+ @examples << example
23
+ end
24
+
25
+ def example_passed example
26
+ end
27
+
28
+ def example_failed example
29
+ @failed_examples << example
30
+ end
31
+
32
+ def example_count
33
+ @examples.size
34
+ end
35
+ end
36
+
37
+ class ConsoleFormatter < Formatter
38
+ def finish
39
+ @failed_examples.each_with_index do |example, i|
40
+ exception = example.exception
41
+ description = example.description
42
+ group = example.example_group
43
+
44
+ case exception
45
+ when OpalSpec::ExpectationNotMetError
46
+ puts "\n#{i + 1}) Failure:\n#{group.description} #{description}:"
47
+ puts "#{exception.message}\n"
48
+ else
49
+ puts "\n#{i + 1}) Error:\n#{group.description} #{description}:"
50
+ puts "#{exception.class}: #{exception.message}\n"
51
+ puts " #{exception.backtrace.join "\n "}\n"
52
+ end
53
+ end
54
+
55
+ puts "\n#{example_count} specifications, #{@failed_examples.size} failures"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ module Kernel
2
+ def describe desc, &block
3
+ OpalSpec::ExampleGroup.create desc, block
4
+ end
5
+
6
+ def mock obj
7
+ Object.new
8
+ end
9
+ end
@@ -0,0 +1,91 @@
1
+ module OpalSpec
2
+ class Matcher
3
+ def initialize actual
4
+ @actual = actual
5
+ end
6
+
7
+ def failure message
8
+ raise OpalSpec::ExpectationNotMetError, message
9
+ end
10
+ end
11
+
12
+ class PositiveOperatorMatcher < Matcher
13
+ def == expected
14
+ if @actual == expected
15
+ true
16
+ else
17
+ failure "expected: #{expected.inspect}, got: #{@actual.inspect} (using ==)."
18
+ end
19
+ end
20
+ end
21
+
22
+ class NegativeOperatorMatcher < Matcher
23
+ def == expected
24
+ if @actual == expected
25
+ failure "expected: #{expected.inspect} not to be #{@actual.inspect} (using ==)."
26
+ end
27
+ end
28
+ end
29
+
30
+ class BeKindOfMatcher < Matcher
31
+ def match expected
32
+ unless expected.kind_of? @actual
33
+ failure "expected #{expected.inspect} to be a kind of #{@actual}, not #{expected.class}."
34
+ end
35
+ end
36
+ end
37
+
38
+ class BeNilMatcher < Matcher
39
+ def match expected
40
+ unless expected.nil?
41
+ failure "expected #{expected.inspect} to be nil."
42
+ end
43
+ end
44
+ end
45
+
46
+ class BeTrueMatcher < Matcher
47
+ def match expected
48
+ unless expected == true
49
+ failure "expected #{expected.inspect} to be true."
50
+ end
51
+ end
52
+ end
53
+
54
+ class BeFalseMatcher < Matcher
55
+ def match expected
56
+ unless expected == false
57
+ failure "expected #{expected.inspect} to be false."
58
+ end
59
+ end
60
+ end
61
+
62
+ class EqualMatcher < Matcher
63
+ def match expected
64
+ unless expected.equal? @actual
65
+ failure "expected #{@actual.inspect} to be the same as #{expected.inspect}."
66
+ end
67
+ end
68
+
69
+ def not_match expected
70
+ if expected.equal? @actual
71
+ failure "expected #{@actual.inspect} not to be equal to #{expected.inspect}."
72
+ end
73
+ end
74
+ end
75
+
76
+ class RaiseErrorMatcher < Matcher
77
+ def match expected
78
+ should_raise = false
79
+ begin
80
+ yield
81
+ should_raise = true
82
+ rescue => e
83
+
84
+ end
85
+
86
+ if should_raise
87
+ failure "expected #{expected} to be raised, but nothing was."
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,38 @@
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 }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module ScratchPad
2
+ def self.clear
3
+ @record = nil
4
+ end
5
+
6
+ def self.record(arg)
7
+ @record = arg
8
+ end
9
+
10
+ def self.<<(arg)
11
+ @record << arg
12
+ end
13
+
14
+ def self.recorded
15
+ @record
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module OpalSpec
2
+ VERSION = "0.0.1"
3
+ end
data/lib/opal/spec.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'opal/spec/kernel'
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'
data/opal-spec.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'opal/spec/version'
3
+
4
+ Gem::Specification.new do |s|
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'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ['lib']
15
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Beynon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-21 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Opal spec lib
15
+ email:
16
+ - adam@adambeynon.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - README.md
23
+ - 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
31
+ - lib/opal/spec/runner.rb
32
+ - lib/opal/spec/scratch_pad.rb
33
+ - lib/opal/spec/version.rb
34
+ - opal-spec.gemspec
35
+ homepage: http://opalscript.org
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.11
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Opal spec lib
59
+ test_files: []