|
1 |
| -module Spec::DSL |
2 |
| - def describe(description, file = __FILE__, line = __LINE__, &block) |
3 |
| - Spec::RootContext.describe(description.to_s, file, line, &block) |
| 1 | +require "colorize" |
| 2 | +require "option_parser" |
| 3 | +require "signal" |
| 4 | + |
| 5 | +module Spec |
| 6 | + private COLORS = { |
| 7 | + success: :green, |
| 8 | + fail: :red, |
| 9 | + error: :red, |
| 10 | + pending: :yellow, |
| 11 | + } |
| 12 | + |
| 13 | + private LETTERS = { |
| 14 | + success: '.', |
| 15 | + fail: 'F', |
| 16 | + error: 'E', |
| 17 | + pending: '*', |
| 18 | + } |
| 19 | + |
| 20 | + @@use_colors = true |
| 21 | + |
| 22 | + # :nodoc: |
| 23 | + def self.color(str, status) |
| 24 | + if use_colors? |
| 25 | + str.colorize(COLORS[status]) |
| 26 | + else |
| 27 | + str |
| 28 | + end |
4 | 29 | end
|
5 | 30 |
|
6 |
| - def context(description, file = __FILE__, line = __LINE__, &block) |
7 |
| - describe(description.to_s, file, line, &block) |
| 31 | + # :nodoc: |
| 32 | + def self.use_colors? |
| 33 | + @@use_colors |
8 | 34 | end
|
9 | 35 |
|
10 |
| - def it(description, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block) |
11 |
| - return unless Spec.matches?(description, file, line, end_line) |
| 36 | + # :nodoc: |
| 37 | + def self.use_colors=(@@use_colors) |
| 38 | + end |
12 | 39 |
|
13 |
| - Spec.formatters.each(&.before_example(description)) |
| 40 | + # :nodoc: |
| 41 | + class AssertionFailed < Exception |
| 42 | + getter file : String |
| 43 | + getter line : Int32 |
14 | 44 |
|
15 |
| - start = Time.now |
16 |
| - begin |
17 |
| - Spec.run_before_each_hooks |
18 |
| - block.call |
19 |
| - Spec::RootContext.report(:success, description, file, line, Time.now - start) |
20 |
| - rescue ex : Spec::AssertionFailed |
21 |
| - Spec::RootContext.report(:fail, description, file, line, Time.now - start, ex) |
22 |
| - Spec.abort! if Spec.fail_fast? |
23 |
| - rescue ex |
24 |
| - Spec::RootContext.report(:error, description, file, line, Time.now - start, ex) |
25 |
| - Spec.abort! if Spec.fail_fast? |
26 |
| - ensure |
27 |
| - Spec.run_after_each_hooks |
| 45 | + def initialize(message, @file, @line) |
| 46 | + super(message) |
28 | 47 | end
|
29 | 48 | end
|
30 | 49 |
|
31 |
| - def pending(description, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block) |
32 |
| - return unless Spec.matches?(description, file, line, end_line) |
| 50 | + @@aborted = false |
| 51 | + |
| 52 | + # :nodoc: |
| 53 | + def self.abort! |
| 54 | + exit |
| 55 | + end |
| 56 | + |
| 57 | + # :nodoc: |
| 58 | + def self.pattern=(pattern) |
| 59 | + @@pattern = Regex.new(Regex.escape(pattern)) |
| 60 | + end |
| 61 | + |
| 62 | + # :nodoc: |
| 63 | + def self.line=(@@line : Int32) |
| 64 | + end |
33 | 65 |
|
34 |
| - Spec.formatters.each(&.before_example(description)) |
| 66 | + # :nodoc: |
| 67 | + def self.slowest=(@@slowest : Int32) |
| 68 | + end |
35 | 69 |
|
36 |
| - Spec::RootContext.report(:pending, description, file, line) |
| 70 | + # :nodoc: |
| 71 | + def self.slowest |
| 72 | + @@slowest |
37 | 73 | end
|
38 | 74 |
|
39 |
| - def assert(file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block) |
40 |
| - it("assert", file, line, end_line, &block) |
| 75 | + # :nodoc: |
| 76 | + def self.to_human(span : Time::Span) |
| 77 | + total_milliseconds = span.total_milliseconds |
| 78 | + if total_milliseconds < 1 |
| 79 | + return "#{(span.total_milliseconds * 1000).round.to_i} microseconds" |
| 80 | + end |
| 81 | + |
| 82 | + total_seconds = span.total_seconds |
| 83 | + if total_seconds < 1 |
| 84 | + return "#{span.total_milliseconds.round(2)} milliseconds" |
| 85 | + end |
| 86 | + |
| 87 | + if total_seconds < 60 |
| 88 | + return "#{total_seconds.round(2)} seconds" |
| 89 | + end |
| 90 | + |
| 91 | + minutes = span.minutes |
| 92 | + seconds = span.seconds |
| 93 | + "#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes" |
41 | 94 | end
|
42 | 95 |
|
43 |
| - def fail(msg, file = __FILE__, line = __LINE__) |
44 |
| - raise Spec::AssertionFailed.new(msg, file, line) |
| 96 | + # :nodoc: |
| 97 | + def self.add_location(file, line) |
| 98 | + locations = @@locations ||= {} of String => Array(Int32) |
| 99 | + lines = locations[File.expand_path(file)] ||= [] of Int32 |
| 100 | + lines << line |
| 101 | + end |
| 102 | + |
| 103 | + # :nodoc: |
| 104 | + def self.matches?(description, file, line, end_line = line) |
| 105 | + spec_pattern = @@pattern |
| 106 | + spec_line = @@line |
| 107 | + locations = @@locations |
| 108 | + |
| 109 | + # When a method invokes `it` and only forwards line information, |
| 110 | + # not end_line information (this can happen in code before we |
| 111 | + # introduced the end_line feature) then running a spec by giving |
| 112 | + # a line won't work because end_line might be located before line. |
| 113 | + # So, we also check `line == spec_line` to somehow preserve |
| 114 | + # backwards compatibility. |
| 115 | + if spec_line && (line == spec_line || line <= spec_line <= end_line) |
| 116 | + return true |
| 117 | + end |
| 118 | + |
| 119 | + if locations |
| 120 | + lines = locations[file]? |
| 121 | + return true if lines && lines.any? { |l| line == l || line <= l <= end_line } |
| 122 | + end |
| 123 | + |
| 124 | + if spec_pattern || spec_line || locations |
| 125 | + Spec::RootContext.matches?(description, spec_pattern, spec_line, locations) |
| 126 | + else |
| 127 | + true |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + @@fail_fast = false |
| 132 | + |
| 133 | + # :nodoc: |
| 134 | + def self.fail_fast=(@@fail_fast) |
| 135 | + end |
| 136 | + |
| 137 | + # :nodoc: |
| 138 | + def self.fail_fast? |
| 139 | + @@fail_fast |
| 140 | + end |
| 141 | + |
| 142 | + # Instructs the spec runner to execute the given block |
| 143 | + # before each spec, regardless of where this method is invoked. |
| 144 | + def self.before_each(&block) |
| 145 | + before_each = @@before_each ||= [] of -> |
| 146 | + before_each << block |
| 147 | + end |
| 148 | + |
| 149 | + # Instructs the spec runner to execute the given block |
| 150 | + # after each spec, regardless of where this method is invoked. |
| 151 | + def self.after_each(&block) |
| 152 | + after_each = @@after_each ||= [] of -> |
| 153 | + after_each << block |
| 154 | + end |
| 155 | + |
| 156 | + # :nodoc: |
| 157 | + def self.run_before_each_hooks |
| 158 | + @@before_each.try &.each &.call |
| 159 | + end |
| 160 | + |
| 161 | + # :nodoc: |
| 162 | + def self.run_after_each_hooks |
| 163 | + @@after_each.try &.each &.call |
| 164 | + end |
| 165 | + |
| 166 | + # :nodoc: |
| 167 | + def self.run |
| 168 | + start_time = Time.now |
| 169 | + at_exit do |
| 170 | + elapsed_time = Time.now - start_time |
| 171 | + Spec::RootContext.print_results(elapsed_time) |
| 172 | + exit 1 unless Spec::RootContext.succeeded |
| 173 | + end |
45 | 174 | end
|
46 | 175 | end
|
47 | 176 |
|
48 |
| -include Spec::DSL |
| 177 | +require "./*" |
0 commit comments