File: class_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 (171 lines) | stat: -rw-r--r-- 4,331 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require "spec"

private class A
end

private class B1 < A
end

private class C1 < B1
end

private class B2 < A
end

private class ClassWithRedefinedName
  def self.name
    "OtherName"
  end
end

private alias RecursiveNilableType = Array(RecursiveNilableType)?

describe Class do
  it "does ===" do
    (Int32 === 1).should be_true
    (Int32 === 1.5).should be_false
    (Array === [1]).should be_true
    (Array(Int32) === [1]).should be_true
    (Array(Int32) === ['a']).should be_false
    (Int32.class === 1).should be_false
    (Int32.class === 1.5).should be_false
    (Int32.class === Int32).should be_true
    (Int32.class === Array).should be_true
  end

  it "casts, allowing the class to be passed in at runtime" do
    ar = [99, "something"]
    cl = {Int32, String}
    casted = {cl[0].cast(ar[0]), cl[1].cast(ar[1])}
    casted.should eq({99, "something"})
    typeof(casted[0]).should eq(Int32)
    typeof(casted[1]).should eq(String)
  end

  it "does |" do
    (Int32 | Char).should eq(typeof(1, 'a'))
    (Int32 | Char | Float64).should eq(typeof(1, 'a', 1.0))
  end

  it "dups" do
    Int32.dup.should eq(Int32)
  end

  it "clones" do
    Int32.clone.should eq(Int32)
  end

  it "#nilable?" do
    Int32.nilable?.should be_false
    Nil.nilable?.should be_true
    (Int32 | String).nilable?.should be_false
    Int32?.nilable?.should be_true
    NoReturn.nilable?.should be_false
    Reference.nilable?.should be_false
    Value.nilable?.should be_true
    Class.nilable?.should be_false
    Object.nilable?.should be_true
    RecursiveNilableType.nilable?.should be_true
  end

  it "does to_s" do
    Int32.to_s.should eq("Int32")
  end

  it "does to_s with name redefined (#7292)" do
    ClassWithRedefinedName.name.should eq("OtherName")
    ClassWithRedefinedName.to_s.should eq("ClassWithRedefinedName")
  end

  describe "comparison operators" do
    t = [A, B1, B2, C1]

    it "<" do
      (t[0] < t[0]).should be_false
      (t[0] < t[1]).should be_false
      (t[0] < t[2]).should be_false
      (t[0] < t[3]).should be_false

      (t[1] < t[0]).should be_true
      (t[1] < t[1]).should be_false
      (t[1] < t[2]).should be_false
      (t[1] < t[3]).should be_false

      (t[2] < t[0]).should be_true
      (t[2] < t[1]).should be_false
      (t[2] < t[2]).should be_false
      (t[2] < t[3]).should be_false

      (t[3] < t[0]).should be_true
      (t[3] < t[1]).should be_true
      (t[3] < t[2]).should be_false
      (t[3] < t[3]).should be_false
    end

    it "<=" do
      (t[0] <= t[0]).should be_true
      (t[0] <= t[1]).should be_false
      (t[0] <= t[2]).should be_false
      (t[0] <= t[3]).should be_false

      (t[1] <= t[0]).should be_true
      (t[1] <= t[1]).should be_true
      (t[1] <= t[2]).should be_false
      (t[1] <= t[3]).should be_false

      (t[2] <= t[0]).should be_true
      (t[2] <= t[1]).should be_false
      (t[2] <= t[2]).should be_true
      (t[2] <= t[3]).should be_false

      (t[3] <= t[0]).should be_true
      (t[3] <= t[1]).should be_true
      (t[3] <= t[2]).should be_false
      (t[3] <= t[3]).should be_true
    end

    it ">" do
      (t[0] > t[0]).should be_false
      (t[0] > t[1]).should be_true
      (t[0] > t[2]).should be_true
      (t[0] > t[3]).should be_true

      (t[1] > t[0]).should be_false
      (t[1] > t[1]).should be_false
      (t[1] > t[2]).should be_false
      (t[1] > t[3]).should be_true

      (t[2] > t[0]).should be_false
      (t[2] > t[1]).should be_false
      (t[2] > t[2]).should be_false
      (t[2] > t[3]).should be_false

      (t[3] > t[0]).should be_false
      (t[3] > t[1]).should be_false
      (t[3] > t[2]).should be_false
      (t[3] > t[3]).should be_false
    end

    it ">=" do
      (t[0] >= t[0]).should be_true
      (t[0] >= t[1]).should be_true
      (t[0] >= t[2]).should be_true
      (t[0] >= t[3]).should be_true

      (t[1] >= t[0]).should be_false
      (t[1] >= t[1]).should be_true
      (t[1] >= t[2]).should be_false
      (t[1] >= t[3]).should be_true

      (t[2] >= t[0]).should be_false
      (t[2] >= t[1]).should be_false
      (t[2] >= t[2]).should be_true
      (t[2] >= t[3]).should be_false

      (t[3] >= t[0]).should be_false
      (t[3] >= t[1]).should be_false
      (t[3] >= t[2]).should be_false
      (t[3] >= t[3]).should be_true
    end
  end
end