HTML-OSM

 view release on metacpan or  search on metacpan

t/function.t  view on Meta::CPAN

subtest 'new: method-style with no args produces correct defaults' => sub {
	my $m = HTML::OSM->new();
	isa_ok($m, 'HTML::OSM');
	is($m->{zoom},   $CFG{DEFAULT_ZOOM},   'default zoom');
	is($m->{height}, $CFG{DEFAULT_HEIGHT}, 'default height');
	is($m->{width},  $CFG{DEFAULT_WIDTH},  'default width');
	is_deeply($m->{coordinates}, [], 'coordinates empty arrayref');
	ok(!$m->{cluster}, 'cluster off');
	like($m->{css_url},             qr/leaflet/,       'css_url');
	like($m->{js_url},              qr/leaflet/,       'js_url');
	like($m->{cluster_js_url},      qr/markercluster/, 'cluster_js_url');
	like($m->{cluster_css_url},     qr/MarkerCluster/, 'cluster_css_url');
	like($m->{heatmap_js_url},      qr/leaflet.heat/,  'heatmap_js_url');
	like($m->{gpx_js_url},          qr/gpx/i,          'gpx_js_url');
	like($m->{tile_url},            qr/openstreetmap/, 'tile_url');
	returns_ok($m, { type => 'object', isa => 'HTML::OSM' }, 'returns HTML::OSM');
	memory_cycle_ok($m->{coordinates}, 'no cycles in coordinates');
	diag("defaults: zoom=$m->{zoom} h=$m->{height} w=$m->{width}") if $ENV{TEST_VERBOSE};
};

subtest 'new: function-style HTML::OSM::new() blesses into HTML::OSM' => sub {
	my $m = HTML::OSM::new();
	isa_ok($m, 'HTML::OSM', 'function-style call');
};

subtest 'new: function-style with args' => sub {
	my $m = HTML::OSM::new(zoom => 8);
	is($m->{zoom}, 8, 'zoom arg respected');
};

subtest 'new: zoom accepted at min and max boundaries' => sub {
	is(HTML::OSM->new(zoom => $CFG{ZOOM_MIN})->{zoom}, $CFG{ZOOM_MIN}, 'zoom 0 ok');
	is(HTML::OSM->new(zoom => $CFG{ZOOM_MAX})->{zoom}, $CFG{ZOOM_MAX}, 'zoom 19 ok');
};

subtest 'new: zoom out of range croaks' => sub {
	dies_ok { HTML::OSM->new(zoom => -1)  } 'zoom -1 dies';
	dies_ok { HTML::OSM->new(zoom => 20)  } 'zoom 20 dies';
	dies_ok { HTML::OSM->new(zoom => 'x') } 'non-integer zoom dies';
};

subtest 'new: unknown param rejected by schema' => sub {
	dies_ok { HTML::OSM->new(bogus_param => 1) } 'unknown param dies';
};

subtest 'new: coordinates must be arrayref' => sub {
	dies_ok { HTML::OSM->new(coordinates => 'bad') } 'string coordinates die';
};

subtest 'new: CDN URLs overridable' => sub {
	my $m = HTML::OSM->new(css_url => 'https://my.cdn/leaflet.css',
	                        gpx_js_url => 'https://my.cdn/gpx.js',
	                        tile_url  => 'https://tiles.internal/{z}/{x}/{y}.png');
	is($m->{css_url},    'https://my.cdn/leaflet.css', 'css_url overridden');
	is($m->{gpx_js_url}, 'https://my.cdn/gpx.js',     'gpx_js_url overridden');
	is($m->{tile_url},   'https://tiles.internal/{z}/{x}/{y}.png', 'tile_url overridden');
};

subtest 'new: clone merges overrides, skips validation, is a distinct object' => sub {
	my $base  = HTML::OSM->new(zoom => 5);
	my $clone = $base->new(zoom => 9, _private_key => 'val');
	isa_ok($clone, 'HTML::OSM', 'clone isa HTML::OSM');
	is($clone->{zoom},        9,                    'clone zoom updated');
	is($clone->{height},      $CFG{DEFAULT_HEIGHT}, 'clone inherits height');
	is($clone->{_private_key}, 'val',               'arbitrary key passes clone (no validation)');
	isnt(refaddr($clone), refaddr($base), 'clone is a distinct reference');
	returns_ok($clone, { type => 'object', isa => 'HTML::OSM' }, 'clone returns HTML::OSM');
};

subtest 'new: cache default is an object with get/set' => sub {
	my $m = HTML::OSM->new();
	can_ok($m->{cache}, 'get');
	can_ok($m->{cache}, 'set');
};

subtest 'new: cluster => 1 stored' => sub {
	ok(HTML::OSM->new(cluster => 1)->{cluster}, 'cluster enabled');
};

# ─────────────────────────────────────────────────────────────────────────────
# zoom()
# ─────────────────────────────────────────────────────────────────────────────

subtest 'zoom: getter returns default' => sub {
	my $m = HTML::OSM->new();
	is($m->zoom(), $CFG{DEFAULT_ZOOM}, 'getter returns 12');
	returns_ok($m->zoom(), { type => 'integer' }, 'zoom() returns integer');
};

subtest 'zoom: setter updates value and is returned' => sub {
	my $m = HTML::OSM->new();
	my $z = $m->zoom(10);
	is($z,        10, 'setter return value');
	is($m->zoom(), 10, 'getter sees new value');
};

subtest 'zoom: boundary values accepted' => sub {
	my $m = HTML::OSM->new();
	$m->zoom($CFG{ZOOM_MIN}); is($m->zoom(), $CFG{ZOOM_MIN}, 'zoom 0');
	$m->zoom($CFG{ZOOM_MAX}); is($m->zoom(), $CFG{ZOOM_MAX}, 'zoom 19');
};

subtest 'zoom: non-integer dies' => sub {
	my $m = HTML::OSM->new();
	dies_ok { $m->zoom('high') } 'string zoom dies';
};

subtest 'zoom: out-of-range dies' => sub {
	my $m = HTML::OSM->new();
	dies_ok { $m->zoom(-1)  } 'zoom -1 dies';
	dies_ok { $m->zoom(20)  } 'zoom 20 dies';
};

# ─────────────────────────────────────────────────────────────────────────────
# add_marker()
# ─────────────────────────────────────────────────────────────────────────────

subtest 'add_marker: [lat, lon] arrayref succeeds, stores tuple' => sub {
	my $m = HTML::OSM->new();
	my $r = $m->add_marker([$CFG{LAT_LONDON}, $CFG{LON_LONDON}], html => 'London');
	is($r, 1, 'returns 1');
	is(scalar @{$m->{coordinates}}, 1,                  'one coord stored');
	is($m->{coordinates}[0][0],     $CFG{LAT_LONDON},   'lat stored');
	is($m->{coordinates}[0][1],     $CFG{LON_LONDON},   'lon stored');
	is($m->{coordinates}[0][2],     'London',           'label stored');



( run in 1.165 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )