easytest 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e042e91fa6e5c9b2c64188574118fbe3df09705081e9ef999cdf78ce50ba6633
4
- data.tar.gz: 6e556cfb345aba74ec5eef4883b9f2bb33863c041223e8dfe3337f16dc2b3501
3
+ metadata.gz: 42389cdde1ebd51de0f636f3c5ec0e67ffe842ff91029f71617795799841d47b
4
+ data.tar.gz: 559dadc55a86bfc6da11234b05f1fd9a5477311379f94ea41565731516271405
5
5
  SHA512:
6
- metadata.gz: a064dadb35c5af370821737bca2e59bcbfea6e3b4ecb6179779fe34f0e17998d3778fc4eb978f997b7a876be9a95b13e13798c8a378eb9993e58f804bca2fb17
7
- data.tar.gz: fef643dcd0e347880cb202653575c832edf431f6004216f0c054d5cc7246aade6427638d6b7f434b9c327c15255a55bb9f1927ead6e57c272418738b080540e1
6
+ metadata.gz: e4d9cc9b6bc5a76ecccf5f98d076e123eeddabe7a16c42a093050e154c3c32565a80c99301ed615ab0be95e2b7b7e0773088d3d5dc621ddc2fda3f69e28ed184
7
+ data.tar.gz: 8a8612f4baf5bc8bc026ecb20fbbe906ad32044f22c9232137dc68b5bb32940d634aed10703d6455f339518bba18c8a433bee66d2ed30afbcff2c93cbe03ec57
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 0.1.2
6
+
7
+ - Fix the `easytest` command exit code.
8
+
5
9
  ## 0.1.1
6
10
 
7
11
  No code changes.
data/exe/easytest CHANGED
@@ -5,7 +5,4 @@ $LOAD_PATH << File.join(Dir.pwd, "test")
5
5
 
6
6
  require "easytest"
7
7
 
8
- test_files = Dir.glob("test/**/*_test.rb").filter do |file|
9
- ARGV.empty? || ARGV.any? { |pattern| file.include?(pattern) }
10
- end
11
- test_files.each { |file| load file }
8
+ exit Easytest::CLI.new(ARGV).run
@@ -0,0 +1,29 @@
1
+ module Easytest
2
+ class CLI
3
+ SUCCESS = 0
4
+ FAILURE = 1
5
+
6
+ attr_reader :argv
7
+ attr_reader :start_time
8
+
9
+ def initialize(argv)
10
+ @argv = argv
11
+ @start_time = Time.now
12
+ end
13
+
14
+ def run
15
+ Easytest.start
16
+ setup
17
+ successful = Easytest.run
18
+ successful ? SUCCESS : FAILURE
19
+ end
20
+
21
+ private
22
+
23
+ def setup
24
+ Dir.glob(Easytest.test_files_location)
25
+ .filter { |file| argv.empty? || argv.any? { |pattern| file.include?(pattern) } }
26
+ .each { |test_file| load test_file }
27
+ end
28
+ end
29
+ end
data/lib/easytest/dsl.rb CHANGED
@@ -6,8 +6,8 @@ module Easytest
6
6
  Easytest.add_case Case.new(name: name, file: file, &block)
7
7
  end
8
8
 
9
- def expect(actual)
10
- Expectation.new(actual)
9
+ def expect(actual = nil, &block)
10
+ Expectation.new(actual, &block)
11
11
  end
12
12
  end
13
13
  end
@@ -1,19 +1,41 @@
1
1
  module Easytest
2
2
  class Expectation
3
- def initialize(actual)
3
+ attr_reader :actual
4
+ attr_reader :block
5
+
6
+ def initialize(actual, &block)
4
7
  @actual = actual
8
+ @block = block
5
9
  end
6
10
 
7
11
  def to_eq(expected)
8
- Matcher::Equal.new(actual: @actual, expected: expected).match!
12
+ Matcher::Equal.new(actual: eval_actual, expected: expected).match!
9
13
  end
10
14
 
11
15
  def to_be(expected)
12
- Matcher::Be.new(actual: @actual, expected: expected).match!
16
+ Matcher::Be.new(actual: eval_actual, expected: expected).match!
13
17
  end
14
18
 
15
19
  def to_be_nil
16
- Matcher::BeNil.new(actual: @actual).match!
20
+ Matcher::BeNil.new(actual: eval_actual).match!
21
+ end
22
+
23
+ def to_raise(exception_class)
24
+
25
+ end
26
+
27
+ def to_not_raise(exception_class)
28
+
29
+ end
30
+
31
+ private
32
+
33
+ def eval_actual
34
+ if block
35
+ block.call
36
+ else
37
+ actual
38
+ end
17
39
  end
18
40
  end
19
41
  end
@@ -12,6 +12,16 @@ module Easytest
12
12
  end
13
13
 
14
14
  def match!
15
+ unless match?
16
+ raise UnmatchedError.new(message: message, actual: actual, expected: expected)
17
+ end
18
+ end
19
+
20
+ def expected
21
+ raise NotImplementedError
22
+ end
23
+
24
+ def message
15
25
  raise NotImplementedError
16
26
  end
17
27
  end
@@ -12,10 +12,8 @@ module Easytest
12
12
  actual.equal? expected
13
13
  end
14
14
 
15
- def match!
16
- unless match?
17
- raise UnmatchedError.new(message: "should be same", actual: actual, expected: expected)
18
- end
15
+ def message
16
+ "should be same"
19
17
  end
20
18
  end
21
19
  end
@@ -5,10 +5,12 @@ module Easytest
5
5
  actual.nil?
6
6
  end
7
7
 
8
- def match!
9
- unless match?
10
- raise UnmatchedError.new(message: "should be `nil`", actual: actual, expected: nil)
11
- end
8
+ def expected
9
+ nil
10
+ end
11
+
12
+ def message
13
+ "should be nil"
12
14
  end
13
15
  end
14
16
  end
@@ -12,11 +12,15 @@ module Easytest
12
12
  actual == expected
13
13
  end
14
14
 
15
- def match!
16
- unless match?
17
- raise UnmatchedError.new(message: "should equal", actual: actual, expected: expected)
18
- end
15
+ def message
16
+ "should equal"
19
17
  end
18
+
19
+ # def match!
20
+ # unless match?
21
+ # raise UnmatchedError.new(message: "should equal", actual: actual, expected: expected)
22
+ # end
23
+ # end
20
24
  end
21
25
  end
22
26
  end
@@ -0,0 +1,94 @@
1
+ module Easytest
2
+ class Runner
3
+ attr_reader :start_time
4
+ attr_accessor :passed_count
5
+ attr_accessor :failed_count
6
+ attr_accessor :file_count
7
+
8
+ def initialize(start_time: Time.now)
9
+ @start_time = start_time
10
+ @passed_count = 0
11
+ @failed_count = 0
12
+ @file_count = 0
13
+ end
14
+
15
+ def run
16
+ cases_by_file = cases.group_by { |c| c.file }
17
+ cases_by_file.each do |file, cases|
18
+ self.file_count += 1
19
+ reports = []
20
+
21
+ cases.each do |c|
22
+ passed = c.run
23
+
24
+ if passed
25
+ self.passed_count += 1
26
+ else
27
+ self.failed_count += 1
28
+ reports << c.report.gsub(/^/, " ")
29
+ end
30
+ end
31
+
32
+ link = Utils.terminal_hyperlink(file)
33
+ if reports.empty?
34
+ puts "#{Rainbow(" PASS ").bright.bg(:green)} #{link}"
35
+ else
36
+ puts "#{Rainbow(" FAIL ").bright.bg(:red)} #{link}"
37
+ reports.each { |report| puts report ; puts "" }
38
+ end
39
+ end
40
+
41
+ if no_tests?
42
+ puts Rainbow("Oops. No tests found!").red.bright
43
+ puts ""
44
+ puts "Write `#{Easytest.test_files_location}` files to include at least one `test` block,"
45
+ puts "or specify a matching pattern to the `easytest` command."
46
+ false
47
+ else
48
+ puts ""
49
+ puts " #{Rainbow('Tests:').bright} #{summary}"
50
+ puts " #{Rainbow('Time:').bright} #{elapsed_time.round(5)} seconds"
51
+ all_passed?
52
+ end
53
+ end
54
+
55
+ def cases
56
+ @cases ||= []
57
+ end
58
+
59
+ def add_case(new_case)
60
+ cases << new_case
61
+ end
62
+
63
+ private
64
+
65
+ def total_count
66
+ passed_count + failed_count
67
+ end
68
+
69
+ def all_passed?
70
+ failed_count == 0
71
+ end
72
+
73
+ def no_tests?
74
+ total_count == 0
75
+ end
76
+
77
+ def elapsed_time
78
+ Time.now - start_time
79
+ end
80
+
81
+ def summary
82
+ summary = ""
83
+
84
+ if failed_count == 0
85
+ summary << Rainbow("#{passed_count} passed").green.bright
86
+ else
87
+ summary << Rainbow("#{failed_count} failed").red.bright
88
+ summary << ", #{Rainbow("#{passed_count} passed").green.bright}"
89
+ end
90
+
91
+ summary << ", #{total_count} total #{Rainbow("(#{file_count} files)").dimgray}"
92
+ end
93
+ end
94
+ end
@@ -1,3 +1,3 @@
1
1
  module Easytest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/easytest.rb CHANGED
@@ -4,6 +4,7 @@ require "rainbow"
4
4
  require_relative "easytest/version"
5
5
 
6
6
  require_relative "easytest/case"
7
+ require_relative "easytest/cli"
7
8
  require_relative "easytest/dsl"
8
9
  require_relative "easytest/errors"
9
10
  require_relative "easytest/expectation"
@@ -12,75 +13,23 @@ require_relative "easytest/matcher/be"
12
13
  require_relative "easytest/matcher/be_nil"
13
14
  require_relative "easytest/matcher/equal"
14
15
  require_relative "easytest/reporter"
16
+ require_relative "easytest/runner"
15
17
  require_relative "easytest/utils"
16
18
 
17
19
  module Easytest
18
- def self.run
19
- passed_count = 0
20
- failed_count = 0
21
- file_count = 0
22
-
23
- cases_by_file = cases.group_by { |c| c.file }
24
- cases_by_file.each do |file, cases|
25
- file_count += 1
26
- reports = []
27
-
28
- cases.each do |c|
29
- passed = c.run
30
-
31
- if passed
32
- passed_count += 1
33
- else
34
- failed_count += 1
35
- reports << c.report.gsub(/^/, " ")
36
- end
37
- end
38
-
39
- link = Utils.terminal_hyperlink(file)
40
- if reports.empty?
41
- puts "#{Rainbow(" PASS ").bright.bg(:green)} #{link}"
42
- else
43
- puts "#{Rainbow(" FAIL ").bright.bg(:red)} #{link}"
44
- reports.each { |report| puts report ; puts "" }
45
- end
46
- end
47
-
48
- [passed_count, failed_count, file_count]
49
- end
50
-
51
- def self.cases
52
- @cases ||= []
20
+ def self.start
21
+ @runner = Runner.new
53
22
  end
54
23
 
55
24
  def self.add_case(new_case)
56
- cases << new_case
25
+ @runner.cases << new_case
57
26
  end
58
- end
59
-
60
- start_time = Time.now
61
-
62
- at_exit do
63
- passed_count, failed_count, file_count = Easytest.run
64
- total_count = passed_count + failed_count
65
27
 
66
- time = Time.now - start_time
67
-
68
- if total_count == 0
69
- puts Rainbow("A test suite should have at least one test case!").red.bright
70
- puts ""
71
- puts "Please put `test/**/*_test.rb` files or specify valid patterns to the `easytest` command."
72
- else
73
- summary = ""
74
- if failed_count == 0
75
- summary << Rainbow("#{passed_count} passed").green.bright
76
- else
77
- summary << Rainbow("#{failed_count} failed").red.bright
78
- summary << ", #{Rainbow("#{passed_count} passed").green.bright}"
79
- end
80
- summary << ", #{total_count} total #{Rainbow("(#{file_count} files)").dimgray}"
28
+ def self.run
29
+ @runner.run
30
+ end
81
31
 
82
- puts ""
83
- puts " #{Rainbow('Tests:').bright} #{summary}"
84
- puts " #{Rainbow('Time:').bright} #{time.round(5)} seconds"
32
+ def self.test_files_location
33
+ "test/**/*_test.rb"
85
34
  end
86
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easytest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masafumi Koba
@@ -38,6 +38,7 @@ files:
38
38
  - exe/easytest
39
39
  - lib/easytest.rb
40
40
  - lib/easytest/case.rb
41
+ - lib/easytest/cli.rb
41
42
  - lib/easytest/dsl.rb
42
43
  - lib/easytest/errors.rb
43
44
  - lib/easytest/expectation.rb
@@ -46,6 +47,7 @@ files:
46
47
  - lib/easytest/matcher/be_nil.rb
47
48
  - lib/easytest/matcher/equal.rb
48
49
  - lib/easytest/reporter.rb
50
+ - lib/easytest/runner.rb
49
51
  - lib/easytest/utils.rb
50
52
  - lib/easytest/version.rb
51
53
  homepage: https://github.com/ybiquitous/easytest