Permalink
Browse files

Fix up some tests so that they run on older Ruby versions.

Specifically, in versions of Ruby prior to 1.9, Hashes are unordered, so
testing JSON and the like can be tricky.
  • Loading branch information...
1 parent 420a224 commit c73b6059238fc0e5f57935ca9b256c9c13694ce0 @dark-panda committed Feb 28, 2012
Showing with 285 additions and 74 deletions.
  1. +108 −64 test/google_maps_api_3_tests.rb
  2. +177 −10 test/writer_tests.rb
@@ -56,22 +56,41 @@ def test_to_jsonable
end
if defined?(JSON)
- def test_to_g_polygon
- assert_equal(
- %{new google.maps.Polygon({"paths": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})},
- @polygon.to_g_polygon
- )
+ def poly_tester(type, expected, poly)
+ json = poly.gsub(/new google.maps.LatLng\(([^)]+)\)/, '[ \1 ]').gsub(/"map":\s*map/, %{"map": "map"})
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polygon(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
+ assert(json =~ /^new google.maps.#{type}\((.+)\)$/)
+
+ assert_equal(expected, JSON.load($~[1]))
+ end
+
+ def test_to_g_polygon
+ poly_tester('Polygon', {
+ "paths" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polygon)
+
+ poly_tester('Polygon', {
+ 'strokeColor' => '#b00b1e',
+ 'strokeWeight' => 5,
+ 'strokeOpacity' => 0.5,
+ 'fillColor' => '#b00b1e',
+ 'map' => 'map',
+ 'paths' => [
+ [ 0.0, 0.0 ],
+ [ 1.0, 1.0 ],
+ [ 2.5, 2.5 ],
+ [ 5.0, 5.0 ],
+ [ 0.0, 0.0 ]
+ ]
+ }, @polygon.to_g_polygon(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ ))
end
def test_to_g_polygon_with_multi_polygon
@@ -82,6 +101,7 @@ def test_to_g_polygon_with_multi_polygon
((20 20, 20 25, 25 25, 25 20, 20 20))
)'
)
+
options = {
:stroke_color => '#b00b1e',
:stroke_weight => 5,
@@ -90,59 +110,83 @@ def test_to_g_polygon_with_multi_polygon
:map => 'map'
}
- assert_equal(
- ["new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)]})",
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]})"],
- multi_polygon.to_g_polygon(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
-
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
- multi_polygon.to_g_polygon({
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- }, {
- :single => true
- })
- )
+ expected = [ {
+ "paths" => [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, {
+ "paths" => [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, {
+ "paths" => [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ } ]
+
+ multi_polygon.to_g_polygon(options).each_with_index do |polygon, i|
+ poly_tester('Polygon', expected[i], polygon)
+ end
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
- multi_polygon.to_g_polygon_single(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
+ poly_tester("Polygon", {
+ "paths" => [
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
+ ],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, multi_polygon.to_g_polygon(options, {
+ :single => true
+ }))
+
+ poly_tester("Polygon", {
+ "paths" => [
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
+ ],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, multi_polygon.to_g_polygon_single(options))
end
def test_to_g_polyline
- assert_equal(
- "new google.maps.Polyline({\"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polyline
- )
-
- assert_equal(
- "new google.maps.Polyline({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"map\": map, \"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polyline(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :map => 'map'
- )
- )
+ poly_tester("Polyline", {
+ "path" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polyline)
+
+ poly_tester("Polyline", {
+ "strokeColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "strokeOpacity" => 0.5,
+ "map" => "map",
+ "path" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polyline(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :map => 'map'
+ ))
end
def test_to_g_marker
Oops, something went wrong.

0 comments on commit c73b605

Please sign in to comment.