Testing compile time errors

Just to complete this topic. This is what i ended up doing:

def assert_error(path : String, message : String) : Nil
  buffer = IO::Memory.new
  result = Process.run("crystal", ["run", "--no-color", "--no-codegen", "spec/" + path], error: buffer)
  result.success?.should be_false
  buffer.to_s.should contain message
  buffer.close
end

Where i created separate files for each assertion, like:

require "../../../routing_spec_helper"

class CompileController < Athena::Routing::Controller
  @[Athena::Routing::Get(path: "int8/")]
  def no_return_type
    123
  end
end

Athena::Routing.run

Then used it

    describe "without a return type" do
      it "should not compile" do
        assert_error "routing/compiler/actions/no_return_type.cr", "Route action return type must be set for 'CompileController.no_return_type'"
      end
    end

EDIT: I created GitHub - athena-framework/spec: Common/helpful Spec compliant testing utilities that includes this among other additional testing utilities.

5 Likes