File: exception_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (48 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require "./spec_helper"

private class FooError < Exception
  def message
    "#{super || ""} -- bar!"
  end
end

describe "Exception" do
  it "allows subclassing #message" do
    ex = FooError.new("foo?")
    ex.message.should eq("foo? -- bar!")
    ex.to_s.should eq("foo? -- bar!")
    ex.inspect_with_backtrace.should contain("foo? -- bar!")
  end

  it "inspects" do
    ex = FooError.new("foo?")
    ex.inspect.should eq("#<FooError:foo? -- bar!>")
  end

  it "inspects with cause" do
    cause = Exception.new("inner")
    ex = expect_raises(Exception, "wrapper") do
      begin
        raise cause
      rescue ex
        raise Exception.new("wrapper", cause: ex)
      end
    end

    ex.cause.should be(cause)
    ex.inspect_with_backtrace.should contain("wrapper")
    ex.inspect_with_backtrace.should contain("Caused by")
    ex.inspect_with_backtrace.should contain("inner")
  end

  it "collect memory within ensure block", tags: %w[slow] do
    sample = datapath("collect_within_ensure")

    _, output, error = compile_and_run_file(sample, ["--release"])

    output.to_s.should be_empty
    error.to_s.should contain("Unhandled exception: Oh no! (Exception)")
    error.to_s.should_not contain("Invalid memory access")
    error.to_s.should_not contain("Illegal instruction")
  end
end