uspec 1.3.0 → 1.4.0

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: d17e77cb5065265172f613c90f9edfb022c217386b56d34af6858b078ba406d8
4
- data.tar.gz: 1c77be04e49ca61e243d7c35aa84c9de56dac3ae8f099ba351f6efa5d9a5ca38
3
+ metadata.gz: 14e2539758227d31271ce1e3cf534b0edb491cb685f62ab7c16a7fbc711caf88
4
+ data.tar.gz: 638daabf4483e4ca6a34b480030f896078dd49bb4286afee24830f3395ed3563
5
5
  SHA512:
6
- metadata.gz: cf99ec58c05e076e9060b7ac71271e5fcdfd8ab44e03810b41a1ac2a67a4921777dca4ed2a5c27637f071b20fabb0262118b4a7214448b63ea8193450dc663b6
7
- data.tar.gz: eb1c4de66aef1dfef542774fd18b9484bf26778bfe5651c0594fc6cd543472003c32484048435a41d9baacdf3695376fcb1e25fd659ff36183072ddb44eafb41
6
+ metadata.gz: 22747fc9d720163116d69d3f8fa8ba7ad15df46f37dcc3138b93b1eab3a07f1fe765a01b8a1d6ef7ec3cb094c60adbefa39813098841d50a23b62291ac4b9116
7
+ data.tar.gz: ca54684feb09cdd6b4fc4370b0dc492fd13dfb20ef32c5255329b7a7744ca1b799fce6afc7f8fa039f5658b473334ee06124b908d5a8f113f8a6c035efbcb7a2
@@ -1,10 +1,3 @@
1
1
  require 'bundler/setup'
2
2
 
3
3
  Bundler.require :development, :test
4
-
5
- require 'uspec'
6
-
7
- Dir.chdir File.dirname(__FILE__)
8
-
9
- extend Uspec
10
-
data/lib/uspec/cli.rb CHANGED
@@ -74,36 +74,10 @@ class Uspec::CLI
74
74
  end
75
75
  elsif path.exist? then
76
76
  puts "#{path.basename path.extname}:"
77
- harness.define.instance_eval(path.read, path.to_s)
77
+ harness.file_eval path
78
78
  else
79
79
  warn "path not found: #{path}"
80
80
  end
81
- rescue Exception => error
82
-
83
- if SignalException === error || SystemExit === error then
84
- exit 3
85
- end
86
-
87
- error_file, error_line, _ = error.backtrace.first.split ?:
88
-
89
- message = <<-MSG
90
- #{error.class} : #{error.message}
91
-
92
- Uspec encountered an error when loading a test file.
93
- This is probably a typo in the test file or the file it is testing.
94
-
95
- If you think this is a bug in Uspec please report it: https://github.com/acook/uspec/issues/new
96
-
97
- Error occured when loading test file `#{spec || path}`.
98
- The origin of the error may be in file `#{error_file}` on line ##{error_line}.
99
-
100
- \t#{error.backtrace[0,3].join "\n\t"}
101
- MSG
102
- puts
103
- warn message
104
- stats << Uspec::Result.new(message, error, caller)
105
-
106
- handle_interrupt! error
107
81
  end
108
82
 
109
83
  end
data/lib/uspec/harness.rb CHANGED
@@ -14,21 +14,53 @@ module Uspec
14
14
  cli.stats
15
15
  end
16
16
 
17
+ def file_eval path
18
+ define.instance_eval(path.read, path.to_s)
19
+ rescue Exception => error
20
+ if SignalException === error || SystemExit === error then
21
+ exit 3
22
+ end
23
+
24
+ error_file, error_line, _ = error.backtrace.first.split ?:
25
+
26
+ message = <<-MSG
27
+ #{error.class} : #{error.message}
28
+
29
+ Uspec encountered an error when loading a test file.
30
+ This is probably a typo in the test file or the file it is testing.
31
+
32
+ If you think this is a bug in Uspec please report it: https://github.com/acook/uspec/issues/new
33
+
34
+ Error occured when loading test file `#{spec || path}`.
35
+ The origin of the error may be in file `#{error_file}` on line ##{error_line}.
36
+
37
+ \t#{error.backtrace[0,3].join "\n\t"}
38
+ MSG
39
+ puts
40
+ warn message
41
+ stats << Uspec::Result.new(message, error, true)
42
+
43
+ cli.handle_interrupt! error
44
+ end
45
+
17
46
  def spec_eval description, &block
47
+ ex = nil
18
48
  state = 0
19
49
  print ' -- ', description
20
50
 
21
51
  if block then
22
52
  begin
23
53
  state = 1
24
- raw_result = Uspec::Spec.new(self, description, &block).__uspec_block
54
+ spec = Uspec::Spec.new(self, description, &block)
55
+ raw_result = spec.__uspec_block
25
56
  state = 2
26
57
  rescue Exception => raw_result
27
58
  state = 3
59
+ ex = true
28
60
  end
29
61
  end
30
62
 
31
- result = Uspec::Result.new description, raw_result, caller
63
+ result = Uspec::Result.new spec, raw_result, ex
32
64
 
33
65
  unless block then
34
66
  state = 4
@@ -49,7 +81,7 @@ module Uspec
49
81
  MSG
50
82
  puts
51
83
  warn message
52
- stats << Uspec::Result.new(message, error, caller)
84
+ stats << Uspec::Result.new(message, error, true)
53
85
  ensure
54
86
  cli.handle_interrupt! result.raw
55
87
  return [state, error, result, raw_result]
data/lib/uspec/result.rb CHANGED
@@ -6,14 +6,17 @@ module Uspec
6
6
  include Terminal
7
7
 
8
8
  PREFIX = "#{Uspec::Terminal.newline}#{Uspec::Terminal.yellow}>\t#{Uspec::Terminal.normal}"
9
+ TRACE_EXCLUDE_PATTERN = /#{Uspec.libpath.join 'lib'}|#{Uspec.libpath.join 'bin'}/
9
10
 
10
- def initialize spec, raw, source
11
+ def initialize spec, raw, ex
11
12
  @spec = spec
12
13
  @raw = raw
13
- @source = source
14
+ @ex = ex
14
15
  @handler = ::TOISB.wrap raw
16
+ @full_backtrace = false
17
+ @caller = caller
15
18
  end
16
- attr_reader :spec, :raw, :source, :handler
19
+ attr_reader :spec, :raw, :ex, :handler, :full_backtrace
17
20
 
18
21
  def pretty
19
22
  if raw == true then
@@ -22,7 +25,7 @@ module Uspec
22
25
  red raw
23
26
  elsif pending? then
24
27
  yellow 'pending'
25
- elsif Exception === raw then
28
+ elsif ex == true then
26
29
  [
27
30
  red('Exception'), vspace,
28
31
  hspace, 'Spec encountered an Exception ', newline,
@@ -41,8 +44,32 @@ module Uspec
41
44
  end
42
45
 
43
46
  def trace
44
- raw.backtrace.inject(String.new) do |text, line|
47
+ @backtrace ||= indent_bt clean_bt(raw.backtrace, !full_backtrace)
48
+ end
49
+
50
+ def source
51
+ @source ||= clean_bt @caller
52
+ end
53
+
54
+ def indent_bt bt
55
+ bt.inject(String.new) do |text, line|
45
56
  text << "#{hspace}#{line}#{newline}"
57
+ end if bt
58
+ end
59
+
60
+ def clean_bt bt, skip_internal = true
61
+ bt.inject(Array.new) do |t, line|
62
+ next t if skip_internal && line.match(TRACE_EXCLUDE_PATTERN)
63
+ t << rewrite_bt_caller(line)
64
+ end if bt
65
+ end
66
+
67
+ def rewrite_bt_caller line
68
+ return line if full_backtrace
69
+ if line.match TRACE_EXCLUDE_PATTERN then
70
+ line
71
+ else
72
+ line.sub /file_eval/, 'spec_block'
46
73
  end
47
74
  end
48
75
 
@@ -54,14 +81,29 @@ module Uspec
54
81
  "#{handler.subklassinfo}: "
55
82
  end
56
83
 
84
+ def desc
85
+ if String === spec then
86
+ spec
87
+ elsif Uspec::Spec === spec then
88
+ spec.instance_variable_get :@__uspec_description
89
+ else
90
+ spec.inspect
91
+ end
92
+ end
93
+
57
94
  # Attempts to inspect an object
58
95
  def inspector
59
96
  if String === raw && raw.include?(?\n) then
60
97
  # if object is a multiline string, display it unescaped
61
-
62
98
  [
63
99
  raw.split(newline).unshift(newline).join(PREFIX), normal, newline,
64
100
  ].join
101
+ elsif Exception === raw then
102
+ [
103
+ raw.message, vspace,
104
+ white(trace),
105
+ normal, newline,
106
+ ].join
65
107
  else
66
108
  handler.inspector!
67
109
  end
@@ -82,7 +124,7 @@ module Uspec
82
124
 
83
125
  If you think this is a bug in Uspec please report it: https://github.com/acook/uspec/issues/new
84
126
 
85
- Error may have occured in test `#{spec}` in file `#{error_file}` on line ##{error_line}.
127
+ Error may have occured in test `#{desc}` in file `#{error_file}` on line ##{error_line}.
86
128
 
87
129
  \t#{error.backtrace.join "\n\t"}
88
130
  MSG
@@ -105,7 +147,7 @@ module Uspec
105
147
  end
106
148
 
107
149
  def inspect
108
- "#{self.class} for `#{spec}` -> #{pretty}"
150
+ "#{self.class} for `#{desc}` -> #{pretty}"
109
151
  end
110
152
  end
111
153
  end
data/lib/uspec/spec.rb CHANGED
@@ -5,6 +5,7 @@ module Uspec
5
5
 
6
6
  def initialize harness, description, &block
7
7
  @__uspec_description = description
8
+ @__uspec_block = block
8
9
  @__uspec_harness = harness
9
10
  ns = harness.define
10
11
 
data/lib/uspec/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Uspec
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
data/lib/uspec.rb CHANGED
@@ -1,8 +1,3 @@
1
- require_relative 'uspec/version'
2
- require_relative 'uspec/harness'
3
- require_relative 'uspec/define'
4
- require_relative 'uspec/stats'
5
-
6
1
  module Uspec
7
2
  def self.included object
8
3
  warn 'Use extend instead of include.'
@@ -15,4 +10,13 @@ module Uspec
15
10
  # object.extend Uspec::DSL
16
11
  #end
17
12
  end
13
+
14
+ def self.libpath
15
+ Pathname.new(__FILE__).dirname.dirname
16
+ end
18
17
  end
18
+
19
+ require_relative 'uspec/version'
20
+ require_relative 'uspec/harness'
21
+ require_relative 'uspec/define'
22
+ require_relative 'uspec/stats'
data/uspec/result_spec.rb CHANGED
@@ -3,7 +3,7 @@ require_relative "uspec_helper"
3
3
  bo = BasicObject.new
4
4
 
5
5
  spec "#pretty doesn't die when given a BasicObject" do
6
- result = Uspec::Result.new "BasicObject Result", bo, []
6
+ result = Uspec::Result.new "BasicObject Result", bo, nil
7
7
  expected = "#<BasicObject:"
8
8
  actual = result.pretty
9
9
  actual.include?(expected) || actual
@@ -14,28 +14,28 @@ class ::TestObject < BasicObject; end
14
14
  obj = TestObject.new
15
15
 
16
16
  spec "ensure BasicObject subclass instances work" do
17
- result = Uspec::Result.new "BasicObject Subclass Result", obj, []
17
+ result = Uspec::Result.new "BasicObject Subclass Result", obj, nil
18
18
  expected = "#<BasicObject/TestObject:"
19
19
  actual = result.pretty
20
20
  actual.include?(expected) || result.pretty
21
21
  end
22
22
 
23
23
  spec "display basic info about Object" do
24
- result = Uspec::Result.new "Object Result", Object.new, []
24
+ result = Uspec::Result.new "Object Result", Object.new, nil
25
25
  expected = "Object < BasicObject: \e[0m#<Object:"
26
26
  actual = result.pretty
27
27
  actual.include?(expected) || result.pretty
28
28
  end
29
29
 
30
30
  spec "display basic info about Array" do
31
- result = Uspec::Result.new "Array Result", [], []
31
+ result = Uspec::Result.new "Array Result", [], nil
32
32
  expected = "Array < Object"
33
33
  actual = result.pretty
34
34
  actual.include?(expected) || result.pretty
35
35
  end
36
36
 
37
37
  spec "display basic info about Array class" do
38
- result = Uspec::Result.new "Array Class Result", Array, []
38
+ result = Uspec::Result.new "Array Class Result", Array, nil
39
39
  expected = "Class < Module: \e[0mArray Class"
40
40
  actual = result.pretty
41
41
  actual.include?(expected) || result.pretty
@@ -44,7 +44,7 @@ end
44
44
  parent = [obj]
45
45
 
46
46
  spec "ensure parent object of BasicObject subclasses get a useful error message" do
47
- result = Uspec::Result.new "BasicObject Parent Result", parent, []
47
+ result = Uspec::Result.new "BasicObject Parent Result", parent, nil
48
48
  expected = "BasicObject and its subclasses"
49
49
  actual = result.pretty
50
50
  actual.include?(expected) || result.inspector
@@ -54,7 +54,7 @@ class ::InspectFail; def inspect; raise RuntimeError, "This error is intentional
54
54
  inspect_fail = InspectFail.new
55
55
 
56
56
  spec "display a useful error message when a user-defined inspect method fails" do
57
- result = Uspec::Result.new "Inspect Fail Result", inspect_fail, []
57
+ result = Uspec::Result.new "Inspect Fail Result", inspect_fail, nil
58
58
  expected = "raises an exception"
59
59
  actual = result.pretty
60
60
  actual.include?(expected) || result.inspector
@@ -63,7 +63,48 @@ end
63
63
  spec "display strings more like their actual contents" do
64
64
  string = "this string:\nshould display \e\[42;2mproperly"
65
65
  expected = /this string:\n.*should display \e\[42;2mproperly/
66
- result = Uspec::Result.new "Inspect Fail Result", string, []
66
+ result = Uspec::Result.new "Inspect Fail Result", string, nil
67
67
  actual = result.pretty
68
68
  actual.match?(expected) || result.inspector
69
69
  end
70
+
71
+ def exception_value
72
+ raise "A test exception!"
73
+ rescue => err
74
+ return err
75
+ end
76
+
77
+ spec "handles exception values" do
78
+ result = Uspec::Result.new "Exception Value Result", exception_value, nil
79
+ expected = "RuntimeError < StandardError: \e[0mA test exception!"
80
+ actual = result.pretty
81
+ actual.include?(expected) || result.pretty
82
+ end
83
+
84
+ spec "handles exception values including backtraces" do
85
+ result = Uspec::Result.new "Exception Value Result", exception_value, nil
86
+ expected = "exception_value"
87
+ actual = result.pretty
88
+ actual.include?(expected) || result.pretty
89
+ end
90
+
91
+ spec "handles raised exceptions" do
92
+ result = Uspec::Result.new "Exception Raised Result", exception_value, true
93
+ expected = "RuntimeError < StandardError: \e[0mA test exception!"
94
+ actual = result.pretty
95
+ actual.include?(expected) || result.pretty
96
+ end
97
+
98
+ spec "handles raised exceptions without backtraces" do
99
+ result = Uspec::Result.new "Exception Raised Result", Exception.new, true
100
+ expected = "Exception < Object: \e[0mException"
101
+ actual = result.pretty
102
+ actual.include?(expected) || result.pretty
103
+ end
104
+
105
+ spec "doesn't show 'run' for spec file in stack trace" do
106
+ result = Uspec::Result.new "No Run Exception Trace Result", exception_value, true
107
+ expected = /uspec.*run/
108
+ actual = result.pretty
109
+ !actual.match?(expected) || result.pretty
110
+ end
data/uspec/stats_spec.rb CHANGED
@@ -1,12 +1,19 @@
1
1
  require_relative 'uspec_helper'
2
2
 
3
+ def stats_object
4
+ Uspec::Stats.new
5
+ end
6
+
3
7
  spec 'stats can be inspected' do
8
+ # this is a regression test
9
+ # the issue it covers occured because of a refactor where stats had a custom inspect method
10
+ # stats no longer has a custom inspect method, but this makes sure that nothing breaks
4
11
  actual = @__uspec_harness.stats.inspect
5
12
  actual.include?("failure") || actual
6
13
  end
7
14
 
8
15
  spec 'stats inspect does not have any stray whitespace' do
9
- output = @__uspec_harness.stats.inspect
16
+ output = stats_object.inspect
10
17
  match = output.match /(.*(?: |\n))/m
11
18
  match == nil || match
12
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony M. Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-23 00:00:00.000000000 Z
11
+ date: 2024-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: that_object_is_so_basic