Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Spec
# :nodoc:
#
# Implement formatter configuration.
def configure_formatter(formatter, output_path = nil)
def configure_formatter(formatter : String, output_path : String? = nil)
case formatter
when "junit"
junit_formatter = Spec::JUnitFormatter.file(self, Path.new(output_path.not_nil!))
Expand All @@ -108,7 +108,7 @@ module Spec
end
end

def main(args)
def main(args : Array(String)) : Nil
begin
option_parser.parse(args)
rescue e : OptionParser::InvalidOption
Expand Down
6 changes: 3 additions & 3 deletions src/spec/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ module Spec
@color = Colorize.default_enabled?(@stdout, @stderr)
end

def add_location(file, line)
def add_location(file : String, line : Int32) : Array(Int32)
locations = @locations ||= {} of String => Array(Int32)
locations.put_if_absent(File.expand_path(file)) { [] of Int32 } << line
end

def add_tag(tag)
def add_tag(tag : String) : Set(String)
if anti_tag = tag.lchop?('~')
(@anti_tags ||= Set(String).new) << anti_tag
else
Expand All @@ -41,7 +41,7 @@ module Spec
getter randomizer_seed : UInt64?
getter randomizer : Random::PCG32?

def order=(mode)
def order=(mode : String | UInt64) : Nil
seed =
case mode
when "default"
Expand Down
6 changes: 3 additions & 3 deletions src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Spec

protected abstract def cli : CLI

def randomize(randomizer)
def randomize(randomizer : Random) : Array(Spec::Example | Spec::ExampleGroup)
children.each do |child|
child.randomize(randomizer) if child.is_a?(ExampleGroup)
end
Expand Down Expand Up @@ -159,7 +159,7 @@ module Spec

protected getter cli : CLI

def results_for(status : Status)
def results_for(status : Status) : Array(Spec::Result)
@results[status]
end

Expand Down Expand Up @@ -357,7 +357,7 @@ module Spec
end

# :nodoc:
def run
def run : Nil
cli.formatters.each(&.push(self))

ran = run_around_all_hooks(ExampleGroup::Procsy.new(self) { internal_run })
Expand Down
22 changes: 11 additions & 11 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module Spec
end

# :nodoc:
def self.to_human(span : Time::Span)
def self.to_human(span : Time::Span) : String
total_milliseconds = span.total_milliseconds
if total_milliseconds < 1
return "#{(span.total_milliseconds * 1000).round.to_i} microseconds"
Expand Down Expand Up @@ -162,14 +162,14 @@ module Spec
class CLI
@aborted = false

def abort!
def abort! : Nil
@aborted = true
finish_run
end

@split_filter : SplitFilter? = nil

def add_split_filter(filter)
def add_split_filter(filter : String?) : Spec::SplitFilter?
if filter
r, _, m = filter.partition('%')
@split_filter = SplitFilter.new(remainder: r.to_i, quotient: m.to_i)
Expand All @@ -180,7 +180,7 @@ module Spec

@start_time : Time::Span? = nil

def run
def run : Nil
@start_time = Time.monotonic

at_exit do |status|
Expand All @@ -204,22 +204,22 @@ module Spec
end
end

def colorize(str, status : Status)
def colorize(str : String | Char, status : Status) : Colorize::Object(String) | Colorize::Object(Char)
str.colorize(status.color).toggle(@color)
end

def colorize(str, kind : InfoKind)
def colorize(str : String, kind : InfoKind) : Colorize::Object(String)
str.colorize(kind.color).toggle(@color)
end

def execute_examples
def execute_examples : Nil
log_setup
maybe_randomize
run_filters
root_context.run
end

def execute_list_tags
def execute_list_tags : Nil
run_filters
tag_counts = collect_tags(root_context)
print_list_tags(tag_counts)
Expand Down Expand Up @@ -287,21 +287,21 @@ module Spec
end
end

def finish_run
def finish_run : Nil
elapsed_time = Time.monotonic - @start_time.not_nil!
root_context.finish(elapsed_time, @aborted)
exit 1 if !root_context.succeeded || @aborted || (focus? && ENV["SPEC_FOCUS_NO_FAIL"]? != "1")
end

# :nodoc:
def maybe_randomize
def maybe_randomize : Nil
if randomizer = @randomizer
root_context.randomize(randomizer)
end
end

# :nodoc:
def run_filters
def run_filters : Nil
root_context.run_filters(@pattern, @line, @locations, @split_filter, @focus, @tags, @anti_tags)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/spec/example.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Spec
end

# :nodoc:
def run
def run : Nil
@parent.cli.root_context.check_nesting_spec(file, line) do
@parent.cli.formatters.each(&.before_example(description))

Expand Down
2 changes: 1 addition & 1 deletion src/spec/example/procsy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Spec

# Executes the wrapped example, possibly executing other
# `around_each` hooks before that.
def run
def run : Nil
@proc.call
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/spec/example_group/procsy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Spec

# Executes the wrapped example group, possibly executing other
# `around_all` hooks before that.
def run
def run : Nil
@proc.call
end
end
Expand Down
56 changes: 28 additions & 28 deletions src/spec/expectations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,45 +85,45 @@ module Spec

# :nodoc:
struct BeTruthyExpectation
def match(actual_value)
def match(actual_value) : Bool
!!actual_value
end

def failure_message(actual_value)
def failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} to be truthy"
end

def negative_failure_message(actual_value)
def negative_failure_message(actual_value : Bool?) : String
"Expected: #{actual_value.pretty_inspect} not to be truthy"
Comment on lines +96 to 97
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the message is correct and this accept truthy values, then Bool? is too restrictive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yup, good catch

end
end

# :nodoc:
struct BeFalseyExpectation
def match(actual_value)
def match(actual_value) : Bool
!actual_value
end

def failure_message(actual_value)
def failure_message(actual_value : Bool?) : String
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

"Expected: #{actual_value.pretty_inspect} to be falsey"
end

def negative_failure_message(actual_value)
def negative_failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} not to be falsey"
end
end

# :nodoc:
struct BeNilExpectation
def match(actual_value)
def match(actual_value) : Bool
actual_value.nil?
end

def failure_message(actual_value)
def failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} to be nil"
end

def negative_failure_message(actual_value)
def negative_failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} not to be nil"
end
end
Expand Down Expand Up @@ -285,15 +285,15 @@ module Spec

# :nodoc:
struct BeEmptyExpectation
def match(actual_value)
def match(actual_value) : Bool
actual_value.empty?
end

def failure_message(actual_value)
def failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} to be empty"
end

def negative_failure_message(actual_value)
def negative_failure_message(actual_value) : String
"Expected: #{actual_value.pretty_inspect} not to be empty"
end
end
Expand All @@ -314,27 +314,27 @@ module Spec
end

# Creates an `Expectation` that passes if actual is true (`== true`).
def be_true
def be_true : Spec::EqualExpectation(Bool)
eq true
end

# Creates an `Expectation` that passes if actual is false (`== false`).
def be_false
def be_false : Spec::EqualExpectation(Bool)
eq false
end

# Creates an `Expectation` that passes if actual is truthy (neither `nil` nor `false`).
def be_truthy
def be_truthy : Spec::BeTruthyExpectation
Spec::BeTruthyExpectation.new
end

# Creates an `Expectation` that passes if actual is falsy (`nil` or `false`).
def be_falsey
def be_falsey : Spec::BeFalseyExpectation
Spec::BeFalseyExpectation.new
end

# Creates an `Expectation` that passes if actual is nil (`Object#nil?`).
def be_nil
def be_nil : Spec::BeNilExpectation
Spec::BeNilExpectation.new
end

Expand All @@ -349,35 +349,35 @@ module Spec
# * passes if actual is lesser than or equal *value*: `be <= value`
# * passes if actual is greater than *value*: `be > value`
# * passes if actual is greater than or equal *value*: `be >= value`
def be
def be : Spec::Be.class
Spec::Be
end

# Creates an `Expectation` that passes if actual matches *value* (`=~`).
def match(value)
def match(value : T) : Spec::MatchExpectation(T) forall T
Spec::MatchExpectation.new(value)
end

# Creates an `Expectation` that passes if actual includes *expected* (`.includes?`).
# Works on collections and `String`.
def contain(expected)
def contain(expected : T) : Spec::ContainExpectation(T) forall T
Spec::ContainExpectation.new(expected)
end

# Creates an `Expectation` that passes if actual starts with *expected* (`.starts_with?`).
# Works on `String`.
def start_with(expected)
def start_with(expected : T) : Spec::StartWithExpectation(T) forall T
Spec::StartWithExpectation.new(expected)
end

# Creates an `Expectation` that passes if actual ends with *expected* (`.ends_with?`).
# Works on `String`.
def end_with(expected)
def end_with(expected : T) : Spec::EndWithExpectation(T) forall T
Spec::EndWithExpectation.new(expected)
end

# Creates an `Expectation` that passes if actual is empty (`.empty?`).
def be_empty
def be_empty : Spec::BeEmptyExpectation
Spec::BeEmptyExpectation.new
end

Expand Down Expand Up @@ -452,7 +452,7 @@ module Spec
# ```
#
# See `Spec::Expectations` for available expectations.
def should(expectation : BeAExpectation(T), failure_message : String? = nil, *, file = __FILE__, line = __LINE__) : T forall T
def should(expectation : BeAExpectation(T), failure_message : String? = nil, *, file : String = __FILE__, line : Int32 = __LINE__) : T forall T
if expectation.match self
self.is_a?(T) ? self : (raise "Bug: expected #{self} to be a #{T}")
else
Expand All @@ -464,7 +464,7 @@ module Spec
# Validates an expectation and fails the example if it does not match.
#
# See `Spec::Expectations` for available expectations.
def should(expectation, failure_message : String? = nil, *, file = __FILE__, line = __LINE__)
def should(expectation, failure_message : String? = nil, *, file : String = __FILE__, line : Int32 = __LINE__) : Nil
unless expectation.match self
failure_message ||= expectation.failure_message(self)
fail(failure_message, file, line)
Expand All @@ -484,7 +484,7 @@ module Spec
# ```
#
# See `Spec::Expectations` for available expectations.
def should_not(expectation : BeAExpectation(T), failure_message : String? = nil, *, file = __FILE__, line = __LINE__) forall T
def should_not(expectation : BeAExpectation(T), failure_message : String? = nil, *, file : String = __FILE__, line : Int32 = __LINE__) forall T
if expectation.match self
failure_message ||= expectation.negative_failure_message(self)
fail(failure_message, file, line)
Expand All @@ -505,7 +505,7 @@ module Spec
# ```
#
# See `Spec::Expectations` for available expectations.
def should_not(expectation : BeNilExpectation, failure_message : String? = nil, *, file = __FILE__, line = __LINE__)
def should_not(expectation : BeNilExpectation, failure_message : String? = nil, *, file : String = __FILE__, line : Int32 = __LINE__)
if expectation.match self
failure_message ||= expectation.negative_failure_message(self)
fail(failure_message, file, line)
Expand All @@ -517,7 +517,7 @@ module Spec
# Validates an expectation and fails the example if it matches.
#
# See `Spec::Expectations` for available expectations.
def should_not(expectation, failure_message : String? = nil, *, file = __FILE__, line = __LINE__)
def should_not(expectation, failure_message : String? = nil, *, file : String = __FILE__, line : Int32 = __LINE__) : Nil
if expectation.match self
failure_message ||= expectation.negative_failure_message(self)
fail(failure_message, file, line)
Expand Down
Loading