2018-09-27 21:49:34 +00:00
|
|
|
require "./spectator/includes"
|
2018-08-19 07:15:32 +00:00
|
|
|
|
2018-09-27 21:41:18 +00:00
|
|
|
# Module that contains all functionality related to Spectator.
|
2018-08-18 21:33:20 +00:00
|
|
|
module Spectator
|
2019-03-03 17:49:28 +00:00
|
|
|
extend self
|
|
|
|
|
2018-09-27 21:41:18 +00:00
|
|
|
# Current version of the Spectator library.
|
2018-08-18 21:33:20 +00:00
|
|
|
VERSION = "0.1.0"
|
|
|
|
|
2018-09-27 21:41:18 +00:00
|
|
|
# Top-level describe method.
|
|
|
|
# All specs in a file must be wrapped in this call.
|
|
|
|
# This takes an argument and a block.
|
|
|
|
# The argument is what your spec is describing.
|
|
|
|
# It can be any Crystal expression,
|
|
|
|
# but is typically a class name or feature string.
|
|
|
|
# The block should contain all of the specs for what is being described.
|
|
|
|
# Example:
|
|
|
|
# ```
|
|
|
|
# Spectator.describe Foo do
|
|
|
|
# # Your specs for `Foo` go here.
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
# NOTE: Inside the block, the `Spectator` prefix is no longer needed.
|
|
|
|
# Actually, prefixing methods and macros with `Spectator`
|
|
|
|
# most likely won't work and can cause compiler errors.
|
2018-09-23 01:41:56 +00:00
|
|
|
macro describe(what, &block)
|
2018-09-27 21:41:18 +00:00
|
|
|
# This macro creates the foundation for all specs.
|
|
|
|
# Every group of examples is defined a separate module - `SpectatorExamples`.
|
|
|
|
# There's multiple reasons for this.
|
|
|
|
#
|
|
|
|
# The first reason is to provide namespace isolation.
|
|
|
|
# We don't want the spec code to accidentally pickup types and values from the `Spectator` module.
|
|
|
|
# Another reason is that we need a root module to put all examples and groups in.
|
|
|
|
# And lastly, the spec DSL needs to be given to the block of code somehow.
|
|
|
|
# The DSL is included in the `SpectatorExamples` module.
|
|
|
|
#
|
|
|
|
# For more information on how the DSL works, see the `DSL` module.
|
|
|
|
|
|
|
|
# Root-level module that contains all examples and example groups.
|
2018-09-23 18:02:51 +00:00
|
|
|
module SpectatorExamples
|
2018-09-27 21:41:18 +00:00
|
|
|
# Include the DSL for creating groups, example, and more.
|
2018-09-23 18:02:51 +00:00
|
|
|
include ::Spectator::DSL::StructureDSL
|
|
|
|
|
2018-09-27 21:41:18 +00:00
|
|
|
# Pass off the "what" argument and block to `DSL::StructureDSL.describe`.
|
|
|
|
# That method will handle creating a new group for this spec.
|
2018-09-23 18:02:51 +00:00
|
|
|
describe({{what}}) {{block}}
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
|
2018-10-15 06:32:29 +00:00
|
|
|
# Flag indicating whether Spectator should automatically run tests.
|
|
|
|
# This should be left alone (set to true) in typical usage.
|
|
|
|
# There are times when Spectator shouldn't run tests.
|
|
|
|
# One of those is testing Spectator.
|
|
|
|
class_property? autorun = true
|
|
|
|
|
2018-09-27 21:41:18 +00:00
|
|
|
# All tests are ran just before the executable exits.
|
2018-10-15 06:32:29 +00:00
|
|
|
# Tests will be skipped, however, if `#autorun?` is set to false.
|
2018-09-27 21:41:18 +00:00
|
|
|
# There are a couple of reasons for this.
|
|
|
|
#
|
|
|
|
# First is that we want a clean interface for the end-user.
|
|
|
|
# They shouldn't need to call a "run" method.
|
|
|
|
# That adds the burden on the developer to ensure the tests are run after they are created.
|
|
|
|
# And that gets complicated when there are multiple files that could run in any order.
|
|
|
|
#
|
|
|
|
# Second is to allow all of the tests and framework to be constructed.
|
|
|
|
# We know that all of the instances and DSL builders have finished
|
|
|
|
# after the main part of the executable has run.
|
|
|
|
#
|
|
|
|
# By doing this, we provide a clean interface and safely run after everything is constructed.
|
|
|
|
# The downside, if something bad happens, like an exception is raised,
|
|
|
|
# Crystal doesn't display much information about what happened.
|
|
|
|
# That issue is handled by putting a begin/rescue block to show a custom error message.
|
2018-08-19 07:15:32 +00:00
|
|
|
at_exit do
|
2019-02-22 23:54:36 +00:00
|
|
|
# Run only if `#autorun?` is true.
|
|
|
|
# Return 1 on failure.
|
|
|
|
exit(1) if autorun? && !run
|
2018-10-15 06:32:29 +00:00
|
|
|
end
|
|
|
|
|
2018-12-13 20:50:59 +00:00
|
|
|
@@config_builder = ConfigBuilder.new
|
2019-03-24 00:35:15 +00:00
|
|
|
@@config : Config?
|
2019-03-24 01:18:29 +00:00
|
|
|
@@random : Random?
|
2018-12-13 20:50:59 +00:00
|
|
|
|
|
|
|
# Provides a means to configure how Spectator will run and report tests.
|
|
|
|
# A `ConfigBuilder` is yielded to allow changing the configuration.
|
|
|
|
# NOTE: The configuration set here can be overriden
|
|
|
|
# with a `.spectator` file and command-line arguments.
|
|
|
|
def configure : Nil
|
|
|
|
yield @@config_builder
|
|
|
|
end
|
|
|
|
|
2019-03-24 01:18:29 +00:00
|
|
|
# Random number generator for the test suite.
|
|
|
|
# All randomly generated values should be pulled from this.
|
|
|
|
# This provides reproducable results even though random values are used.
|
|
|
|
# The seed for this random generator is controlled by `ConfigBuilder.seed=`.
|
|
|
|
def random
|
|
|
|
@@random ||= Random.new(config.seed)
|
|
|
|
end
|
|
|
|
|
2018-10-15 06:32:29 +00:00
|
|
|
# Builds the tests and runs the framework.
|
2019-03-03 17:49:28 +00:00
|
|
|
private def run
|
2018-12-12 22:27:40 +00:00
|
|
|
# Build the test suite and run it.
|
|
|
|
suite = ::Spectator::DSL::Builder.build
|
2018-12-13 20:50:59 +00:00
|
|
|
Runner.new(suite, config).run
|
2018-10-15 06:32:29 +00:00
|
|
|
rescue ex
|
|
|
|
# Catch all unhandled exceptions here.
|
|
|
|
# Examples are already wrapped, so any exceptions they throw are caught.
|
|
|
|
# But if an exception occurs outside an example,
|
|
|
|
# it's likely the fault of the test framework (Spectator).
|
|
|
|
# So we display a helpful error that could be reported and return non-zero.
|
2018-12-27 22:47:29 +00:00
|
|
|
display_error_stack(ex)
|
2019-02-22 23:54:36 +00:00
|
|
|
false
|
2018-12-12 22:33:54 +00:00
|
|
|
end
|
|
|
|
|
2018-12-13 20:50:59 +00:00
|
|
|
# Processes and builds up a configuration to use for running tests.
|
2019-03-03 17:49:28 +00:00
|
|
|
private def config
|
2019-03-24 00:35:15 +00:00
|
|
|
@@config ||= build_config
|
|
|
|
end
|
|
|
|
|
|
|
|
# Builds the configuration.
|
|
|
|
private def build_config
|
2018-12-13 20:50:59 +00:00
|
|
|
# Build up the configuration from various sources.
|
|
|
|
# The sources that take priority are later in the list.
|
|
|
|
apply_config_file
|
|
|
|
apply_command_line_args
|
|
|
|
|
|
|
|
@@config_builder.build
|
|
|
|
end
|
|
|
|
|
|
|
|
# Path to the Spectator configuration file.
|
|
|
|
# The contents of this file should contain command-line arguments.
|
|
|
|
# Those arguments are automatically applied when Spectator starts.
|
|
|
|
# Arguments should be placed with one per line.
|
|
|
|
CONFIG_FILE_PATH = ".spectator"
|
|
|
|
|
|
|
|
# Loads configuration arguments from a file.
|
|
|
|
# The file is expected to be new-line delimited,
|
|
|
|
# one argument per line.
|
|
|
|
# The arguments are identical to those
|
|
|
|
# that would be passed on the command-line.
|
2019-03-03 17:49:28 +00:00
|
|
|
private def apply_config_file(file_path = CONFIG_FILE_PATH) : Nil
|
2018-12-13 20:50:59 +00:00
|
|
|
return unless File.exists?(file_path)
|
|
|
|
args = File.read(file_path).lines
|
|
|
|
CommandLineArgumentsConfigSource.new(args).apply_to(@@config_builder)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Applies configuration options from the command-line arguments
|
2019-03-03 17:49:28 +00:00
|
|
|
private def apply_command_line_args : Nil
|
2018-12-13 20:50:59 +00:00
|
|
|
CommandLineArgumentsConfigSource.new.apply_to(@@config_builder)
|
|
|
|
end
|
|
|
|
|
2018-12-27 22:47:29 +00:00
|
|
|
# Displays a complete error stack.
|
|
|
|
# Prints an error and everything that caused it.
|
|
|
|
# Stacktrace is included.
|
2019-03-03 17:49:28 +00:00
|
|
|
private def display_error_stack(error) : Nil
|
2018-10-15 06:32:29 +00:00
|
|
|
puts
|
|
|
|
puts "Encountered an unexpected error in framework"
|
2018-12-27 22:47:29 +00:00
|
|
|
# Loop while there's a cause for the error.
|
|
|
|
# Print each error in the stack.
|
|
|
|
loop do
|
|
|
|
display_error(error)
|
|
|
|
error = error.cause
|
|
|
|
break unless error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Display a single error and its stacktrace.
|
2019-03-03 17:49:28 +00:00
|
|
|
private def display_error(error) : Nil
|
2018-12-27 22:47:29 +00:00
|
|
|
puts
|
|
|
|
puts "Caused by: #{error.message}"
|
2018-12-12 22:33:54 +00:00
|
|
|
puts error.backtrace.join("\n")
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
2018-08-18 21:33:20 +00:00
|
|
|
end
|