File: box_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 (63 lines) | stat: -rw-r--r-- 1,362 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "spec"

describe "Box" do
  it "boxes and unboxes" do
    a = 1
    box = Box.box(a)
    Box(Int32).unbox(box).should eq(1)
  end

  it "boxing a reference returns the same pointer" do
    a = "foo"
    box = Box.box(a)
    box.address.should eq(a.object_id)

    Box(String).unbox(box).should be(a)
  end

  it "boxing a nilable reference returns the same pointer" do
    a = "foo".as(String?)
    box = Box.box(a)
    box.address.should eq(a.object_id)

    b = Box(String?).unbox(box)
    b.should be_a(String)
    b.should be(a)
  end

  it "boxing a nilable value returns the same value" do
    a = 1.as(Int32?)
    box = Box.box(a)

    b = Box(Int32?).unbox(box)
    b.should be_a(Int32)
    b.should eq(a)
  end

  it "boxes with explicit type" do
    box = Box(Int32?).box(1)
    b = Box(Int32?).unbox(box)
    b.should be_a(Int32)
    b.should eq(1)
  end

  it "boxing nil returns a null pointer" do
    box = Box.box(nil)
    box.address.should eq(0)

    Box(Nil).unbox(box).should be_nil
  end

  it "boxing nil in a reference-like union returns a null pointer (#11839)" do
    box = Box.box(nil.as(String?))
    box.address.should eq(0)

    Box(String?).unbox(box).should be_nil
  end

  it "boxing nil in a value-like union doesn't crash (#11839)" do
    box = Box.box(nil.as(Int32?))

    Box(Int32?).unbox(box).should be_nil
  end
end