File: struct_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 (90 lines) | stat: -rw-r--r-- 1,738 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "./spec_helper"
require "big"

private module StructSpec
  struct TestClass
    @x : Int32
    @y : String

    def initialize(@x, @y)
    end
  end

  struct BigIntWrapper
    @value : BigInt

    def initialize(@value : BigInt)
    end
  end

  struct DupCloneStruct
    property x, y

    def initialize
      @x = 1
      @y = [1, 2, 3]
    end

    def_clone
  end

  abstract struct GeneralStruct
  end

  struct FooStruct < GeneralStruct
  end

  struct BarStruct < GeneralStruct
  end
end

describe "Struct" do
  it "does to_s" do
    s = StructSpec::TestClass.new(1, "hello")
    s.to_s.should eq(%(StructSpec::TestClass(@x=1, @y="hello")))
  end

  it "does ==" do
    s = StructSpec::TestClass.new(1, "hello")
    s.should eq(s)
  end

  it "does hash" do
    s = StructSpec::TestClass.new(1, "hello")
    s.hash.should eq(s.dup.hash)
  end

  it "does hash for struct wrapper (#1940)" do
    s = StructSpec::BigIntWrapper.new(BigInt.new(0))
    s.hash.should eq(s.dup.hash)
  end

  it "does dup" do
    original = StructSpec::DupCloneStruct.new
    duplicate = original.dup
    duplicate.x.should eq(original.x)
    duplicate.y.should be(original.y)

    original.x = 10
    duplicate.x.should_not eq(10)
  end

  it "clones with def_clone" do
    original = StructSpec::DupCloneStruct.new
    clone = original.clone
    clone.x.should eq(original.x)
    clone.y.should_not be(original.y)
    clone.y.should eq(original.y)

    original.x = 10
    clone.x.should_not eq(10)
  end

  it "should retrieve multiple descendants from hashed data structure" do
    foo = StructSpec::FooStruct.new
    bar = StructSpec::BarStruct.new
    set = Set{foo, bar}
    set.should contain(foo)
    set.should contain(bar)
  end
end