| 1 | use strictscip-perl cpan Catmandu 1.2025 strict/; |
| 2 | use warningsscip-perl cpan Catmandu 1.2025 warnings/; |
| 3 | use Test::Morescip-perl cpan Catmandu 1.2025 Test/More/; |
| 4 | use Test::Exceptionscip-perl cpan Catmandu 1.2025 Test/Exception/; |
| 5 | |
| 6 | my $pkg; |
| 7 | |
| 8 | BEGIN { |
| 9 | $pkg = 'Catmandu::Exporter::TSV'; |
| 10 | use_okscip-perl cpan Catmandu 1.2025 main/use_ok(). $pkg; |
| 11 | } |
| 12 | require_okscip-perl cpan Catmandu 1.2025 main/require_ok(). $pkg; |
| 13 | |
| 14 | my $data = [ |
| 15 | {'a' => 'moose', b => '1'}, |
| 16 | {'a' => 'pony', b => '2'}, |
| 17 | {'a' => 'shrimp', b => '3'} |
| 18 | ]; |
| 19 | my $out = ""; |
| 20 | |
| 21 | my $exporter = $pkg->newscip-perl cpan Catmandu 1.2025 main/new().(file => \$out); |
| 22 | isa_okscip-perl cpan Catmandu 1.2025 main/isa_ok(). $exporter, $pkg; |
| 23 | |
| 24 | $exporter->add_manyscip-perl cpan Catmandu 1.2025 main/add_many().($data); |
| 25 | $exporter->commitscip-perl cpan Catmandu 1.2025 main/commit().; |
| 26 | |
| 27 | my $tsv = <<EOF; |
| 28 | a\tb |
| 29 | moose\t1 |
| 30 | pony\t2 |
| 31 | shrimp\t3 |
| 32 | EOF |
| 33 | |
| 34 | isscip-perl cpan Catmandu 1.2025 main/is(). $out, $tsv, "TSV strings ok"; |
| 35 | isscip-perl cpan Catmandu 1.2025 main/is(). $exporter->countscip-perl cpan Catmandu 1.2025 main/count()., 3, "Count ok"; |
| 36 | |
| 37 | $data = [{b => '1'}, {'a' => 'pony', b => '2'}, {'a' => 'shrimp', b => '3'}]; |
| 38 | $out = ""; |
| 39 | $exporter = $pkg->newscip-perl cpan Catmandu 1.2025 main/new().(file => \$out); |
| 40 | $exporter->add_manyscip-perl cpan Catmandu 1.2025 main/add_many().($data); |
| 41 | $exporter->commitscip-perl cpan Catmandu 1.2025 main/commit().; |
| 42 | $tsv = <<EOF; |
| 43 | b |
| 44 | 1 |
| 45 | 2 |
| 46 | 3 |
| 47 | EOF |
| 48 | isscip-perl cpan Catmandu 1.2025 main/is(). $out, $tsv, "first record determines fields without collect"; |
| 49 | |
| 50 | $out = ""; |
| 51 | $exporter = $pkg->newscip-perl cpan Catmandu 1.2025 main/new().(file => \$out, collect_fields => 1); |
| 52 | $exporter->add_manyscip-perl cpan Catmandu 1.2025 main/add_many().($data); |
| 53 | $exporter->commitscip-perl cpan Catmandu 1.2025 main/commit().; |
| 54 | $tsv = <<EOF; |
| 55 | a\tb |
| 56 | \t1 |
| 57 | pony\t2 |
| 58 | shrimp\t3 |
| 59 | EOF |
| 60 | isscip-perl cpan Catmandu 1.2025 main/is(). $out, $tsv, "collect field names"; |
| 61 | |
| 62 | $out = ""; |
| 63 | $exporter |
| 64 | = $pkg->newscip-perl cpan Catmandu 1.2025 main/new().(fields => 'a,x', columns => 'Longname,X', file => \$out); |
| 65 | $exporter->addscip-perl cpan Catmandu 1.2025 main/add().({a => 'Hello', b => 'World'}); |
| 66 | $tsv = "Longname\tX\nHello\t\n"; |
| 67 | isscip-perl cpan Catmandu 1.2025 main/is(). $out, $tsv, "custom column names"; |
| 68 | |
| 69 | $out = ""; |
| 70 | my $fixer = Catmandu->fixerscip-perl cpan Catmandu 1.2025 Catmandu/fixer().('if exists(foo) reject() end'); |
| 71 | my $importer = Catmandu->importerscip-perl cpan Catmandu 1.2025 Catmandu/importer().('JSON', file => 't/csv_test.json'); |
| 72 | |
| 73 | $exporter = $pkg->newscip-perl cpan Catmandu 1.2025 main/new().(file => \$out); |
| 74 | $exporter->add_manyscip-perl cpan Catmandu 1.2025 main/add_many().($fixscip-perl cpan Catmandu 1.2025 Catmandu/fix().er->fix($importer)); |
| 75 | $tsv = "fob\ntest\n"; |
| 76 | isscip-perl cpan Catmandu 1.2025 main/is(). $out, $tsv, "custom column names as HASH with reject fix"; |
| 77 | |
| 78 | done_testing; |