File: type_spec.rb

package info (click to toggle)
ruby-dbus 0.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 776 kB
  • sloc: ruby: 6,584; xml: 225; sh: 38; makefile: 8
file content (227 lines) | stat: -rwxr-xr-x 6,223 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env rspec
# frozen_string_literal: true

require_relative "spec_helper"
require "dbus"

describe DBus do
  describe ".type" do
    good = [
      "i",
      "ai",
      "a(ii)",
      "aai"
    ]

    context "valid single types" do
      good.each do |s|
        it "#{s.inspect} is parsed" do
          expect(DBus.type(s).to_s).to eq(s)
        end
      end
    end

    bad = [
      ["\x00", "Unknown type code"],
      ["!", "Unknown type code"],

      # ARRAY related
      ["a", "Empty ARRAY"],
      ["aa", "Empty ARRAY"],

      # STRUCT related
      ["r", "Abstract STRUCT"],
      ["()", "Empty STRUCT"],
      ["(ii", "STRUCT not closed"],
      ["a{i)", "STRUCT unexpectedly closed"],

      # TODO: deep nesting arrays, structs, combined

      # DICT_ENTRY related
      ["e", "Abstract DICT_ENTRY"],
      ["a{}", "DICT_ENTRY must have 2 subtypes, found 0"],
      ["a{s}", "DICT_ENTRY must have 2 subtypes, found 1"],
      ["a{sss}", "DICT_ENTRY must have 2 subtypes, found 3"],
      ["a{vs}", "DICT_ENTRY key must be basic (non-container)"],
      ["{sv}", "DICT_ENTRY not an immediate child of an ARRAY"],
      ["a({sv})", "DICT_ENTRY not an immediate child of an ARRAY"],
      ["a{s", "DICT_ENTRY not closed"],
      ["a{sv", "DICT_ENTRY not closed"],
      ["}", "DICT_ENTRY unexpectedly closed"],

      # Too long
      ["(#{"y" * 254})", "longer than 255"],

      # not Single Complete Types
      ["", "expecting a Single Complete Type"],
      ["ii", "more than a Single Complete Type"]
    ]
    context "invalid single types" do
      bad.each.each do |s, msg|
        it "#{s.inspect} raises an exception mentioning: #{msg}" do
          rx = Regexp.new(Regexp.quote(msg))
          expect { DBus.type(s) }.to raise_error(DBus::Type::SignatureException, rx)
        end
      end
    end
  end

  describe ".types" do
    good = [
      "",
      "ii"
    ]

    context "valid signatures" do
      good.each do |s|
        it "#{s.inspect} is parsed" do
          expect(DBus.types(s).map(&:to_s).join).to eq(s)
        end
      end
    end
  end

  describe DBus::Type do
    let(:as1) { DBus.type("as") }
    let(:as2) { DBus.type("as") }
    let(:aas) { DBus.type("aas") }

    describe "#==" do
      it "is true for same types" do
        expect(as1).to eq(as2)
      end

      it "is true for a type and its string representation" do
        expect(as1).to eq("as")
      end

      it "is false for different types" do
        expect(as1).to_not eq(aas)
      end

      it "is false for a type and a different string" do
        expect(as1).to_not eq("aas")
      end
    end

    describe "#eql?" do
      it "is true for same types" do
        expect(as1).to eql(as2)
      end

      it "is false for a type and its string representation" do
        expect(as1).to_not eql("as")
      end

      it "is false for different types" do
        expect(as1).to_not eql(aas)
      end

      it "is false for a type and a different string" do
        expect(as1).to_not eql("aas")
      end
    end

    describe "#<<" do
      it "raises if the argument is not a Type" do
        t = DBus::Type.new(DBus::Type::ARRAY)
        expect { t << "s" }.to raise_error(ArgumentError)
      end

      # TODO: the following raise checks do not occur in practice, as there are
      # parallel checks in the parses. The code could be simplified?
      it "raises if adding too much to an array" do
        t = DBus::Type.new(DBus::Type::ARRAY)
        b = DBus::Type.new(DBus::Type::BOOLEAN)
        t << b
        expect { t << b }.to raise_error(DBus::Type::SignatureException)
      end

      it "raises if adding too much to a dict_entry" do
        t = DBus::Type.new(DBus::Type::DICT_ENTRY, abstract: true)
        b = DBus::Type.new(DBus::Type::BOOLEAN)
        t << b
        t << b
        expect { t << b }.to raise_error(DBus::Type::SignatureException)
      end

      it "raises if adding to a non-container" do
        t = DBus::Type.new(DBus::Type::STRING)
        b = DBus::Type.new(DBus::Type::BOOLEAN)
        expect { t << b }.to raise_error(DBus::Type::SignatureException)

        t = DBus::Type.new(DBus::Type::VARIANT)
        expect { t << b }.to raise_error(DBus::Type::SignatureException)
      end
    end

    describe DBus::Type::Array do
      describe ".[]" do
        it "takes Type argument" do
          t = DBus::Type::Array[DBus::Type.new("s")]
          expect(t.to_s).to eq "as"
        end

        it "takes 's':String argument" do
          t = DBus::Type::Array["s"]
          expect(t.to_s).to eq "as"
        end

        it "takes String:Class argument" do
          t = DBus::Type::Array[String]
          expect(t.to_s).to eq "as"
        end

        it "rejects Integer:Class argument" do
          expect { DBus::Type::Array[Integer] }.to raise_error(ArgumentError)
        end

        it "rejects /./:Regexp argument" do
          expect { DBus::Type::Array[/./] }.to raise_error(ArgumentError)
        end
      end
    end

    describe DBus::Type::Hash do
      describe ".[]" do
        it "takes Type arguments" do
          t = DBus::Type::Hash[DBus::Type.new("s"), DBus::Type.new("v")]
          expect(t.to_s).to eq "a{sv}"
        end

        it "takes 's':String arguments" do
          t = DBus::Type::Hash["s", "v"]
          expect(t.to_s).to eq "a{sv}"
        end

        it "takes String:Class argument" do
          t = DBus::Type::Hash[String, DBus::Type::VARIANT]
          expect(t.to_s).to eq "a{sv}"
        end
      end
    end

    describe DBus::Type::Struct do
      describe ".[]" do
        it "takes Type arguments" do
          t = DBus::Type::Struct[DBus::Type.new("s"), DBus::Type.new("v")]
          expect(t.to_s).to eq "(sv)"
        end

        it "takes 's':String arguments" do
          t = DBus::Type::Struct["s", "v"]
          expect(t.to_s).to eq "(sv)"
        end

        it "takes String:Class argument" do
          t = DBus::Type::Struct[String, DBus::Type::VARIANT]
          expect(t.to_s).to eq "(sv)"
        end

        it "raises on no arguments" do
          expect { DBus::Type::Struct[] }.to raise_error(ArgumentError)
        end
      end
    end
  end
end