expectations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
@@ -0,0 +1,20 @@
1
+ class Expectations::Expectation
2
+ attr_accessor :expected, :block, :file, :line, :actual
3
+ def initialize(expected, &block)
4
+ self.expected, self.block = expected, block
5
+ self.file, self.line = eval "[__FILE__, __LINE__]", block.binding
6
+ end
7
+
8
+ def execute
9
+ begin
10
+ self.actual = block.call
11
+ return self.extend(Expectations::Results::Fulfilled) if expected == actual
12
+ rescue Exception => ex
13
+ return self.extend(Expectations::Results::Fulfilled) if expected == ex.class
14
+ self.extend(Expectations::Results::Error)
15
+ self.exception, self.actual = ex, ex.class
16
+ return self
17
+ end
18
+ self.extend(Expectations::Results::Failure)
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module Expectations::Results
2
+ def initialize(file, line)
3
+ self.line, self.file = line, file
4
+ end
5
+
6
+ def fulfilled?
7
+ self.is_a?(Expectations::Results::Fulfilled)
8
+ end
9
+
10
+ def self.included(klass)
11
+ klass.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def char(arg)
16
+ define_method :char do
17
+ arg
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ module Expectations::Results
24
+ module Failure
25
+ include Expectations::Results
26
+ char "F"
27
+ end
28
+ end
29
+
30
+ module Expectations::Results
31
+ module Fulfilled
32
+ include Expectations::Results
33
+ char "."
34
+ end
35
+ end
36
+
37
+ module Expectations::Results
38
+ module Error
39
+ attr_accessor :exception
40
+ include Expectations::Results
41
+ char "E"
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ class Expectations::Suite
2
+
3
+ def execute(out=STDOUT)
4
+ suite_result = Expectations::SuiteResults.new(out)
5
+ benchmark = Benchmark.measure do
6
+ expectations.each do |expectation|
7
+ suite_result << expectation.execute
8
+ end
9
+ end
10
+ suite_result.print_results(benchmark)
11
+ suite_result.result
12
+ end
13
+
14
+ def expectations
15
+ @expectations ||= []
16
+ end
17
+
18
+ def expect(expected, &block)
19
+ self.expectations << Expectations::Expectation.new(expected, &block)
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ class Expectations::SuiteResults
2
+ attr_accessor :result, :out, :expectations
3
+
4
+ def initialize(out)
5
+ self.out, self.result, self.expectations = out, true, []
6
+ out.print "Expectations "
7
+ end
8
+
9
+ def <<(expectation_result)
10
+ out.print expectation_result.char
11
+ self.expectations << expectation_result
12
+ self.result &&= expectation_result.fulfilled?
13
+ self
14
+ end
15
+
16
+ def fulfilled
17
+ expectations.select { |expectation| expectation.is_a?(Expectations::Results::Fulfilled) }
18
+ end
19
+
20
+ def errors
21
+ expectations.select { |expectation| expectation.is_a?(Expectations::Results::Error) }
22
+ end
23
+
24
+ def failures
25
+ expectations.select { |expectation| expectation.is_a?(Expectations::Results::Failure) }
26
+ end
27
+
28
+ def print_results(benchmark)
29
+ benchmark_seconds = benchmark.real.to_s.split(".").first
30
+ benchmark_subseconds = benchmark.real.to_s.split(".").last[0..5]
31
+ out.puts "\nFinished in #{benchmark_seconds}.#{benchmark_subseconds} seconds"
32
+ if result
33
+ out.puts "\nSuccess: #{fulfilled.size} fulfilled"
34
+ else
35
+ out.puts "\nFailure: #{failures.size} failed, #{errors.size} errors, #{fulfilled.size} fulfilled"
36
+ out.puts "\nErrors:" if errors.any?
37
+ errors.each do |error|
38
+ out.puts " #{error.file}:#{error.line}:in `expect'"
39
+ out.puts " line <#{error.line}>"
40
+ out.puts " error <#{error.exception.message}>"
41
+ out.puts " expected <#{error.expected.inspect}> got <#{error.actual.inspect}>"
42
+ end
43
+ out.puts "\nFailures:" if failures.any?
44
+ failures.each do |failure|
45
+ out.puts " #{failure.file}:#{failure.line}:in `expect'"
46
+ out.puts " line <#{failure.line}>"
47
+ out.puts " expected: <#{failure.expected.inspect}> got: <#{failure.actual.inspect}>"
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ class Expectations::SuiteRunner
2
+ include Singleton
3
+ attr_accessor :suite
4
+
5
+ def initialize
6
+ self.suite = Expectations::Suite.new
7
+ at_exit do
8
+ exit 1 unless suite.execute
9
+ end
10
+ end
11
+
12
+ def suite_eval(&block)
13
+ self.suite.instance_eval &block
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module Expectations
2
+ end
3
+
4
+ def Expectations(&block)
5
+ Expectations::SuiteRunner.instance.suite_eval &block
6
+ end
7
+
8
+ require 'singleton'
9
+ require 'benchmark'
10
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/suite')
11
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_runner')
12
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_results')
13
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/expectation')
14
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/results')
data/rakefile.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ task :default do
7
+ require File.dirname(__FILE__) + '/test/all_tests.rb'
8
+ end
9
+
10
+ desc 'Generate RDoc'
11
+ Rake::RDocTask.new do |task|
12
+ task.main = 'README'
13
+ task.title = 'expectations'
14
+ task.rdoc_dir = 'doc'
15
+ task.options << "--line-numbers" << "--inline-source"
16
+ task.rdoc_files.include('README', 'lib/**/*.rb')
17
+ end
18
+
19
+ desc "Upload RDoc to RubyForge"
20
+ task :publish_rdoc => [:rdoc] do
21
+ Rake::SshDirPublisher.new("jaycfields@rubyforge.org", "/var/www/gforge-projects/expectations", "doc").upload
22
+ end
23
+
24
+ Gem::manage_gems
25
+
26
+ specification = Gem::Specification.new do |s|
27
+ s.name = "expectations"
28
+ s.summary = "A lightweight unit testing framework. Tests (expectations) will be written as follows
29
+ expect 2 do
30
+ 1 + 1
31
+ end
32
+
33
+ expect NoMethodError do
34
+ Object.invalid_method_call
35
+ end."
36
+ s.version = "0.0.1"
37
+ s.author = 'Jay Fields'
38
+ s.description = "A lightweight unit testing framework. Tests (expectations) will be written as follows
39
+ expect 2 do
40
+ 1 + 1
41
+ end
42
+
43
+ expect NoMethodError do
44
+ Object.invalid_method_call
45
+ end."
46
+ s.homepage = 'http://expectations.rubyforge.org'
47
+ s.rubyforge_project = 'expectations'
48
+
49
+ s.has_rdoc = true
50
+ s.extra_rdoc_files = ['README']
51
+ s.rdoc_options << '--title' << 'expectations' << '--main' << 'README' << '--line-numbers'
52
+
53
+ s.autorequire = 'arbs'
54
+ s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'rakefile.rb'].to_a
55
+ s.test_file = "test/all_tests.rb"
56
+ end
57
+
58
+ Rake::GemPackageTask.new(specification) do |package|
59
+ package.need_zip = false
60
+ package.need_tar = false
61
+ end
data/test/all_tests.rb ADDED
@@ -0,0 +1 @@
1
+ Dir['**/*_test.rb'].each { |test_case| require test_case }
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ Expectations do
4
+ expect true do
5
+ Expectations::Expectation.new(1) { 2 }.execute.is_a?(Expectations::Results::Failure)
6
+ end
7
+
8
+ expect true do
9
+ Expectations::Expectation.new(1) { 1 }.execute.is_a?(Expectations::Results::Fulfilled)
10
+ end
11
+
12
+ expect true do
13
+ Expectations::Expectation.new(1) { raise }.execute.is_a?(Expectations::Results::Error)
14
+ end
15
+
16
+ expect __LINE__ + 1 do
17
+ Expectations::Expectation.new(1) { 2 }.execute.line
18
+ end
19
+
20
+ expect __LINE__ + 1 do
21
+ Expectations::Expectation.new(1) { raise }.execute.line
22
+ end
23
+
24
+ expect __FILE__ do
25
+ Expectations::Expectation.new(1) { 2 }.execute.file
26
+ end
27
+
28
+ expect __FILE__ do
29
+ Expectations::Expectation.new(1) { raise }.execute.file
30
+ end
31
+
32
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ Expectations do
4
+
5
+ expect "." do
6
+ Object.new.extend(Expectations::Results::Fulfilled).char
7
+ end
8
+
9
+ expect "F" do
10
+ Object.new.extend(Expectations::Results::Failure).char
11
+ end
12
+
13
+ expect "E" do
14
+ Object.new.extend(Expectations::Results::Error).char
15
+ end
16
+
17
+ expect true do
18
+ Object.new.extend(Expectations::Results::Fulfilled).fulfilled?
19
+ end
20
+
21
+ expect false do
22
+ Object.new.extend(Expectations::Results::Failure).fulfilled?
23
+ end
24
+
25
+ expect false do
26
+ Object.new.extend(Expectations::Results::Error).fulfilled?
27
+ end
28
+
29
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ Expectations do
4
+ expect 1 do
5
+ results = Expectations::SuiteResults.new(Silent)
6
+ results << Object.new.extend(Expectations::Results::Fulfilled)
7
+ results.fulfilled.size
8
+ end
9
+
10
+ expect 0 do
11
+ results = Expectations::SuiteResults.new(Silent)
12
+ results << Object.new.extend(Expectations::Results::Fulfilled)
13
+ results.errors.size
14
+ end
15
+
16
+ expect true do
17
+ results = Expectations::SuiteResults.new(Silent)
18
+ results << Object.new.extend(Expectations::Results::Fulfilled)
19
+ results.result
20
+ end
21
+
22
+ expect false do
23
+ results = Expectations::SuiteResults.new(Silent)
24
+ results << Object.new.extend(Expectations::Results::Failure)
25
+ results.result
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ Expectations do
4
+ expect 1 do
5
+ suite = Expectations::Suite.new
6
+ suite.expect(1) { 1 }
7
+ suite.expectations.size
8
+ end
9
+
10
+ expect true do
11
+ suite = Expectations::Suite.new
12
+ suite.execute(Silent)
13
+ end
14
+
15
+ expect false do
16
+ suite = Expectations::Suite.new
17
+ suite.expect(1) { 2 }
18
+ suite.execute(Silent)
19
+ end
20
+
21
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ Expectations do
4
+ expect 2 do
5
+ 1 + 1
6
+ end
7
+
8
+ expect NoMethodError do
9
+ Object.no_method
10
+ end
11
+
12
+ end
data/test/silent.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Silent
2
+ def self.print(arg)
3
+ end
4
+
5
+ def self.puts(arg=nil)
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/silent'
2
+ require File.dirname(__FILE__) + '/../lib/expectations'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expectations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jay Fields
8
+ autorequire: arbs
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2007-12-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A lightweight unit testing framework. Tests (expectations) will be written as follows expect 2 do 1 + 1 end expect NoMethodError do Object.invalid_method_call end.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/expectations/expectation.rb
26
+ - lib/expectations/results.rb
27
+ - lib/expectations/suite.rb
28
+ - lib/expectations/suite_results.rb
29
+ - lib/expectations/suite_runner.rb
30
+ - lib/expectations.rb
31
+ - test/all_tests.rb
32
+ - test/expectations/expectation_test.rb
33
+ - test/expectations/results_test.rb
34
+ - test/expectations/suite_results_test.rb
35
+ - test/expectations/suite_test.rb
36
+ - test/sample_expecations_test.rb
37
+ - test/silent.rb
38
+ - test/test_helper.rb
39
+ - rakefile.rb
40
+ - README
41
+ has_rdoc: true
42
+ homepage: http://expectations.rubyforge.org
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --title
46
+ - expectations
47
+ - --main
48
+ - README
49
+ - --line-numbers
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: expectations
67
+ rubygems_version: 1.0.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: A lightweight unit testing framework. Tests (expectations) will be written as follows expect 2 do 1 + 1 end expect NoMethodError do Object.invalid_method_call end.
71
+ test_files:
72
+ - test/all_tests.rb