HTML-OSM

 view release on metacpan or  search on metacpan

t/function.t  view on Meta::CPAN


# ─────────────────────────────────────────────────────────────────────────────
# _html_attr() — escapes a value for a double-quoted HTML attribute.
# ─────────────────────────────────────────────────────────────────────────────

subtest '_html_attr: undef becomes empty string' => sub {
	is(HTML::OSM::_html_attr(undef), '', 'undef input gives empty string');
};

subtest '_html_attr: plain URL passes through unchanged' => sub {
	is(HTML::OSM::_html_attr('https://unpkg.com/leaflet.js'), 'https://unpkg.com/leaflet.js', 'URL unchanged');
};

subtest '_html_attr: &, <, > and " become entities, & first' => sub {
	is(HTML::OSM::_html_attr(q{a&b"c<d>}), 'a&amp;b&quot;c&lt;d&gt;', 'all four escaped');
	is(HTML::OSM::_html_attr('&lt;'), '&amp;lt;', 'existing entity is not left as-is');
};

# ─────────────────────────────────────────────────────────────────────────────
# _css_length() — validates width/height before they reach the <style> block.
# ─────────────────────────────────────────────────────────────────────────────

subtest '_css_length: valid lengths are returned unchanged' => sub {
	for my $v (qw(600px 100% 2.5em 50vh auto 300)) {
		is(HTML::OSM::_css_length('width', $v, $CFG{DEFAULT_WIDTH}), $v, "'$v' accepted");
	}
};

subtest '_css_length: undef and empty fall back to the default silently' => sub {
	my @warnings;
	local $SIG{__WARN__} = sub { push @warnings, @_ };
	is(HTML::OSM::_css_length('width', undef, $CFG{DEFAULT_WIDTH}), $CFG{DEFAULT_WIDTH}, 'undef -> default');
	is(HTML::OSM::_css_length('width', '',    $CFG{DEFAULT_WIDTH}), $CFG{DEFAULT_WIDTH}, 'empty -> default');
	is(scalar @warnings, 0, 'no warning for a missing value');
};

subtest '_css_length: hostile value falls back to the default with a warning' => sub {
	my @warnings;
	local $SIG{__WARN__} = sub { push @warnings, @_ };
	is(HTML::OSM::_css_length('height', '1px}</style><script>', $CFG{DEFAULT_HEIGHT}),
		$CFG{DEFAULT_HEIGHT}, 'injection attempt replaced');
	is(HTML::OSM::_css_length('height', "600px\n", $CFG{DEFAULT_HEIGHT}),
		$CFG{DEFAULT_HEIGHT}, 'trailing newline rejected');
	is(scalar @warnings, 2, 'one warning per replacement');
	like($warnings[0], qr/onload_render: invalid height/, 'warning names the field');
};

# ─────────────────────────────────────────────────────────────────────────────
# _positive_number() — heatmap radius/blur are embedded raw in JS.
# ─────────────────────────────────────────────────────────────────────────────

subtest '_positive_number: positive numbers are returned as numbers' => sub {
	is(HTML::OSM::_positive_number(30, 25),     30,  'integer');
	is(HTML::OSM::_positive_number('12.5', 25), 12.5, 'numeric string');
	is(HTML::OSM::_positive_number(' 7', 25),   7,   'surrounding space is trimmed');
	is(HTML::OSM::_positive_number('.5', 25),   0.5, 'leading decimal point');
	is(HTML::OSM::_positive_number(Scalar::Util::dualvar(12.5, '12,5'), 25), '12.5', 'comma-printed number');
};

subtest '_positive_number: everything else gives the default' => sub {
	for my $v (undef, '', 0, -5, 'abc', '1});alert(1);//', 'Inf', 'NaN', 1e9, '1e2', '12,5', [1]) {
		is(HTML::OSM::_positive_number($v, 25), 25, 'default for ' . ($v // 'undef'));
	}
};

# ─────────────────────────────────────────────────────────────────────────────
# _validate() — private helper, called as a plain function (not a method).
# ─────────────────────────────────────────────────────────────────────────────

subtest '_validate: valid WGS-84 coordinates return 1' => sub {
	is(HTML::OSM::_validate($CFG{LAT_LONDON}, $CFG{LON_LONDON}), 1, 'London valid');
	is(HTML::OSM::_validate(0, 0), 1, 'origin (0,0) valid');
	returns_ok(HTML::OSM::_validate(0, 0), { type => 'integer' }, '_validate returns integer');
};

subtest '_validate: boundary values accepted' => sub {
	is(HTML::OSM::_validate($CFG{LAT_MIN}, $CFG{LON_MIN}), 1, 'min boundary -90,-180');
	is(HTML::OSM::_validate($CFG{LAT_MAX}, $CFG{LON_MAX}), 1, 'max boundary +90,+180');
};

subtest '_validate: leading-dot decimal is valid (documented since 0.05)' => sub {
	is(HTML::OSM::_validate('.5',    '.5'),    1, 'leading-dot positive');
	is(HTML::OSM::_validate('-.5167', '.5'), 1, 'leading-dot signed negative lat');
};

subtest '_validate: empty string is rejected' => sub {
	# The old regex /^-?\d*(\.\d+)?$/ matched '' — the fixed one requires a digit.
	local $SIG{__WARN__} = $SILENCE;
	is(HTML::OSM::_validate('', 0),  0, 'empty string lat rejected');
	is(HTML::OSM::_validate(0,  ''), 0, 'empty string lon rejected');
};

subtest '_validate: out-of-range values return 0 and emit a carp' => sub {
	warning_like { HTML::OSM::_validate(91, 0) }
		qr/Skipping invalid coordinate/,
		'out-of-range lat carps';
	{
		local $SIG{__WARN__} = $SILENCE;
		is(HTML::OSM::_validate(91,   0),  0, 'lat 91');
		is(HTML::OSM::_validate(-91,  0),  0, 'lat -91');
		is(HTML::OSM::_validate(0,  181),  0, 'lon 181');
		is(HTML::OSM::_validate(0, -181),  0, 'lon -181');
	}
};

subtest '_validate: non-numeric strings return 0 and carp' => sub {
	local $SIG{__WARN__} = $SILENCE;
	is(HTML::OSM::_validate('abc', 0),   0, 'alpha lat rejected');
	is(HTML::OSM::_validate(0, 'xyz'),   0, 'alpha lon rejected');
	is(HTML::OSM::_validate('1e5', 0),   0, 'scientific notation rejected');
};

subtest '_validate: undef args return 0 silently (no carp)' => sub {
	# There is no value to complain about when the caller never supplied coords.
	my @warnings;
	local $SIG{__WARN__} = sub { push @warnings, @_ };
	is(HTML::OSM::_validate(undef, 0),     0, 'undef lat -> 0');
	is(HTML::OSM::_validate(0,     undef), 0, 'undef lon -> 0');
	is(scalar @warnings, 0, 'no carp for undef args');
};



( run in 2.298 seconds using v1.01-cache-2.11-cpan-60884a8df76 )