Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/sphero/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ def response header, body
end

def packet_header
header.pack 'CCCCCC'
format = "C" * header.length
header.pack format
end

def packet_body
@data.pack @pattern
end

def checksum
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a one liner with bunch of things happening! The only reason that I changed it was having a few intermediate variables for more clarity and telling the reader what's going on a little bit more clear! (as Kent Beck mentions that having well-named intermediate variables during computation will make things a little bit more clear!) I don't know if you agree that it became a little bit more clear or not?! Let me know please, thanks!

~((packet_header + packet_body).unpack('C*').drop(2).reduce(:+) % 256) & 0xFF
unpacked_header_and_body = (packet_header + packet_body).unpack "C*"
dropped_sops = unpacked_header_and_body.drop 2
reduced = dropped_sops.reduce(:+) % 256
ones_complement = ~reduced
ones_complement & 0xFF
end

def bytes
Expand Down
49 changes: 49 additions & 0 deletions test/test_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'minitest/autorun'
require 'sphero/request'

class TestRequest < MiniTest::Unit::TestCase
STUB_REQUEST_ID = 0x05

def setup
@stub_request_class = Sphero::Request.
make_command Sphero::Request, STUB_REQUEST_ID
end

def test_request_checksum
stub_request = @stub_request_class.new 0x01, 1, 2
assert_equal 243, stub_request.checksum
end

def test_packet_body
auto_reconnect = @stub_request_class.new 0x01, 0, 0x00
assert_equal [0, 0x00].pack("C*"), auto_reconnect.packet_body
end

def test_request_dlen
auto_reconnect = @stub_request_class.new 0x01, 0, 0x00
assert_equal 3, auto_reconnect.dlen
end

def test_request_header
stub_request = @stub_request_class.new 0x01, 1, 2
expected_header = [Sphero::Request::SOP1, Sphero::Request::SOP2,
0x00, STUB_REQUEST_ID, 0x01, 3]

assert_equal expected_header, stub_request.header
end

def test_ping_to_str
ping = Sphero::Request::Ping.new 0
assert_equal "\xFF\xFF\x00\x01\x00\x01\xFD", ping.to_str
end

def test_ping_checksum
ping = Sphero::Request::Ping.new 0
assert_equal "\xFD", ping.checksum.chr
end

def test_sleep_dlen
sleep = Sphero::Request::Sleep.new 0, 0, 0
assert_equal 0x04, sleep.dlen
end
end
14 changes: 0 additions & 14 deletions test/test_sphero.rb

This file was deleted.