From dfc4ad95b3506c7b52ead6cf1e5f34d3a13dbb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 4 Oct 2017 08:31:17 +0100 Subject: [PATCH] Add a spike based on Rantly --- lib/coordinate.rb | 5 +++-- spec/shouty_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 spec/shouty_spec.rb 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