Geo-Coder-Free

 view release on metacpan or  search on metacpan

t/domain.t  view on Meta::CPAN

	foreach my $case([ 'a', 1 ], [ $upgraded_umlaut, 1 ], [ "\x{4e2d}", 1 ], [ "\x{1F600}", 0 ]) {
		my ($alphabet, $has_words) = @{$case};
		my $name = _show($alphabet);

		# Spaces split the text, so the last word is left to be looked up with a comma
		my $backend = _openaddr();
		my $short = ($alphabet x ($shortest - 4)) . ' UK';
		is(length($short), $shortest - 1, "$name: one under the limit");
		is(scalar($backend->geocode(scantext => $short)), undef, 'finds nothing');
		is($backend->reads(), 0, 'without a read');

		$backend = _openaddr();
		my $long = ($alphabet x ($shortest - 3)) . ' UK';
		is(length($long), $shortest, "$name: at the limit");
		lives_ok { $backend->geocode(scantext => $long) } 'does not die';
		cmp_ok($backend->reads(), ($has_words ? '>' : '=='), 0, $has_words ? 'and reads the table' : 'and has no word to look up');
	}

	my $backend = _openaddr();
	$backend->geocode(scantext => "M\x{fc}nchen UK");
	ok(!scalar(grep { $_ eq 'NCHENGB' } @{$backend->{'openaddr_db'}{'digests'}}), 'an umlaut does not end a word');
};

# ---------------------------------------------------------------------------
# distance
# ---------------------------------------------------------------------------

subtest 'distance(lat1, lon1, lat2, lon2): the range of each coordinate' => sub {
	my ($lat, $lon, $over) = @config{qw(lat_max lon_max just_over)};

	my @positions = (
		[ 'lat1', 0 ],
		[ 'lon1', 1 ],
		[ 'lat2', 2 ],
		[ 'lon2', 3 ],
	);
	foreach my $position(@positions) {
		my ($name, $index) = @{$position};
		my $limit = ($index % 2) ? $lon : $lat;
		my $error = ($index % 2) ? $ERR_LONGITUDE : $ERR_LATITUDE;
		my @base = (1, 1, 2, 2);

		foreach my $edge($limit, -$limit) {
			my @args = @base;
			$args[$index] = $edge;
			lives_ok { distance(@args) } "valid: $name at $edge";
		}
		foreach my $outside($limit + $over, -$limit - $over) {
			my @args = @base;
			$args[$index] = $outside;
			throws_ok { distance(@args) } $error, "invalid: $name just past the edge, $outside";
		}

		foreach my $bad([ undef, qr/^$name must be defined/ ], [ 'abc', qr/^$name must be numeric/ ], [ '', qr/^$name must be numeric/ ], [ "\x{fc}", qr/^$name must be numeric/ ], [ 'Inf', $error ], [ '-Inf', $error ]) {
			my @args = @base;
			$args[$index] = $bad->[0];
			throws_ok { distance(@args) } $bad->[1], "invalid: $name is " . _show($bad->[0]);
		}

		TODO: {
			local $TODO = 'NaN looks like a number and compares false, so it passes the range check and dies in Math::Complex';
			my @args = @base;
			$args[$index] = 'NaN';
			throws_ok { distance(@args) } $error, "invalid: $name is NaN";
		}
	}

	foreach my $numeric([ '1e1', 'an exponent' ], [ ' 1', 'a leading space' ], [ "1\n", 'a trailing newline' ], [ '+1', 'a leading plus' ], [ '.5', 'no digit before the point' ]) {
		lives_ok { distance($numeric->[0], 0, 0, 0) } "valid: $numeric->[1], which looks like a number";
	}

	throws_ok { distance(0, 0, 1) } qr/Too few arguments/, 'invalid: three coordinates';
	throws_ok { distance(0, 0, 1, 1, 'K', 'extra') } qr/Too many arguments/, 'invalid: six arguments';
};

subtest 'distance(): the combinations at the edges of the globe' => sub {
	my ($lat, $lon, $slack) = @config{qw(lat_max lon_max distance_slack)};

	my @cases = (
		[ [ 0, 0, 0, 0 ], 0, 'the same point' ],
		[ [ $lat, $lon, $lat, $lon ], 0, 'the same corner' ],
		[ [ $lat, 0, -$lat, 0 ], $config{'half_earth_km'}, 'pole to pole' ],
		[ [ 0, 0, 0, $lon ], $config{'half_earth_km'}, 'half way round the equator' ],
		[ [ 0, 0, 0, -$lon ], $config{'half_earth_km'}, 'half way round the other way' ],
		[ [ 0, $lon, 0, -$lon ], 0, 'the two sides of the antimeridian are one place' ],
		[ [ $lat, -$lon, $lat, $lon ], 0, 'latitude at its maximum and longitude at both ends: all the pole' ],
		[ [ $lat, -$lon, -$lat, $lon ], $config{'half_earth_km'}, 'latitude at both ends and longitude at both ends' ],
		[ [ 0, 0, 1, 0 ], $config{'one_degree_km'}, 'one degree of latitude' ],
	);
	foreach my $case(@cases) {
		my ($points, $want, $name) = @{$case};
		cmp_ok(abs(distance(@{$points}, 'K') - $want), '<', $slack, "$name is $want km");
	}
};

subtest 'distance(unit): K, N or M, in either case' => sub {
	my $slack = $config{'distance_slack'};
	my @halfway = (0, 0, 0, $config{'lon_max'});

	my @cases = (
		[ 'K', 'half_earth_km' ],
		[ 'k', 'half_earth_km' ],
		[ 'N', 'half_earth_nm' ],
		[ 'n', 'half_earth_nm' ],
		[ 'M', 'half_earth_miles' ],
		[ 'm', 'half_earth_miles' ],
		[ undef, 'half_earth_miles' ],
		[ '', 'half_earth_miles' ],
		[ '0', 'half_earth_miles' ],
	);
	foreach my $case(@cases) {
		my ($unit, $key) = @{$case};
		cmp_ok(abs(distance(@halfway, $unit) - $config{$key}), '<', $slack, 'valid: ' . _show($unit) . " is $key");
	}
	cmp_ok(abs(distance(@halfway) - $config{'half_earth_miles'}), '<', $slack, 'absent: miles');

	foreach my $bad('X', 'KM', 'kilometres', ' K', 'K ', '1', "\x{fc}", "\x{1F600}") {
		throws_ok { distance(@halfway, $bad) } $ERR_UNIT, 'invalid: ' . _show($bad);
	}

	is(distance(0, 0, 0, 0, 'X'), 0, 'the same point is 0 before the unit is looked at');

	TODO: {
		local $TODO = 'the unit check uses $, which lets a trailing newline through, and the unit then falls through to miles';



( run in 1.367 second using v1.01-cache-2.11-cpan-60884a8df76 )