spectus 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +14 -0
  4. data/.travis.yml +6 -0
  5. data/.yardopts +1 -0
  6. data/Gemfile +2 -0
  7. data/LICENSE.md +22 -0
  8. data/README.md +239 -0
  9. data/Rakefile +18 -0
  10. data/VERSION.semver +1 -0
  11. data/lib/spectus.rb +6 -0
  12. data/lib/spectus/dsl.rb +14 -0
  13. data/lib/spectus/expectation_target.rb +27 -0
  14. data/lib/spectus/matcher.rb +35 -0
  15. data/lib/spectus/matcher/capture_stderr.rb +31 -0
  16. data/lib/spectus/matcher/capture_stdout.rb +31 -0
  17. data/lib/spectus/matcher/eql.rb +21 -0
  18. data/lib/spectus/matcher/equal.rb +21 -0
  19. data/lib/spectus/matcher/match.rb +21 -0
  20. data/lib/spectus/matcher/raise_exception.rb +27 -0
  21. data/lib/spectus/reporter.rb +45 -0
  22. data/lib/spectus/version.rb +9 -0
  23. data/spectus.gemspec +20 -0
  24. data/test/helper_test.rb +4 -0
  25. data/test/spectus/helper_test.rb +1 -0
  26. data/test/spectus/matcher/built_in/helper_test.rb +1 -0
  27. data/test/spectus/matcher/built_in/test_capture_stderr.rb +21 -0
  28. data/test/spectus/matcher/built_in/test_capture_stdout.rb +21 -0
  29. data/test/spectus/matcher/built_in/test_eql.rb +21 -0
  30. data/test/spectus/matcher/built_in/test_equal.rb +21 -0
  31. data/test/spectus/matcher/built_in/test_match.rb +21 -0
  32. data/test/spectus/matcher/built_in/test_raise_exception.rb +21 -0
  33. data/test/spectus/matcher/custom/be_prime/helper_test.rb +13 -0
  34. data/test/spectus/matcher/custom/be_prime/test_be_prime.rb +21 -0
  35. data/test/spectus/matcher/custom/be_the_answer/helper_test.rb +11 -0
  36. data/test/spectus/matcher/custom/be_the_answer/test_be_the_answer.rb +21 -0
  37. data/test/spectus/matcher/custom/helper_test.rb +1 -0
  38. data/test/spectus/matcher/custom/start_with/helper_test.rb +15 -0
  39. data/test/spectus/matcher/custom/start_with/test_start_with.rb +21 -0
  40. data/test/spectus/matcher/helper_test.rb +1 -0
  41. data/test/spectus/test_dsl.rb +9 -0
  42. data/test/spectus/test_expectation_target.rb +17 -0
  43. data/test/spectus/test_matcher.rb +34 -0
  44. data/test/spectus/test_reporter.rb +97 -0
  45. data/test/spectus/test_version.rb +11 -0
  46. data/test/support.rb +3 -0
  47. data/test/support/coverage.rb +3 -0
  48. data/test/support/env.rb +4 -0
  49. data/test/support/presenter.rb +23 -0
  50. metadata +175 -0
@@ -0,0 +1,31 @@
1
+ require 'stringio'
2
+
3
+ module Spectus
4
+ module Matcher
5
+
6
+ # @api private
7
+ # Provides the implementation for `capture_stdout`.
8
+ class CaptureStdout
9
+
10
+ # @api private
11
+ def initialize expected
12
+ @expected = expected
13
+
14
+ freeze
15
+ end
16
+
17
+ # @return [Boolean] Comparison between actual and expected values.
18
+ def matches?
19
+ begin
20
+ orig_std = $stdout
21
+ $stdout = StringIO.new
22
+
23
+ yield
24
+ $stdout.string.eql? @expected
25
+ ensure
26
+ $stdout = orig_std
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ module Spectus
2
+ module Matcher
3
+
4
+ # @api private
5
+ # Provides the implementation for `eql`.
6
+ class Eql
7
+
8
+ # @api private
9
+ def initialize expected
10
+ @expected = expected
11
+
12
+ freeze
13
+ end
14
+
15
+ # @return [Boolean] Comparison between actual and expected values.
16
+ def matches?
17
+ @expected.eql? yield
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Spectus
2
+ module Matcher
3
+
4
+ # @api private
5
+ # Provides the implementation for `equal`.
6
+ class Equal
7
+
8
+ # @api private
9
+ def initialize expected
10
+ @expected = expected
11
+
12
+ freeze
13
+ end
14
+
15
+ # @return [Boolean] Comparison between actual and expected values.
16
+ def matches?
17
+ @expected.equal? yield
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Spectus
2
+ module Matcher
3
+
4
+ # @api private
5
+ # Provides the implementation for `match`.
6
+ class Match
7
+
8
+ # @api private
9
+ def initialize expected
10
+ @expected = expected
11
+
12
+ freeze
13
+ end
14
+
15
+ # @return [Boolean] Comparison between actual and expected values.
16
+ def matches?
17
+ !@expected.match(yield).nil?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module Spectus
2
+ module Matcher
3
+
4
+ # @api private
5
+ # Provides the implementation for `raise_exception`.
6
+ class RaiseException
7
+
8
+ # @api private
9
+ def initialize expected
10
+ @expected = expected
11
+
12
+ freeze
13
+ end
14
+
15
+ # @return [Boolean] Comparison between actual and expected values.
16
+ def matches?
17
+ begin
18
+ yield
19
+ rescue @expected
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ module Spectus
2
+
3
+ # This class is responsible for reporting the result of the expectation.
4
+ class Reporter
5
+ attr_reader :negated, :matcher, :expected, :exception
6
+
7
+ # @api private
8
+ def initialize negated, matcher, expected, result
9
+ @negated = negated
10
+ @matcher = matcher
11
+ @expected = expected
12
+ @pass = result.equal? true
13
+ @exception = ([ true, false ].include?(result) ? nil : result)
14
+
15
+ freeze
16
+ end
17
+
18
+ # Returns true if the expectation is true. False otherwise.
19
+ def pass?
20
+ @pass
21
+ end
22
+
23
+ # Returns the state of the expectation.
24
+ def state
25
+ if pass?
26
+ :success
27
+ elsif @exception.nil?
28
+ :failure
29
+ else
30
+ :error
31
+ end
32
+ end
33
+
34
+ # Returns a char as the result of the expectation.
35
+ def to_s
36
+ if pass?
37
+ '.'
38
+ elsif @exception.nil?
39
+ 'F'
40
+ else
41
+ 'E'
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ require 'pathname'
2
+
3
+ module Spectus
4
+
5
+ # Gem version
6
+ VERSION = File.open(
7
+ Pathname.new(__FILE__).join '..', '..', '..', 'VERSION.semver'
8
+ ).read.chomp.to_sym
9
+ end
data/spectus.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'spectus'
3
+ spec.version = File.read('VERSION.semver')
4
+ spec.authors = ['Cyril Wack']
5
+ spec.email = ['cyril@sashite.com']
6
+ spec.summary = %q{Expectation library.}
7
+ spec.description = %q{An expectation library with some matchers for Ruby.}
8
+ spec.homepage = 'https://github.com/cyril/spectus.rb'
9
+ spec.license = 'MIT'
10
+
11
+ spec.files = `git ls-files -z`.split("\x0")
12
+ spec.test_files = spec.files.grep(%r{^test/})
13
+ spec.require_paths = ['lib']
14
+ spec.required_ruby_version = '>= 2.0.0'
15
+
16
+ spec.add_development_dependency 'bundler', '~> 1.7'
17
+ spec.add_development_dependency 'rake', '~> 10.0'
18
+ spec.add_development_dependency 'yard', '~> 0.8'
19
+ spec.add_development_dependency 'coveralls', '~> 0.7'
20
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'support'
2
+
3
+ require_relative File.join '..', 'lib', 'spectus'
4
+ extend Spectus::DSL
@@ -0,0 +1 @@
1
+ require_relative File.join '..', 'helper_test'
@@ -0,0 +1 @@
1
+ require_relative File.join '..', 'helper_test'
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'capture standard error built-in matcher' do
4
+ Spectus::Matcher::CaptureStderr.new "foo\n"
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to eql: "foo\n"
9
+ end
10
+
11
+ it 'must match the captured standard error' do
12
+ expect { subject.matches? { warn 'foo' } }.to equal: true
13
+ end
14
+
15
+ it 'must not match the captured standard error' do
16
+ expect { subject.matches? { warn 'bar' } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'capture standard output built-in matcher' do
4
+ Spectus::Matcher::CaptureStdout.new "foo\n"
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to eql: "foo\n"
9
+ end
10
+
11
+ it 'must match the captured standard output' do
12
+ expect { subject.matches? { puts 'foo' } }.to equal: true
13
+ end
14
+
15
+ it 'must not match the captured standard output' do
16
+ expect { subject.matches? { puts 'bar' } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'eql built-in matcher' do
4
+ Spectus::Matcher::Eql.new 'foo'
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to eql: 'foo'
9
+ end
10
+
11
+ it 'must be eql' do
12
+ expect { subject.matches? { 'foo' } }.to equal: true
13
+ end
14
+
15
+ it 'must not be eql' do
16
+ expect { subject.matches? { 'bar' } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'equal built-in matcher' do
4
+ Spectus::Matcher::Equal.new :foo
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to equal: :foo
9
+ end
10
+
11
+ it 'must be equal' do
12
+ expect { subject.matches? { :foo } }.to equal: true
13
+ end
14
+
15
+ it 'must not be equal' do
16
+ expect { subject.matches? { :bar } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'match built-in matcher' do
4
+ Spectus::Matcher::Match.new(/^foo/)
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to({eql: /^foo/})
9
+ end
10
+
11
+ it 'must match the string' do
12
+ expect { subject.matches? { 'foobar' } }.to equal: true
13
+ end
14
+
15
+ it 'must not match the string' do
16
+ expect { subject.matches? { 'bar' } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'raise exception built-in matcher' do
4
+ Spectus::Matcher::RaiseException.new ZeroDivisionError
5
+ end
6
+
7
+ it 'must return the expected value' do
8
+ expect { subject.instance_variable_get(:@expected) }.to equal: ZeroDivisionError
9
+ end
10
+
11
+ it 'must raise the expected exception' do
12
+ expect { subject.matches? { 0 / 0 } }.to equal: true
13
+ end
14
+
15
+ it 'must not raise any exceptions' do
16
+ expect { subject.matches? { :foo } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,13 @@
1
+ require_relative File.join '..', 'helper_test'
2
+
3
+ require 'prime'
4
+
5
+ module Spectus
6
+ module Matcher
7
+ class BePrime
8
+ def matches?
9
+ Prime.prime? yield
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'be prime custom matcher' do
4
+ Spectus::Matcher::BePrime.new
5
+ end
6
+
7
+ it 'must confirm that the instance variable is undefined' do
8
+ expect { subject.instance_variable_defined?(:@expected) }.to equal: false
9
+ end
10
+
11
+ it 'must be prime' do
12
+ expect { subject.matches? { 3 } }.to equal: true
13
+ end
14
+
15
+ it 'must not match the string' do
16
+ expect { subject.matches? { 4 } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1,11 @@
1
+ require_relative File.join '..', 'helper_test'
2
+
3
+ module Spectus
4
+ module Matcher
5
+ class BeTheAnswer
6
+ def matches?
7
+ 42.equal? yield
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'helper_test'
2
+
3
+ subject 'be the answer custom matcher' do
4
+ Spectus::Matcher::BeTheAnswer.new
5
+ end
6
+
7
+ it 'must confirm that the instance variable is undefined' do
8
+ expect { subject.instance_variable_defined?(:@expected) }.to equal: false
9
+ end
10
+
11
+ it 'must be the answer' do
12
+ expect { subject.matches? { 42 } }.to equal: true
13
+ end
14
+
15
+ it 'must not match the string' do
16
+ expect { subject.matches? { 'foo' } }.to equal: false
17
+ end
18
+
19
+ it 'must return the raised exception' do
20
+ expect { raise subject.matches? { BOOM } }.to raise_exception: NameError
21
+ end
@@ -0,0 +1 @@
1
+ require_relative File.join '..', 'helper_test'
@@ -0,0 +1,15 @@
1
+ require_relative File.join '..', 'helper_test'
2
+
3
+ module Spectus
4
+ module Matcher
5
+ class StartWith
6
+ def initialize expected
7
+ @expected = expected
8
+ end
9
+
10
+ def matches?
11
+ Regexp.new("^#{@expected}").match(yield) != nil
12
+ end
13
+ end
14
+ end
15
+ end