diff --git a/lib/coordinate.rb b/lib/coordinate.rb index 49cdb29..01fd546 100644 --- a/lib/coordinate.rb +++ b/lib/coordinate.rb @@ -7,7 +7,8 @@ def initialize(x, y) end def distance_from(other) - # TODO: actually caluculate distance. I think we need to use the Pythagorean theorem? - 0 + dx = other.x - x + dy = other.y - y + Math.sqrt(dx*dx+dy*dy) end end diff --git a/spec/shouty_spec.rb b/spec/shouty_spec.rb new file mode 100644 index 0000000..38ea4b4 --- /dev/null +++ b/spec/shouty_spec.rb @@ -0,0 +1,21 @@ +require 'rantly/rspec_extensions' +require 'shouty' +require 'coordinate' + +describe Shouty do + it "only tranmits shouts within 1000m" do + property_of { + shouter_loc = Coordinate.new(*Rantly(2) { range(0, 2000) }) + listener_loc = Coordinate.new(*Rantly(2) { range(0, 2000) }) + guard shouter_loc.distance_from(listener_loc) < 1000 + + [shouter_loc, listener_loc] + }.check { |shouter_loc, listener_loc| + shouty = Shouty.new + shouty.set_location('shouter', shouter_loc) + shouty.set_location('listener', listener_loc) + shouty.shout('shouter', listener_loc) + expect(shouty.messages_heard_by('listener').length).to eq(1) + } + end +end