From 7d08baca38827b59af9592e415bc4778fb8c4958 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 16 Feb 2012 16:24:51 +0100 Subject: Start building tests. --- test/00.load.py | 10 -- test/10-input.py | 103 ------------- test/20-reader.py | 419 --------------------------------------------------- test/30-output.py | 107 ------------- test/40-writer.py | 200 ------------------------ test/all_tests.py | 11 ++ test/input.py | 102 +++++++++++++ test/load.py | 10 ++ test/output.py | 107 +++++++++++++ test/pod-coverage.py | 9 -- test/pod_coverage.py | 9 ++ test/reader.py | 419 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/writer.py | 200 ++++++++++++++++++++++++ 13 files changed, 858 insertions(+), 848 deletions(-) delete mode 100644 test/00.load.py delete mode 100644 test/10-input.py delete mode 100644 test/20-reader.py delete mode 100644 test/30-output.py delete mode 100644 test/40-writer.py create mode 100644 test/all_tests.py create mode 100644 test/input.py create mode 100644 test/load.py create mode 100644 test/output.py delete mode 100644 test/pod-coverage.py create mode 100644 test/pod_coverage.py create mode 100644 test/reader.py create mode 100644 test/writer.py (limited to 'test') diff --git a/test/00.load.py b/test/00.load.py deleted file mode 100644 index e4f3f5f..0000000 --- a/test/00.load.py +++ /dev/null @@ -1,10 +0,0 @@ -# -*- coding: utf-8 -*- -import unittest - -class TestBasics(unittest.TestCase): - def test_import(self): - import yamlish - from yamlish import Reader - from yamlish import Writer - self.assert_(True, - "Testing import of yamlish, version %s." % yamlish.__version__) diff --git a/test/10-input.py b/test/10-input.py deleted file mode 100644 index 5ec8d96..0000000 --- a/test/10-input.py +++ /dev/null @@ -1,103 +0,0 @@ -# -*- coding: utf-8 -*- -import unittest - -IN = """ - --- - bill-to: - address: - city: "Royal Oak" - lines: "458 Walkman Dr.\nSuite #292\n" - postal: 48046 - state: MI - family: Dumars - given: Chris - comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n" - date: 2001-01-23 - invoice: 34843 - product: - - - description: Basketball - price: 450.00 - quantity: 4 - sku: BL394D - - - description: "Super Hoop" - price: 2392.00 - quantity: 1 - sku: BL4438H - tax: 251.42 - total: 4443.52 - ... -""" - -OUT = { - 'bill-to': { - 'given': 'Chris', - 'address': { - 'city': 'Royal Oak', - 'postal': '48046', - 'lines': "458 Walkman Dr.\nSuite #292\n", - 'state': 'MI' - }, - 'family': 'Dumars' - }, - 'invoice': '34843', - 'date': '2001-01-23', - 'tax': '251.42', - 'product': [ - { - 'sku': 'BL394D', - 'quantity': '4', - 'price': '450.00', - 'description': 'Basketball' - }, - { - 'sku': 'BL4438H', - 'quantity': '1', - 'price': '2392.00', - 'description': 'Super Hoop' - } - ], - 'comments': - "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", - 'total': '4443.52' -} - -class TestInput(unittest.TestCase): - """FIXME description of this class""" - def test_reader(self): - #my @lines = @$in; - #my $scalar = join("\n", @lines) . "\n"; - # - #my @source = ( - # { - # name= > 'Array reference', - # source= > $in, - # }, - # { - # name= > 'Closure', - # source= > sub { shift @lines }, - # }, - # { - # name= > 'Scalar', - # source= > $scalar, - # }, - # { - # name= > 'Scalar ref', - # source= > \$scalar, - # }, - #); - # - #for my $src (@source) { - # my $name = $src -> {name}; - # ok my $yaml = Data::YAML::Reader -> new, "$name: Created"; - # isa_ok $yaml, 'Data::YAML::Reader'; - # - # my $got = eval { $yaml -> read($src -> {source}) }; - # unless (is_deeply $got, $out, "$name: Result matches") { - # local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; - # diag(Data::Dumper -> Dump([$got], ['$got'])); - # diag(Data::Dumper -> Dump([$out], ['$expected'])); - # } - #} - pass diff --git a/test/20-reader.py b/test/20-reader.py deleted file mode 100644 index fc6806f..0000000 --- a/test/20-reader.py +++ /dev/null @@ -1,419 +0,0 @@ -use strict; -use warnings; -use Test::More; -use Data::Dumper; - -use Data::YAML::Reader; - -my @SCHEDULE; - -BEGIN { - @SCHEDULE = ( - { - name => 'Hello World', - in => [ '--- Hello, World', '...', ], - out => "Hello, World", - }, - { - name => 'Hello World 2', - in => [ '--- \'Hello, \'\'World\'', '...', ], - out => "Hello, 'World", - }, - { - name => 'Hello World 3', - in => [ '--- "Hello, World"', '...', ], - out => "Hello, World", - }, - { - name => 'Hello World 4', - in => [ '--- "Hello, World"', '...', ], - out => "Hello, World", - }, - { - name => 'Hello World 4', - in => [ '--- >', ' Hello,', ' World', '...', ], - out => "Hello, World\n", - }, - { - name => 'Hello World 5', - in => [ '--- >', ' Hello,', ' World', '...', ], - error => qr{Missing\s+'[.][.][.]'}, - }, - { - name => 'Simple array', - in => [ '---', '- 1', '- 2', '- 3', '...', ], - out => [ '1', '2', '3' ], - }, - { - name => 'Mixed array', - in => [ '---', '- 1', '- \'two\'', '- "three\n"', '...', ], - out => [ '1', 'two', "three\n" ], - }, - { - name => 'Hash in array', - in => [ '---', '- 1', '- two: 2', '- 3', '...', ], - out => [ '1', { two => '2' }, '3' ], - }, - { - name => 'Hash in array 2', - in => [ '---', '- 1', '- two: 2', ' three: 3', '- 4', '...', ], - out => [ '1', { two => '2', three => '3' }, '4' ], - }, - { - name => 'Nested array', - in => [ - '---', - '- one', - '-', - ' - two', - ' -', - ' - three', - ' - four', - '- five', - '...', - ], - out => [ 'one', [ 'two', ['three'], 'four' ], 'five' ], - }, - { - name => 'Nested hash', - in => [ - '---', - 'one:', - ' five: 5', - ' two:', - ' four: 4', - ' three: 3', - 'six: 6', - '...', - ], - out => { - one => { two => { three => '3', four => '4' }, five => '5' }, - six => '6' - }, - }, - - { - name => 'Original YAML::Tiny test', - in => [ - '---', - 'invoice: 34843', - 'date : 2001-01-23', - 'bill-to:', - ' given : Chris', - ' family : Dumars', - ' address:', - ' lines: |', - ' 458 Walkman Dr.', - ' Suite #292', - ' city : Royal Oak', - ' state : MI', - ' postal : 48046', - 'product:', - ' - sku : BL394D', - ' quantity : 4', - ' description : Basketball', - ' price : 450.00', - ' - sku : BL4438H', - ' quantity : 1', - ' description : Super Hoop', - ' price : 2392.00', - 'tax : 251.42', - 'total: 4443.52', - 'comments: >', - ' Late afternoon is best.', - ' Backup contact is Nancy', - ' Billsmer @ 338-4338', - '...', - ], - out => { - 'bill-to' => { - 'given' => 'Chris', - 'address' => { - 'city' => 'Royal Oak', - 'postal' => '48046', - 'lines' => "458 Walkman Dr.\nSuite #292\n", - 'state' => 'MI' - }, - 'family' => 'Dumars' - }, - 'invoice' => '34843', - 'date' => '2001-01-23', - 'tax' => '251.42', - 'product' => [ - { - 'sku' => 'BL394D', - 'quantity' => '4', - 'price' => '450.00', - 'description' => 'Basketball' - }, - { - 'sku' => 'BL4438H', - 'quantity' => '1', - 'price' => '2392.00', - 'description' => 'Super Hoop' - } - ], - 'comments' => - "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", - 'total' => '4443.52' - } - }, - - # Tests harvested from YAML::Tiny - { - in => ['...'], - name => 'Regression: empty', - error => qr{document\s+header\s+not\s+found} - }, - { - in => [ '# comment', '...' ], - name => 'Regression: only_comment', - error => qr{document\s+header\s+not\s+found} - }, - { - out => undef, - in => [ '---', '...' ], - name => 'Regression: only_header', - error => qr{Premature\s+end}i, - }, - { - out => undef, - in => [ '---', '---', '...' ], - name => 'Regression: two_header', - error => qr{Unexpected\s+start}i, - }, - { - out => undef, - in => [ '--- ~', '...' ], - name => 'Regression: one_undef' - }, - { - out => undef, - in => [ '--- ~', '...' ], - name => 'Regression: one_undef2' - }, - { - in => [ '--- ~', '---', '...' ], - name => 'Regression: two_undef', - error => qr{Missing\s+'[.][.][.]'}, - }, - { - out => 'foo', - in => [ '--- foo', '...' ], - name => 'Regression: one_scalar', - }, - { - out => 'foo', - in => [ '--- foo', '...' ], - name => 'Regression: one_scalar2', - }, - { - in => [ '--- foo', '--- bar', '...' ], - name => 'Regression: two_scalar', - error => qr{Missing\s+'[.][.][.]'}, - }, - { - out => ['foo'], - in => [ '---', '- foo', '...' ], - name => 'Regression: one_list1' - }, - { - out => [ 'foo', 'bar' ], - in => [ '---', '- foo', '- bar', '...' ], - name => 'Regression: one_list2' - }, - { - out => [ undef, 'bar' ], - in => [ '---', '- ~', '- bar', '...' ], - name => 'Regression: one_listundef' - }, - { - out => { 'foo' => 'bar' }, - in => [ '---', 'foo: bar', '...' ], - name => 'Regression: one_hash1' - }, - { - out => { - 'foo' => 'bar', - 'this' => undef - }, - in => [ '---', 'foo: bar', 'this: ~', '...' ], - name => 'Regression: one_hash2' - }, - { - out => { 'foo' => [ 'bar', undef, 'baz' ] }, - in => [ '---', 'foo:', ' - bar', ' - ~', ' - baz', '...' ], - name => 'Regression: array_in_hash' - }, - { - out => { - 'bar' => { 'foo' => 'bar' }, - 'foo' => undef - }, - in => [ '---', 'foo: ~', 'bar:', ' foo: bar', '...' ], - name => 'Regression: hash_in_hash' - }, - { - out => [ - { - 'foo' => undef, - 'this' => 'that' - }, - 'foo', undef, - { - 'foo' => 'bar', - 'this' => 'that' - } - ], - in => [ - '---', - '-', - ' foo: ~', - ' this: that', - '- foo', - '- ~', - '-', - ' foo: bar', - ' this: that', - '...' - ], - name => 'Regression: hash_in_array' - }, - { - out => ['foo'], - in => [ '---', '- \'foo\'', '...' ], - name => 'Regression: single_quote1' - }, - { - out => [' '], - in => [ '---', '- \' \'', '...' ], - name => 'Regression: single_spaces' - }, - { - out => [''], - in => [ '---', '- \'\'', '...' ], - name => 'Regression: single_null' - }, - { - out => ' ', - in => [ '--- " "', '...' ], - name => 'Regression: only_spaces' - }, - { - out => [ - undef, - { - 'foo' => 'bar', - 'this' => 'that' - }, - 'baz' - ], - in => - [ '---', '- ~', '- foo: bar', ' this: that', '- baz', '...' ], - name => 'Regression: inline_nested_hash' - }, - { - name => "Unprintables", - in => [ - "---", - "- \"\\z\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"", - "- \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\e\\x1c\\x1d\\x1e\\x1f\"", - "- \" !\\\"#\$%&'()*+,-./\"", - "- 0123456789:;<=>?", - "- '\@ABCDEFGHIJKLMNO'", - "- 'PQRSTUVWXYZ[\\]^_'", - "- '`abcdefghijklmno'", - "- 'pqrstuvwxyz{|}~\177'", - "- \200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", - "- \220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", - "- \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", - "- \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", - "- \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", - "- \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", - "- \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", - "- \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377", - "..." - ], - out => [ - "\0\1\2\3\4\5\6\a\b\t\n\13\f\r\16\17", - "\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37", - " !\"#\$%&'()*+,-./", - "0123456789:;<=>?", - "\@ABCDEFGHIJKLMNO", - "PQRSTUVWXYZ[\\]^_", - "`abcdefghijklmno", - "pqrstuvwxyz{|}~\177", - "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", - "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", - "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", - "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", - "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", - "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", - "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", - "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377" - ], - }, - { - name => 'Quoted hash keys', - in => [ - '---', ' "quoted": Magic!', ' "\n\t": newline, tab', '...', - ], - out => { - quoted => 'Magic!', - "\n\t" => 'newline, tab', - }, - }, - ); - - plan tests => @SCHEDULE * 5; -} - -sub iter { - my $ar = shift; - return sub { - return shift @$ar; - }; -} - -for my $test ( @SCHEDULE ) { - my $name = $test->{name}; - ok my $yaml = Data::YAML::Reader->new, "$name: Created"; - isa_ok $yaml, 'Data::YAML::Reader'; - - # diag "$name\n"; - - # unless ( $test->{in} ) { - # pass for 1 .. 2; - # use YAML; - # diag "Input for test:\n"; - # diag( Dump( $test->{out} ) ); - # next; - # } - - my $source = join( "\n", @{ $test->{in} } ) . "\n"; - - my $iter = iter( $test->{in} ); - my $got = eval { $yaml->read( $iter ) }; - - my $raw = $yaml->get_raw; - - if ( my $err = $test->{error} ) { - unless ( like $@, $err, "$name: Error message" ) { - diag "Error: $@\n"; - } - ok !$got, "$name: No result"; - pass; - } - else { - my $want = $test->{out}; - unless ( ok !$@, "$name: No error" ) { - diag "Error: $@\n"; - } - unless ( is_deeply $got, $want, "$name: Result matches" ) { - local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; - diag( Data::Dumper->Dump( [$got], ['$got'] ) ); - diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); - } - is $raw, $source, "$name: Captured source matches"; - } -} diff --git a/test/30-output.py b/test/30-output.py deleted file mode 100644 index 56cfb0a..0000000 --- a/test/30-output.py +++ /dev/null @@ -1,107 +0,0 @@ -use strict; -use warnings; -use Test::More tests => 9; -use Data::Dumper; - -use Data::YAML::Writer; - -my $out = [ - "---", - "bill-to:", - " address:", - " city: 'Royal Oak'", - " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", - " postal: 48046", - " state: MI", - " family: Dumars", - " given: Chris", - "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", - "date: 2001-01-23", - "invoice: 34843", - "product:", - " -", - " description: Basketball", - " price: 450.00", - " quantity: 4", - " sku: BL394D", - " -", - " description: 'Super Hoop'", - " price: 2392.00", - " quantity: 1", - " sku: BL4438H", - "tax: 251.42", - "total: 4443.52", - "...", -]; - -my $in = { - 'bill-to' => { - 'given' => 'Chris', - 'address' => { - 'city' => 'Royal Oak', - 'postal' => '48046', - 'lines' => "458 Walkman Dr.\nSuite #292\n", - 'state' => 'MI' - }, - 'family' => 'Dumars' - }, - 'invoice' => '34843', - 'date' => '2001-01-23', - 'tax' => '251.42', - 'product' => [ - { - 'sku' => 'BL394D', - 'quantity' => '4', - 'price' => '450.00', - 'description' => 'Basketball' - }, - { - 'sku' => 'BL4438H', - 'quantity' => '1', - 'price' => '2392.00', - 'description' => 'Super Hoop' - } - ], - 'comments' => - "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", - 'total' => '4443.52' -}; - -my @buf1 = (); -my @buf2 = (); -my $buf3 = ''; - -my @destination = ( - { - name => 'Array reference', - destination => \@buf1, - normalise => sub { return \@buf1 }, - }, - { - name => 'Closure', - destination => sub { push @buf2, shift }, - normalise => sub { return \@buf2 }, - }, - { - name => 'Scalar', - destination => \$buf3, - normalise => sub { - my @ar = split( /\n/, $buf3 ); - return \@ar; - }, - }, -); - -for my $dest ( @destination ) { - my $name = $dest->{name}; - ok my $yaml = Data::YAML::Writer->new, "$name: Created"; - isa_ok $yaml, 'Data::YAML::Writer'; - - $yaml->write( $in, $dest->{destination} ); - my $got = $dest->{normalise}->(); - unless ( is_deeply $got, $out, "$name: Result matches" ) { - local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; - diag( Data::Dumper->Dump( [$got], ['$got'] ) ); - diag( Data::Dumper->Dump( [$out], ['$expected'] ) ); - } -} diff --git a/test/40-writer.py b/test/40-writer.py deleted file mode 100644 index 4d70780..0000000 --- a/test/40-writer.py +++ /dev/null @@ -1,200 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Test::More; -use Data::Dumper; -use Data::YAML::Reader; -use Data::YAML::Writer; - -my @SCHEDULE; - -BEGIN { - @SCHEDULE = ( - { - name => 'Simple scalar', - in => 1, - out => [ '--- 1', '...', ], - }, - { - name => 'Undef', - in => undef, - out => [ '--- ~', '...', ], - }, - { - name => 'Unprintable', - in => "\x01\n\t", - out => [ '--- "\x01\n\t"', '...', ], - }, - { - name => 'Simple array', - in => [ 1, 2, 3 ], - out => [ '---', '- 1', '- 2', '- 3', '...', ], - }, - { - name => 'Array, two elements, undef', - in => [ undef, undef ], - out => [ '---', '- ~', '- ~', '...', ], - }, - { - name => 'Nested array', - in => [ 1, 2, [ 3, 4 ], 5 ], - out => - [ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ], - }, - { - name => 'Simple hash', - in => { one => '1', two => '2', three => '3' }, - out => [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ], - }, - { - name => 'Nested hash', - in => { - one => '1', - two => '2', - more => { three => '3', four => '4' } - }, - out => [ - '---', - 'more:', - ' four: 4', - ' three: 3', - 'one: 1', - 'two: 2', - '...', - ], - }, - { - name => 'Unprintable key', - in => { one => '1', "\x02" => '2', three => '3' }, - out => [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ], - }, - { - name => 'Empty key', - in => { '' => 'empty' }, - out => [ '---', "'': empty", '...', ], - }, - { - name => 'Empty value', - in => { '' => '' }, - out => [ '---', "'': ''", '...', ], - }, - { - name => 'Complex', - in => { - 'bill-to' => { - 'given' => 'Chris', - 'address' => { - 'city' => 'Royal Oak', - 'postal' => '48046', - 'lines' => "458 Walkman Dr.\nSuite #292\n", - 'state' => 'MI' - }, - 'family' => 'Dumars' - }, - 'invoice' => '34843', - 'date' => '2001-01-23', - 'tax' => '251.42', - 'product' => [ - { - 'sku' => 'BL394D', - 'quantity' => '4', - 'price' => '450.00', - 'description' => 'Basketball' - }, - { - 'sku' => 'BL4438H', - 'quantity' => '1', - 'price' => '2392.00', - 'description' => 'Super Hoop' - } - ], - 'comments' => - "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", - 'total' => '4443.52' - }, - out => [ - "---", - "bill-to:", - " address:", - " city: 'Royal Oak'", - " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", - " postal: 48046", - " state: MI", - " family: Dumars", - " given: Chris", - "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", - "date: 2001-01-23", - "invoice: 34843", - "product:", - " -", - " description: Basketball", - " price: 450.00", - " quantity: 4", - " sku: BL394D", - " -", - " description: 'Super Hoop'", - " price: 2392.00", - " quantity: 1", - " sku: BL4438H", - "tax: 251.42", - "total: 4443.52", - "...", - ], - }, - ); - - plan tests => @SCHEDULE * 5; -} - -sub iter { - my $ar = shift; - return sub { - return shift @$ar; - }; -} - -for my $test ( @SCHEDULE ) { - my $name = $test->{name}; - ok my $yaml = Data::YAML::Writer->new, "$name: Created"; - isa_ok $yaml, 'Data::YAML::Writer'; - - my $got = []; - my $writer = sub { push @$got, shift }; - - my $data = $test->{in}; - - eval { $yaml->write( $data, $writer ) }; - - if ( my $err = $test->{error} ) { - unless ( like $@, $err, "$name: Error message" ) { - diag "Error: $@\n"; - } - is_deeply $got, [], "$name: No result"; - pass; - } - else { - my $want = $test->{out}; - unless ( ok !$@, "$name: No error" ) { - diag "Error: $@\n"; - } - unless ( is_deeply $got, $want, "$name: Result matches" ) { - local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; - diag( Data::Dumper->Dump( [$got], ['$got'] ) ); - diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); - } - - my $yr = Data::YAML::Reader->new; - - # Now try parsing it - my $reader = sub { shift @$got }; - my $parsed = $yr->read( $reader ); - - unless ( is_deeply $parsed, $data, "$name: Reparse OK" ) { - local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; - diag( Data::Dumper->Dump( [$parsed], ['$parsed'] ) ); - diag( Data::Dumper->Dump( [$data], ['$data'] ) ); - } - } -} - diff --git a/test/all_tests.py b/test/all_tests.py new file mode 100644 index 0000000..f4f3bbe --- /dev/null +++ b/test/all_tests.py @@ -0,0 +1,11 @@ +import unittest +import load, input, reader, output, writer + +add_tests_from_class = unittest.TestLoader().loadTestsFromTestCase + +suite = unittest.TestSuite() +suite.addTest(add_tests_from_class(load.TestBasics)) +suite.addTest(add_tests_from_class(TestHappyPath)) +suite.addTest(add_tests_from_class(TestBadPath)) +suite.addTest(add_tests_from_class(TestPiglitData)) +suite.addTest(add_tests_from_class(TestMainArgsMgmt)) diff --git a/test/input.py b/test/input.py new file mode 100644 index 0000000..8f7e553 --- /dev/null +++ b/test/input.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +import logging +import unittest +import yamlish + +IN = """ + --- + bill-to: + address: + city: "Royal Oak" + lines: "458 Walkman Dr.\nSuite #292\n" + postal: 48046 + state: MI + family: Dumars + given: Chris + comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n" + date: 2001-01-23 + invoice: 34843 + product: + - + description: Basketball + price: 450.00 + quantity: 4 + sku: BL394D + - + description: "Super Hoop" + price: 2392.00 + quantity: 1 + sku: BL4438H + tax: 251.42 + total: 4443.52 + ... +""" + +OUT = { + 'bill-to': { + 'given': 'Chris', + 'address': { + 'city': 'Royal Oak', + 'postal': '48046', + 'lines': "458 Walkman Dr.\nSuite #292\n", + 'state': 'MI' + }, + 'family': 'Dumars' + }, + 'invoice': '34843', + 'date': '2001-01-23', + 'tax': '251.42', + 'product': [ + { + 'sku': 'BL394D', + 'quantity': '4', + 'price': '450.00', + 'description': 'Basketball' + }, + { + 'sku': 'BL4438H', + 'quantity': '1', + 'price': '2392.00', + 'description': 'Super Hoop' + } + ], + 'comments': + "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", + 'total': '4443.52' +} + +class TestInput(unittest.TestCase): + """FIXME description of this class""" + def test_reader(self): + scalar = IN + source = [ + { + "name": 'Array reference', + "source": IN.split("\n"), + }, +# { +# "name": 'Closure', +# "source": sub { shift @lines }, +# }, + { + "name": 'Scalar', + "source": IN, + } + ] + + for src in source: + name = src['name'] + yaml = yamlish.Reader() + self.assert_(True, "$name: Created") + self.assert_(isinstance(yaml, yamlish.Reader)) + + #my $got = eval { $yaml -> read($src -> {source}) }; + got = yaml.read(src['source']) + self.assertEqual(got, OUT, """%s: Result matches + expected = %s + + observed = %s + """ % (name, OUT, got)) + +if __name__ == "__main__": + unittest.main() diff --git a/test/load.py b/test/load.py new file mode 100644 index 0000000..e4f3f5f --- /dev/null +++ b/test/load.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +import unittest + +class TestBasics(unittest.TestCase): + def test_import(self): + import yamlish + from yamlish import Reader + from yamlish import Writer + self.assert_(True, + "Testing import of yamlish, version %s." % yamlish.__version__) diff --git a/test/output.py b/test/output.py new file mode 100644 index 0000000..56cfb0a --- /dev/null +++ b/test/output.py @@ -0,0 +1,107 @@ +use strict; +use warnings; +use Test::More tests => 9; +use Data::Dumper; + +use Data::YAML::Writer; + +my $out = [ + "---", + "bill-to:", + " address:", + " city: 'Royal Oak'", + " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", + " postal: 48046", + " state: MI", + " family: Dumars", + " given: Chris", + "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", + "date: 2001-01-23", + "invoice: 34843", + "product:", + " -", + " description: Basketball", + " price: 450.00", + " quantity: 4", + " sku: BL394D", + " -", + " description: 'Super Hoop'", + " price: 2392.00", + " quantity: 1", + " sku: BL4438H", + "tax: 251.42", + "total: 4443.52", + "...", +]; + +my $in = { + 'bill-to' => { + 'given' => 'Chris', + 'address' => { + 'city' => 'Royal Oak', + 'postal' => '48046', + 'lines' => "458 Walkman Dr.\nSuite #292\n", + 'state' => 'MI' + }, + 'family' => 'Dumars' + }, + 'invoice' => '34843', + 'date' => '2001-01-23', + 'tax' => '251.42', + 'product' => [ + { + 'sku' => 'BL394D', + 'quantity' => '4', + 'price' => '450.00', + 'description' => 'Basketball' + }, + { + 'sku' => 'BL4438H', + 'quantity' => '1', + 'price' => '2392.00', + 'description' => 'Super Hoop' + } + ], + 'comments' => + "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", + 'total' => '4443.52' +}; + +my @buf1 = (); +my @buf2 = (); +my $buf3 = ''; + +my @destination = ( + { + name => 'Array reference', + destination => \@buf1, + normalise => sub { return \@buf1 }, + }, + { + name => 'Closure', + destination => sub { push @buf2, shift }, + normalise => sub { return \@buf2 }, + }, + { + name => 'Scalar', + destination => \$buf3, + normalise => sub { + my @ar = split( /\n/, $buf3 ); + return \@ar; + }, + }, +); + +for my $dest ( @destination ) { + my $name = $dest->{name}; + ok my $yaml = Data::YAML::Writer->new, "$name: Created"; + isa_ok $yaml, 'Data::YAML::Writer'; + + $yaml->write( $in, $dest->{destination} ); + my $got = $dest->{normalise}->(); + unless ( is_deeply $got, $out, "$name: Result matches" ) { + local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; + diag( Data::Dumper->Dump( [$got], ['$got'] ) ); + diag( Data::Dumper->Dump( [$out], ['$expected'] ) ); + } +} diff --git a/test/pod-coverage.py b/test/pod-coverage.py deleted file mode 100644 index c412996..0000000 --- a/test/pod-coverage.py +++ /dev/null @@ -1,9 +0,0 @@ -#!perl -T - -use Test::More; -eval "use Test::Pod::Coverage 1.04"; -plan skip_all => - "Test::Pod::Coverage 1.04 required for testing POD coverage" - if $@; -all_pod_coverage_ok( - { private => [ qr{^BUILD|DEMOLISH|AUTOMETHOD|START$}, qr{^_} ] } ); diff --git a/test/pod_coverage.py b/test/pod_coverage.py new file mode 100644 index 0000000..c412996 --- /dev/null +++ b/test/pod_coverage.py @@ -0,0 +1,9 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => + "Test::Pod::Coverage 1.04 required for testing POD coverage" + if $@; +all_pod_coverage_ok( + { private => [ qr{^BUILD|DEMOLISH|AUTOMETHOD|START$}, qr{^_} ] } ); diff --git a/test/reader.py b/test/reader.py new file mode 100644 index 0000000..fc6806f --- /dev/null +++ b/test/reader.py @@ -0,0 +1,419 @@ +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +use Data::YAML::Reader; + +my @SCHEDULE; + +BEGIN { + @SCHEDULE = ( + { + name => 'Hello World', + in => [ '--- Hello, World', '...', ], + out => "Hello, World", + }, + { + name => 'Hello World 2', + in => [ '--- \'Hello, \'\'World\'', '...', ], + out => "Hello, 'World", + }, + { + name => 'Hello World 3', + in => [ '--- "Hello, World"', '...', ], + out => "Hello, World", + }, + { + name => 'Hello World 4', + in => [ '--- "Hello, World"', '...', ], + out => "Hello, World", + }, + { + name => 'Hello World 4', + in => [ '--- >', ' Hello,', ' World', '...', ], + out => "Hello, World\n", + }, + { + name => 'Hello World 5', + in => [ '--- >', ' Hello,', ' World', '...', ], + error => qr{Missing\s+'[.][.][.]'}, + }, + { + name => 'Simple array', + in => [ '---', '- 1', '- 2', '- 3', '...', ], + out => [ '1', '2', '3' ], + }, + { + name => 'Mixed array', + in => [ '---', '- 1', '- \'two\'', '- "three\n"', '...', ], + out => [ '1', 'two', "three\n" ], + }, + { + name => 'Hash in array', + in => [ '---', '- 1', '- two: 2', '- 3', '...', ], + out => [ '1', { two => '2' }, '3' ], + }, + { + name => 'Hash in array 2', + in => [ '---', '- 1', '- two: 2', ' three: 3', '- 4', '...', ], + out => [ '1', { two => '2', three => '3' }, '4' ], + }, + { + name => 'Nested array', + in => [ + '---', + '- one', + '-', + ' - two', + ' -', + ' - three', + ' - four', + '- five', + '...', + ], + out => [ 'one', [ 'two', ['three'], 'four' ], 'five' ], + }, + { + name => 'Nested hash', + in => [ + '---', + 'one:', + ' five: 5', + ' two:', + ' four: 4', + ' three: 3', + 'six: 6', + '...', + ], + out => { + one => { two => { three => '3', four => '4' }, five => '5' }, + six => '6' + }, + }, + + { + name => 'Original YAML::Tiny test', + in => [ + '---', + 'invoice: 34843', + 'date : 2001-01-23', + 'bill-to:', + ' given : Chris', + ' family : Dumars', + ' address:', + ' lines: |', + ' 458 Walkman Dr.', + ' Suite #292', + ' city : Royal Oak', + ' state : MI', + ' postal : 48046', + 'product:', + ' - sku : BL394D', + ' quantity : 4', + ' description : Basketball', + ' price : 450.00', + ' - sku : BL4438H', + ' quantity : 1', + ' description : Super Hoop', + ' price : 2392.00', + 'tax : 251.42', + 'total: 4443.52', + 'comments: >', + ' Late afternoon is best.', + ' Backup contact is Nancy', + ' Billsmer @ 338-4338', + '...', + ], + out => { + 'bill-to' => { + 'given' => 'Chris', + 'address' => { + 'city' => 'Royal Oak', + 'postal' => '48046', + 'lines' => "458 Walkman Dr.\nSuite #292\n", + 'state' => 'MI' + }, + 'family' => 'Dumars' + }, + 'invoice' => '34843', + 'date' => '2001-01-23', + 'tax' => '251.42', + 'product' => [ + { + 'sku' => 'BL394D', + 'quantity' => '4', + 'price' => '450.00', + 'description' => 'Basketball' + }, + { + 'sku' => 'BL4438H', + 'quantity' => '1', + 'price' => '2392.00', + 'description' => 'Super Hoop' + } + ], + 'comments' => + "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", + 'total' => '4443.52' + } + }, + + # Tests harvested from YAML::Tiny + { + in => ['...'], + name => 'Regression: empty', + error => qr{document\s+header\s+not\s+found} + }, + { + in => [ '# comment', '...' ], + name => 'Regression: only_comment', + error => qr{document\s+header\s+not\s+found} + }, + { + out => undef, + in => [ '---', '...' ], + name => 'Regression: only_header', + error => qr{Premature\s+end}i, + }, + { + out => undef, + in => [ '---', '---', '...' ], + name => 'Regression: two_header', + error => qr{Unexpected\s+start}i, + }, + { + out => undef, + in => [ '--- ~', '...' ], + name => 'Regression: one_undef' + }, + { + out => undef, + in => [ '--- ~', '...' ], + name => 'Regression: one_undef2' + }, + { + in => [ '--- ~', '---', '...' ], + name => 'Regression: two_undef', + error => qr{Missing\s+'[.][.][.]'}, + }, + { + out => 'foo', + in => [ '--- foo', '...' ], + name => 'Regression: one_scalar', + }, + { + out => 'foo', + in => [ '--- foo', '...' ], + name => 'Regression: one_scalar2', + }, + { + in => [ '--- foo', '--- bar', '...' ], + name => 'Regression: two_scalar', + error => qr{Missing\s+'[.][.][.]'}, + }, + { + out => ['foo'], + in => [ '---', '- foo', '...' ], + name => 'Regression: one_list1' + }, + { + out => [ 'foo', 'bar' ], + in => [ '---', '- foo', '- bar', '...' ], + name => 'Regression: one_list2' + }, + { + out => [ undef, 'bar' ], + in => [ '---', '- ~', '- bar', '...' ], + name => 'Regression: one_listundef' + }, + { + out => { 'foo' => 'bar' }, + in => [ '---', 'foo: bar', '...' ], + name => 'Regression: one_hash1' + }, + { + out => { + 'foo' => 'bar', + 'this' => undef + }, + in => [ '---', 'foo: bar', 'this: ~', '...' ], + name => 'Regression: one_hash2' + }, + { + out => { 'foo' => [ 'bar', undef, 'baz' ] }, + in => [ '---', 'foo:', ' - bar', ' - ~', ' - baz', '...' ], + name => 'Regression: array_in_hash' + }, + { + out => { + 'bar' => { 'foo' => 'bar' }, + 'foo' => undef + }, + in => [ '---', 'foo: ~', 'bar:', ' foo: bar', '...' ], + name => 'Regression: hash_in_hash' + }, + { + out => [ + { + 'foo' => undef, + 'this' => 'that' + }, + 'foo', undef, + { + 'foo' => 'bar', + 'this' => 'that' + } + ], + in => [ + '---', + '-', + ' foo: ~', + ' this: that', + '- foo', + '- ~', + '-', + ' foo: bar', + ' this: that', + '...' + ], + name => 'Regression: hash_in_array' + }, + { + out => ['foo'], + in => [ '---', '- \'foo\'', '...' ], + name => 'Regression: single_quote1' + }, + { + out => [' '], + in => [ '---', '- \' \'', '...' ], + name => 'Regression: single_spaces' + }, + { + out => [''], + in => [ '---', '- \'\'', '...' ], + name => 'Regression: single_null' + }, + { + out => ' ', + in => [ '--- " "', '...' ], + name => 'Regression: only_spaces' + }, + { + out => [ + undef, + { + 'foo' => 'bar', + 'this' => 'that' + }, + 'baz' + ], + in => + [ '---', '- ~', '- foo: bar', ' this: that', '- baz', '...' ], + name => 'Regression: inline_nested_hash' + }, + { + name => "Unprintables", + in => [ + "---", + "- \"\\z\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"", + "- \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\e\\x1c\\x1d\\x1e\\x1f\"", + "- \" !\\\"#\$%&'()*+,-./\"", + "- 0123456789:;<=>?", + "- '\@ABCDEFGHIJKLMNO'", + "- 'PQRSTUVWXYZ[\\]^_'", + "- '`abcdefghijklmno'", + "- 'pqrstuvwxyz{|}~\177'", + "- \200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", + "- \220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", + "- \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", + "- \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", + "- \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", + "- \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", + "- \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", + "- \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377", + "..." + ], + out => [ + "\0\1\2\3\4\5\6\a\b\t\n\13\f\r\16\17", + "\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37", + " !\"#\$%&'()*+,-./", + "0123456789:;<=>?", + "\@ABCDEFGHIJKLMNO", + "PQRSTUVWXYZ[\\]^_", + "`abcdefghijklmno", + "pqrstuvwxyz{|}~\177", + "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", + "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", + "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", + "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", + "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", + "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", + "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", + "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377" + ], + }, + { + name => 'Quoted hash keys', + in => [ + '---', ' "quoted": Magic!', ' "\n\t": newline, tab', '...', + ], + out => { + quoted => 'Magic!', + "\n\t" => 'newline, tab', + }, + }, + ); + + plan tests => @SCHEDULE * 5; +} + +sub iter { + my $ar = shift; + return sub { + return shift @$ar; + }; +} + +for my $test ( @SCHEDULE ) { + my $name = $test->{name}; + ok my $yaml = Data::YAML::Reader->new, "$name: Created"; + isa_ok $yaml, 'Data::YAML::Reader'; + + # diag "$name\n"; + + # unless ( $test->{in} ) { + # pass for 1 .. 2; + # use YAML; + # diag "Input for test:\n"; + # diag( Dump( $test->{out} ) ); + # next; + # } + + my $source = join( "\n", @{ $test->{in} } ) . "\n"; + + my $iter = iter( $test->{in} ); + my $got = eval { $yaml->read( $iter ) }; + + my $raw = $yaml->get_raw; + + if ( my $err = $test->{error} ) { + unless ( like $@, $err, "$name: Error message" ) { + diag "Error: $@\n"; + } + ok !$got, "$name: No result"; + pass; + } + else { + my $want = $test->{out}; + unless ( ok !$@, "$name: No error" ) { + diag "Error: $@\n"; + } + unless ( is_deeply $got, $want, "$name: Result matches" ) { + local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; + diag( Data::Dumper->Dump( [$got], ['$got'] ) ); + diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); + } + is $raw, $source, "$name: Captured source matches"; + } +} diff --git a/test/writer.py b/test/writer.py new file mode 100644 index 0000000..4d70780 --- /dev/null +++ b/test/writer.py @@ -0,0 +1,200 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use Data::YAML::Reader; +use Data::YAML::Writer; + +my @SCHEDULE; + +BEGIN { + @SCHEDULE = ( + { + name => 'Simple scalar', + in => 1, + out => [ '--- 1', '...', ], + }, + { + name => 'Undef', + in => undef, + out => [ '--- ~', '...', ], + }, + { + name => 'Unprintable', + in => "\x01\n\t", + out => [ '--- "\x01\n\t"', '...', ], + }, + { + name => 'Simple array', + in => [ 1, 2, 3 ], + out => [ '---', '- 1', '- 2', '- 3', '...', ], + }, + { + name => 'Array, two elements, undef', + in => [ undef, undef ], + out => [ '---', '- ~', '- ~', '...', ], + }, + { + name => 'Nested array', + in => [ 1, 2, [ 3, 4 ], 5 ], + out => + [ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ], + }, + { + name => 'Simple hash', + in => { one => '1', two => '2', three => '3' }, + out => [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ], + }, + { + name => 'Nested hash', + in => { + one => '1', + two => '2', + more => { three => '3', four => '4' } + }, + out => [ + '---', + 'more:', + ' four: 4', + ' three: 3', + 'one: 1', + 'two: 2', + '...', + ], + }, + { + name => 'Unprintable key', + in => { one => '1', "\x02" => '2', three => '3' }, + out => [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ], + }, + { + name => 'Empty key', + in => { '' => 'empty' }, + out => [ '---', "'': empty", '...', ], + }, + { + name => 'Empty value', + in => { '' => '' }, + out => [ '---', "'': ''", '...', ], + }, + { + name => 'Complex', + in => { + 'bill-to' => { + 'given' => 'Chris', + 'address' => { + 'city' => 'Royal Oak', + 'postal' => '48046', + 'lines' => "458 Walkman Dr.\nSuite #292\n", + 'state' => 'MI' + }, + 'family' => 'Dumars' + }, + 'invoice' => '34843', + 'date' => '2001-01-23', + 'tax' => '251.42', + 'product' => [ + { + 'sku' => 'BL394D', + 'quantity' => '4', + 'price' => '450.00', + 'description' => 'Basketball' + }, + { + 'sku' => 'BL4438H', + 'quantity' => '1', + 'price' => '2392.00', + 'description' => 'Super Hoop' + } + ], + 'comments' => + "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", + 'total' => '4443.52' + }, + out => [ + "---", + "bill-to:", + " address:", + " city: 'Royal Oak'", + " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", + " postal: 48046", + " state: MI", + " family: Dumars", + " given: Chris", + "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", + "date: 2001-01-23", + "invoice: 34843", + "product:", + " -", + " description: Basketball", + " price: 450.00", + " quantity: 4", + " sku: BL394D", + " -", + " description: 'Super Hoop'", + " price: 2392.00", + " quantity: 1", + " sku: BL4438H", + "tax: 251.42", + "total: 4443.52", + "...", + ], + }, + ); + + plan tests => @SCHEDULE * 5; +} + +sub iter { + my $ar = shift; + return sub { + return shift @$ar; + }; +} + +for my $test ( @SCHEDULE ) { + my $name = $test->{name}; + ok my $yaml = Data::YAML::Writer->new, "$name: Created"; + isa_ok $yaml, 'Data::YAML::Writer'; + + my $got = []; + my $writer = sub { push @$got, shift }; + + my $data = $test->{in}; + + eval { $yaml->write( $data, $writer ) }; + + if ( my $err = $test->{error} ) { + unless ( like $@, $err, "$name: Error message" ) { + diag "Error: $@\n"; + } + is_deeply $got, [], "$name: No result"; + pass; + } + else { + my $want = $test->{out}; + unless ( ok !$@, "$name: No error" ) { + diag "Error: $@\n"; + } + unless ( is_deeply $got, $want, "$name: Result matches" ) { + local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; + diag( Data::Dumper->Dump( [$got], ['$got'] ) ); + diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); + } + + my $yr = Data::YAML::Reader->new; + + # Now try parsing it + my $reader = sub { shift @$got }; + my $parsed = $yr->read( $reader ); + + unless ( is_deeply $parsed, $data, "$name: Reparse OK" ) { + local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; + diag( Data::Dumper->Dump( [$parsed], ['$parsed'] ) ); + diag( Data::Dumper->Dump( [$data], ['$data'] ) ); + } + } +} + -- cgit