File: binding_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 (76 lines) | stat: -rwxr-xr-x 2,515 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
#!/usr/bin/env rspec
# frozen_string_literal: true

# Test the binding of dbus concepts to ruby concepts
require_relative "spec_helper"

require "dbus"

describe "BindingTest" do
  before(:each) do
    @bus = DBus::ASessionBus.new
    @svc = @bus.service("org.ruby.service")
    @base = @svc.object "/org/ruby/MyInstance"
    @base.default_iface = "org.ruby.SampleInterface"
  end

  # https://trac.luon.net/ruby-dbus/ticket/36#comment:3
  it "tests class inheritance" do
    derived = @svc.object "/org/ruby/MyDerivedInstance"

    # it should inherit from the parent
    expect(derived["org.ruby.SampleInterface"]).not_to be_nil
  end

  # https://trac.luon.net/ruby-dbus/ticket/36
  # Interfaces and methods/signals appeared on all classes
  it "tests separation of classes" do
    test2 = @svc.object "/org/ruby/MyInstance2"

    # it should have its own interface
    expect(test2["org.ruby.Test2"]).not_to be_nil
    # but not an interface of the Test class
    expect { test2["org.ruby.SampleInterface"] }.to raise_error(DBus::Error) do |e|
      expect(e.message).to match(/no such interface/)
    end

    # and the parent should not get polluted by the child
    expect { @base["org.ruby.Test2"] }.to raise_error(DBus::Error) do |e|
      expect(e.message).to match(/no such interface/)
    end
  end

  it "tests translating errors into exceptions" do
    # this is a generic call that will reply with the specified error
    expect { @base.Error "org.example.Fail", "as you wish" }.to raise_error(DBus::Error) do |e|
      expect(e.name).to eq("org.example.Fail")
      expect(e.message).to match(/as you wish/)
    end
  end

  it "tests generic dbus error" do
    # this is a generic call that will reply with the specified error
    expect { @base.will_raise_error_failed }.to raise_error(DBus::Error) do |e|
      expect(e.name).to eq("org.freedesktop.DBus.Error.Failed")
      expect(e.message).to match(/failed as designed/)
    end
  end

  it "tests dynamic interface definition" do
    # interfaces can be defined dynamicaly
    derived = DBus::Object.new "/org/ruby/MyDerivedInstance"

    # define a new interface
    derived.singleton_class.instance_eval do
      dbus_interface "org.ruby.DynamicInterface" do
        dbus_method :hello2, "in name:s, in name2:s" do |name, name2|
          puts "hello(#{name}, #{name2})"
        end
      end
    end

    # the object should have the new iface
    ifaces = derived.intfs
    expect(ifaces).to include "org.ruby.DynamicInterface"
  end
end