Data-TableReader

 view release on metacpan or  search on metacpan

t/30-detect-input-format.t  view on Meta::CPAN

	{
		name  => 'XLS from content type',
		hints => {
			content_type => 'application/vnd.ms-excel',
			content_head => '',
		},
		expected => [ 'XLS' ],
	},
	{
		name  => 'XLSX from content type',
		hints => {
			content_type =>
				'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
			content_head => '',
		},
		expected => [ 'XLSX' ],
	},
	{
		name  => 'CSV from filename',
		hints => {
			filename     => 'sample.csv',
			charset      => 'UTF-8',
			content_head => $csv,
		},
		expected => [ 'CSV', encoding => 'utf-8-strict' ],
	},
	{
		name  => 'TSV from filename',
		hints => {
			filename     => 'sample.tsv',
			charset      => 'UTF-16LE',
			content_head => encode('UTF-16LE', $tsv),
		},
		expected => [ 'TSV', encoding => 'UTF-16LE' ],
	},
	{
		name  => 'HTM suffix normalizes to HTML',
		hints => {
			filename     => 'sample.htm',
			content_head => '<html>',
		},
		expected => [ 'HTML' ],
	},
	{
		name  => 'XLS magic takes priority over CSV filename',
		hints => {
			filename     => 'wrong.csv',
			content_head => "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1",
		},
		expected => [ 'XLS' ],
	},
	{
		name  => 'XLSX magic takes priority over CSV filename',
		hints => {
			filename     => 'wrong.csv',
			content_head => "PK\x03\x04more bytes",
		},
		expected => [ 'XLSX' ],
	},
	{
		name  => 'HTML UTF-8 without BOM',
		hints => {
			content_head => '<!DOCTYPE html><html>',
		},
		expected => [ 'HTML' ],
	},
	{
		name  => 'HTML UTF-8 with BOM',
		hints => {
			content_head => encode_with_bom(
				'UTF-8',
				'<!DOCTYPE html><html>'
			),
		},
		expected => [ 'HTML', encoding => 'utf-8-strict' ],
	},
	{
		name  => 'HTML UTF-16LE with BOM',
		hints => {
			content_head => encode_with_bom(
				'UTF-16LE',
				'<!DOCTYPE html><html>',
			),
		},
		expected => [ 'HTML', encoding => 'UTF-16LE' ],
	},
	{
		name  => 'HTML UTF-16BE with BOM',
		hints => {
			content_head => encode_with_bom(
				'UTF-16BE',
				'<html>',
			),
		},
		expected => [ 'HTML', encoding => 'UTF-16BE' ],
	},
	{
		name  => 'HTML UTF-32LE with BOM',
		hints => {
			content_head => encode_with_bom(
				'UTF-32LE',
				'<html>',
			),
		},
		expected => [ 'HTML', encoding => 'UTF-32LE' ],
	},
	{
		name  => 'HTML UTF-32BE with BOM',
		hints => {
			content_head => encode_with_bom(
				'UTF-32BE',
				'<html>',
			),
		},
		expected => [ 'HTML', encoding => 'UTF-32BE' ],
	},
	{
		name  => 'CSV probe in UTF-8',
		hints => {
			content_head => $csv,
		},
		expected => [ 'CSV' ],
	},
	{
		name  => 'TSV probe in UTF-8',
		hints => {
			content_head => $tsv,
		},
		expected => [ 'TSV' ],
	},
	{
		name  => 'CSV probe using supplied UTF-16LE',
		hints => {
			charset      => 'UTF-16LE',
			content_head => encode('UTF-16LE', $csv),
		},
		expected => [ 'CSV', encoding => 'UTF-16LE' ],
	},
	{
		name  => 'TSV probe using supplied UTF-32BE',
		hints => {
			charset      => 'UTF-32BE',
			content_head => encode('UTF-32BE', $tsv),
		},
		expected => [ 'TSV', encoding => 'UTF-32BE' ],
	},
	{
		name  => 'CSV probe detects UTF-16LE BOM',
		hints => {
			content_head => encode_with_bom('UTF-16LE', $csv),
		},
		expected => [ 'CSV', encoding => 'UTF-16LE' ],
	},
	{
		name  => 'TSV probe detects UTF-16BE BOM',
		hints => {
			content_head => encode_with_bom('UTF-16BE', $tsv),
		},
		expected => [ 'TSV', encoding => 'UTF-16BE' ],
	},
	{
		name  => 'CSV probe detects UTF-32LE without BOM',
		hints => {
			content_head => encode('UTF-32LE', $csv),
		},
		expected => [ 'CSV', encoding => 'UTF-32LE' ],
	},
	{
		name  => 'TSV probe detects UTF-32BE without BOM',
		hints => {
			content_head => encode('UTF-32BE', $tsv),
		},
		expected => [ 'TSV', encoding => 'UTF-32BE' ],
	},
	{
		name  => 'unknown content type falls through to probe',
		hints => {
			content_type => 'application/octet-stream',
			content_head => $csv,
		},
		expected => [ 'CSV' ],
	},
	{
		name  => 'unknown charset is ignored',
		hints => {
			content_type => 'text/csv',
			charset     => 'surely-not-an-encoding',
			content_head => $csv,
		},
		expected => [ 'CSV' ],
	},
	{
		name  => 'nonsense is not detected',
		hints => {
			content_head => 'this is not tabular data',
		},
		expected => [],
	},
	{
		name  => 'ambiguous equal comma and tab evidence is not detected',
		hints => {
			content_head => "one,two\tthree\n",
		},
		expected => [],
	},
	{
		name  => 'empty content without filename is not detected',
		hints => {
			content_head => '',
		},
		expected => [],
	},
);

for my $t (@tests) {
	subtest $t->{name} => sub {
		my $tr= new_ok(
			'Data::TableReader',
			[
				input  => $t->{input} || unused_input(),
				fields => [],
				log    => sub { note "$_[0] $_[1]" },
			],
			'reader',
		);

		my (@got, $error);
		{
			local $@;



( run in 0.915 second using v1.01-cache-2.11-cpan-8dfa8b56332 )