App-MtAws

 view release on metacpan or  search on metacpan

t/integration/utf8_line_protocol.t  view on Meta::CPAN




my $str = "Тест";

ok (decode_data(encode_data($str)) eq $str);
ok (utf8::is_utf8 decode_data(encode_data($str)) );
ok (!utf8::is_utf8 encode_data($str) );
ok (length(decode_data(encode_data($str))) == 4 );
is (bytes::length(encode_data($str)), 26);

my $str_binary = encode("UTF-8", $str);
my $recorded = decode_data(encode_data($str_binary));
ok ($recorded eq $str_binary);
ok (utf8::is_utf8($recorded) && !utf8::is_utf8($str_binary));

my $mtroot = get_temp_dir();
my $tmp_file = "$mtroot/line_proto_test";
our $file = undef;

# serialization tests

my $samples = [
	{ a => 'b' },
	{},
	undef,
	{ z => undef },
	{ a => '0' },
	{ '0' => '0' },
	['0'],
	{ a => '1' },
	{ '1' => '1' },
	['1'],
	{ a => 1.1 },
	{ 1.2 => 1.3 },
	[1.5],

			# is not compat with YAML
	{ a => 0 },
	{ 0 => 0 },
	[0],
	{ a => 1 },
	{ 1 => 1 },
	[1],

	[],
	[undef],
	[undef, undef],

	{
		'somekey1' => { 'keyA' => 'data1', 'keyB' => 'data2', 'keyC' => '99999' },
		'somekey2' => { 'XkeyA' => '2734234', 'XkeyB' => "data2", 'XkeyC' => '76324' },
	},
	{
	'somekey1' => { 'keyA' => [ 'data1', 'data2', '837'], 'keyB' => 'data2', 'keyC' => {'99999'=>undef}, 'z' => {undef => undef} },
	'somekey2' => { 'XkeyA' => '2734234', 'XkeyB' => { '123' => "data1", "g1" => "x5", "c4" => "x4"}, 'XkeyC' => [undef] },
},
[ { a=> 123, b=> 456 }, { c => 789 }, { e => 909}]
];

my $escape_samples = [
"a\nb",
"ab\n",
"ab\n",
"\nab\n",
"\n\n",
"\n",
"\\",
"\\\\",
"\\n",
"\\\n",
"\\\\n",
"\r",
"\\r",
"\\\r",
"\\\r\\",
"\r\\",
"a\r\\c",
"\r\t",
"\\\\\\\\",
"\n\r\@\~",
"x" x 8192,
];

for my $src (@$samples, map { { x => $_} } @$escape_samples) {
	my $enc = encode_data($src);
	my $dst = decode_data($enc);
	cmp_deeply $dst, $src;
	ok $enc =~ qr/\A[^\n\r]+\z/s;
}


sub sending
{
	local $file;
	open $file, ">", $tmp_file;
	shift->();
	close $file;
}

sub receiving
{
	local $file;
	open $file, "<", $tmp_file;
	shift->();
	close $file;
}

# should work
{
	my $src = { var => 'test' };
	sending sub {
		ok send_data($file, 'testaction', 'sometaskid', $src);
	};
	receiving sub {
		my ($pid, $action, $taskid, $data) = get_data($file);
		is $pid, $$;
		is $action, 'testaction';
		is $taskid, 'sometaskid';
		cmp_deeply($data, $src);
	}
}

sub check_utf8_on
{
	my $h = shift;
	utf8::is_utf8((keys %$h)[0]) && utf8::is_utf8((values %$h)[0]);
}

# should not downgrade Latin-1 strings
{

	my $c = 'Ñ';
	ok ord $c <= 255;

	ok utf8::is_utf8 $c;

	my $src = { $c => $c };
	ok check_utf8_on($src);

	sending sub {
		ok send_data($file, 'testaction', 'sometaskid', $src);
		ok check_utf8_on($src);
	};
	receiving sub {



( run in 2.616 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )