Skip to content

Commit

Permalink
added console and builder for message using tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aberant committed Aug 19, 2009
1 parent 037d24f commit 64bc399
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 43 deletions.
2 changes: 2 additions & 0 deletions script/console
@@ -0,0 +1,2 @@
#!/bin/bash
irb -r lib/osc-ruby.rb
40 changes: 40 additions & 0 deletions spec/builders/message_builder.rb
@@ -0,0 +1,40 @@
class MessageBuilder

def initialize
@address = ""
@tags = []
@values = []
end

def with_address( addr )
@address = addr
self
end

def with_float( float )
with_arg( "f", float )
self
end

def with_int( int )
with_arg( "i", int )
self
end

def with_string( string )
with_arg( "s", string )
self
end


def build
OSC::Message.new( @address , @tags.join, *@values)
end

private

def with_arg( tag, value )
@tags << tag
@values << value
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,8 +3,10 @@
require 'rr' require 'rr'


$:.unshift( File.join( File.dirname( __FILE__), '..', 'lib' ) ) $:.unshift( File.join( File.dirname( __FILE__), '..', 'lib' ) )
$:.unshift( File.dirname( __FILE__ ) )


require 'osc-ruby' require 'osc-ruby'
require 'builders/message_builder'


Spec::Runner.configure do |config| Spec::Runner.configure do |config|
config.mock_with RR::Adapters::Rspec config.mock_with RR::Adapters::Rspec
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/message_spec.rb
@@ -0,0 +1,9 @@
# some test data for when i unit test this
# @simple_packet = "/hi\000,\000\000\000"
# @simple_with_integer_arg = "/hi\000,i\000\000\000\000\000*"
# @simple_with_two_integer_args = "/hi\000,ii\000\000\000\000*\000\000\000!"
# @simple_with_float_arg = "/hi\000,f\000\000B(\n="
# @simple_with_two_float_args = "/hi\000,ff\000B(\n=B\004\n="
# @simple_with_string_arg = "/hi\000,s\000\000greetings\000\000\000"
# @simple_with_two_string_args = "/hi\000,ss\000greetings\000\000\000how are you?\000\000\000\000"
# @simple_with_int_float_string = "/hi\000,ifs\000\000\000\000\000\000\000*B\004\n=greetings\000\000\000"
105 changes: 62 additions & 43 deletions spec/unit/osc_simple_packets_spec.rb
Expand Up @@ -3,74 +3,93 @@


describe OSC::OSCPacket do describe OSC::OSCPacket do
before :each do before :each do
@simple_packet = "/hi\000,\000\000\000" @address = "/hi"
@simple_with_integer_arg = "/hi\000,i\000\000\000\000\000*" @first_int = 42
@simple_with_two_integer_args = "/hi\000,ii\000\000\000\000*\000\000\000!" @second_int = 33
@simple_with_float_arg = "/hi\000,f\000\000B(\n="
@simple_with_two_float_args = "/hi\000,ff\000B(\n=B\004\n=" @first_float = 42.01
@simple_with_string_arg = "/hi\000,s\000\000greetings\000\000\000" @second_float = 33.01
@simple_with_two_string_args = "/hi\000,ss\000greetings\000\000\000how are you?\000\000\000\000"
@simple_with_int_float_string = "/hi\000,ifs\000\000\000\000\000\000\000*B\004\n=greetings\000\000\000" @first_string = "greetings"
@second_string = "how are you?"

@builder = MessageBuilder.new
@builder.with_address( @address )
end end


it "should decode the address of a simple message from the network data" do it "should decode the address of a simple message from the network data" do
messages = OSC::OSCPacket.messages_from_network( @simple_packet ) sent_msg = @builder.build
messages.should have( 1 ).item
messages.first.should eql( OSC::Message.new( "/hi" ) ) msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )

msg.first.address.should == @address
end end


it "should decode the address and int arg of a simple message from the network data" do it "should decode the int arg of a simple message from the network data" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_integer_arg ) sent_msg = @builder.with_int( @first_int ).build
messages.should have( 1 ).item
messages.first.should eql( OSC::Message.new( "/hi", "i", 42 ) ) msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )

msg.first.to_a.should == [@first_int]
end end


it "should decode the address and two int args" do it "should decode two int args" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_two_integer_args ) sent_msg = @builder.with_int( @first_int ).with_int( @second_int ).build
messages.should have( 1 ).item
messages.first.should eql( OSC::Message.new( "/hi", "ii", 42, 33 ) ) msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )

msg.first.to_a.should == [@first_int, @second_int]
end end


it "shold decode address with float arg" do it "shold decode address with float arg" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_float_arg ) sent_msg = @builder.with_float( @first_float ).build
messages.should have( 1 ).item
message = messages.first


message.address.should eql( "/hi" ) msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )
message.to_a.first.should be_close(42.01, 0.001)
msg.first.to_a[0].should be_close( @first_float, 0.001 )
end end



it "shold decode address with two float args" do it "shold decode address with two float args" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_two_float_args ) sent_msg = @builder.with_float( @first_float ).with_float( @second_float).build
messages.should have( 1 ).item
message = messages.first msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )


message.address.should eql( "/hi" ) args = msg.first.to_a
args = message.to_a args.first.should be_close( @first_float, 0.001 )
args.first.should be_close( 42.01, 0.001 ) args[1].should be_close( @second_float, 0.0001 )
args[1].should be_close( 33.01, 0.0001 )
end end


it "should decode address with string arg" do it "should decode address with string arg" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_string_arg ) sent_msg = @builder.with_string( @first_string ).build

msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )


messages.first.should eql( OSC::Message.new( "/hi", "ss", "greetings" ) ) msg.first.to_a.should == [@first_string]
end end


it "should decode address with string arg" do it "should decode address with multiple string args" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_two_string_args ) sent_msg = @builder.with_string( @first_string ).with_string( @second_string).build
msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )


messages.first.should eql( OSC::Message.new( "/hi", "ss", "greetings", "how are you?" ) ) args = msg.first.to_a
args[0].should == @first_string
args[1].should == @second_string
end end



it "should decode messages with three different types of args" do it "should decode messages with three different types of args" do
messages = OSC::OSCPacket.messages_from_network( @simple_with_int_float_string ) sent_msg = @builder.with_int( @first_int ).
args = messages.first.to_a with_float( @second_float ).
with_string( @first_string ).
build

msg = OSC::OSCPacket.messages_from_network( sent_msg.encode )


args.should have(3).items args = msg.first.to_a


args[0].should eql( 42 ) args[0].should eql( @first_int )
args[1].should be_close( 33.01, 0.0001 ) args[1].should be_close( @second_float, 0.0001 )
args[2].should eql( "greetings" ) args[2].should eql( @first_string )
end end
end end

0 comments on commit 64bc399

Please sign in to comment.