module

Athena::Spec::Methods

Namespace for common/helpful testing methods.

This module can be included into your spec_helper in order to allow your specs to use them all. This module is also included into ASPEC::TestCase by default to allow using them within your unit tests as well.

May be reopened to add additional application specific helpers.

Instance methods

assert_compile_time_error(message : String, code : String, *, preamble : String = "", postamble : String = "", line : Int32 = __LINE__, file : String = __FILE__) : Nil

Executes the provided Crystal code and asserts it results in a compile time error with the provided message.

ASPEC::Methods.assert_compile_time_error "can't instantiate abstract class Foo", <<-CR
  abstract class Foo; end
  Foo.new
CR

The preamble and postamble parameters may be used to add code before/after the actual code. This is primarily useful when wrapping this method in another private method for use within a specific test file to share common before/after code.

private def assert_compiles(message : String, code : String, *, line : Int32 = __LINE__) : Nil
  ASPEC::Methods.assert_compile_time_error message, code, preamble: %(require "../spec_helper.cr"), postamble: "MyClass.new", line: line
end

NOTE: When files are required within the code, they are relative to the file calling this method.

Source
assert_compiles(code : String, *, preamble : String = "", postamble : String = "", line : Int32 = __LINE__, file : String = __FILE__) : Nil

Similar to .assert_compile_time_error, but asserts the provided Crystal code successfully compiles.

ASPEC::Methods.assert_compiles <<-CR
  raise "Still passes"
CR

The preamble and postamble parameters may be used to add code before/after the actual code. This is primarily useful when wrapping this method in another private method for use within a specific test file to share common before/after code.

private def assert_compiles(code : String, *, line : Int32 = __LINE__) : Nil
  ASPEC::Methods.assert_compiles code, preamble: %(require "../spec_helper.cr"), postamble: "MyClass.new", line: line
end

NOTE: When files are required within the code, they are relative to the file calling this method.

Source
assert_executes(code : String, *, line : Int32 = __LINE__, file : String = __FILE__) : Nil

Similar to .assert_runtime_error, but asserts the provided Crystal code successfully executes.

ASPEC::Methods.assert_executes <<-CR
  puts 2 + 2
CR

NOTE: When files are required within the code, they are relative to the file calling this method.

Source
assert_runtime_error(message : String, code : String, *, line : Int32 = __LINE__, file : String = __FILE__) : Nil

Executes the provided Crystal code and asserts it results in a runtime error with the provided message. This can be helpful in order to test something in isolation, without affecting other test cases.

ASPEC::Methods.assert_runtime_error "Oh noes!", <<-CR
 raise "Oh noes!"
CR

NOTE: When files are required within the code, they are relative to the file calling this method.

Source
run_executable(path : String, args : Array(String) = [] of String, & : String, String, Process::Status -> ) : Nil

Runs the executable at the given path, optionally with the provided args.

The standard output, error output, and status of the execution are yielded.

require "athena-spec"

ASPEC::Methods.run_executable "/usr/bin/ls" do |output, error, status|
  output # => "docs\n" + "LICENSE\n" + "README.md\n" + "shard.yml\n" + "spec\n" + "src\n"
  error  # => ""
  status # => #<Process::Status:0x7f7bc9befb70 @exit_status=0>
end
Source
run_executable(path : String, input : IO, args : Array(String) = [] of String, & : String, String, Process::Status -> ) : Nil

Runs the executable at the given path, with the given input, optionally with the provided args.

The standard output, error output, and status of the execution are yielded.

require "athena-spec"

input = IO::Memory.new %({"id":1})

ASPEC::Methods.run_executable "jq", input, [".", "-c"] do |output, error, status|
  output # => "{\"id\":1}\n"
  error  # => ""
  status # => #<Process::Status:0x7f26ec698b70 @exit_status=0>
end

invalid_input = IO::Memory.new %({"id"1})

ASPEC::Methods.run_executable "jq", invalid_input, [".", "-c"] do |output, error, status|
  output # => ""
  error  # => "parse error: Expected separator between values at line 1, column 7\n"
  status # => #<Process::Status:0x7f0217496900 @exit_status=1024>
end
Source