mirror of
https://github.com/vidarh/ruby-x11
synced 2024-12-27 09:59:21 +01:00
ad10e78306
I decided to ditch the Packet class name and go with a Form module that contains a BaseForm which child classes inherit from. I've created a few tests so you can see how the BaseForm class works and how packets can be read and sent over a socket to an X11 server
66 lines
1.4 KiB
Ruby
66 lines
1.4 KiB
Ruby
require File.expand_path('../helper', __FILE__)
|
|
|
|
class MockSocket
|
|
def initialize(packet)
|
|
@packet = packet
|
|
end
|
|
|
|
def read(amount)
|
|
@packet.slice!(0..amount-1)
|
|
end
|
|
end
|
|
|
|
class Point < X11::Form::BaseForm
|
|
field :x, Int8
|
|
field :y, Int8
|
|
end
|
|
|
|
class Child < X11::Form::BaseForm
|
|
field :name, Uint16, :length
|
|
field :name, String8, :string
|
|
end
|
|
|
|
class Parent < X11::Form::BaseForm
|
|
field :value, Uint8
|
|
field :point, Point
|
|
|
|
field :name, Uint16, :length
|
|
field :name, String8, :string
|
|
|
|
field :children, Uint16, :length
|
|
field :children, Child, :list
|
|
end
|
|
|
|
describe X11::Form::BaseForm do
|
|
it "setters and getters on form should work" do
|
|
|
|
# we can create partial form objects
|
|
# without specifying all parameters.
|
|
parent = Parent.new(1337)
|
|
|
|
# To fill in the rest of the parameters
|
|
# we use attr_accessors
|
|
parent.name = "Parent Form"
|
|
parent.point = Point.new(0,0)
|
|
|
|
parent.point.must_be_instance_of Point
|
|
parent.name.must_equal "Parent Form"
|
|
|
|
parent.children = []
|
|
parent.children << Child.new
|
|
parent.children << Child.new
|
|
|
|
assert_equal parent.children.size, 2
|
|
end
|
|
|
|
it "should encode/decode a packet" do
|
|
parent = Parent.new(255,Point.new(23,17), "Parent Form", [])
|
|
socket = MockSocket.new(parent.to_packet)
|
|
|
|
decoded = Parent.from_packet(socket)
|
|
decoded.value.must_equal 255
|
|
decoded.name.must_equal "Parent Form"
|
|
decoded.point.x.must_equal 23
|
|
decoded.point.y.must_equal 17
|
|
end
|
|
end
|