location_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. require 'spec_helper'
  2. describe Location do
  3. let(:location) {
  4. Location.new(
  5. lat: BigDecimal.new('2.0'),
  6. lng: BigDecimal.new('3.0'),
  7. radius: 300,
  8. speed: 2,
  9. course: 30)
  10. }
  11. it "converts values to Float" do
  12. expect(location.lat).to be_a Float
  13. expect(location.lat).to eq 2.0
  14. expect(location.lng).to be_a Float
  15. expect(location.lng).to eq 3.0
  16. expect(location.radius).to be_a Float
  17. expect(location.radius).to eq 300.0
  18. expect(location.speed).to be_a Float
  19. expect(location.speed).to eq 2.0
  20. expect(location.course).to be_a Float
  21. expect(location.course).to eq 30.0
  22. end
  23. it "provides hash-style access to its properties with both symbol and string keys" do
  24. expect(location[:lat]).to be_a Float
  25. expect(location[:lat]).to eq 2.0
  26. expect(location['lat']).to be_a Float
  27. expect(location['lat']).to eq 2.0
  28. end
  29. it "does not allow hash-style assignment" do
  30. expect {
  31. location[:lat] = 2.0
  32. }.to raise_error
  33. end
  34. it "ignores invalid values" do
  35. location2 = Location.new(
  36. lat: 2,
  37. lng: 3,
  38. radius: -1,
  39. speed: -1,
  40. course: -1)
  41. expect(location2.radius).to be_nil
  42. expect(location2.speed).to be_nil
  43. expect(location2.course).to be_nil
  44. end
  45. it "considers a location empty if either latitude or longitude is missing" do
  46. expect(Location.new.empty?).to be_truthy
  47. expect(Location.new(lat: 2, radius: 1).present?).to be_falsy
  48. expect(Location.new(lng: 3, radius: 1).present?).to be_falsy
  49. end
  50. it "is droppable" do
  51. {
  52. '{{location.lat}}' => '2.0',
  53. '{{location.latitude}}' => '2.0',
  54. '{{location.lng}}' => '3.0',
  55. '{{location.longitude}}' => '3.0',
  56. }.each { |template, result|
  57. expect(Liquid::Template.parse(template).render('location' => location.to_liquid)).to eq(result),
  58. "expected #{template.inspect} to expand to #{result.inspect}"
  59. }
  60. end
  61. end