| 1 | #!/usr/bin/env perl |
| 2 | use strictscip-perl cpan Catmandu 1.2025 strict/; |
| 3 | use warningsscip-perl cpan Catmandu 1.2025 warnings/; |
| 4 | use Test::Morescip-perl cpan Catmandu 1.2025 Test/More/; |
| 5 | use Test::Exceptionscip-perl cpan Catmandu 1.2025 Test/Exception/; |
| 6 | use Catmandu::Fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/; |
| 7 | use Catmandu::Importer::Mockscip-perl cpan Catmandu 1.2025 Catmandu/Importer/Mock/; |
| 8 | use Catmandu::Utilscip-perl cpan Catmandu 1.2025 Catmandu/Util/ qw(:is); |
| 9 | |
| 10 | my $pkg; |
| 11 | |
| 12 | BEGIN { |
| 13 | $pkg = 'Catmandu::Fix::Bind::identity'; |
| 14 | use_okscip-perl cpan Catmandu 1.2025 main/use_ok(). $pkg; |
| 15 | } |
| 16 | require_okscip-perl cpan Catmandu 1.2025 main/require_ok(). $pkg; |
| 17 | |
| 18 | my $monad = Catmandu::Fix::Bind::identity->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/new().(); |
| 19 | my $f = sub {$_scip-perl cpan Catmandu 1.2025 main/`@_`.[0]->{demo} = 1; $_scip-perl cpan Catmandu 1.2025 main/`@_`.[0]}; |
| 20 | my $g = sub {$_scip-perl cpan Catmandu 1.2025 main/`@_`.[0]->{demo} += 1; $_scip-perl cpan Catmandu 1.2025 main/`@_`.[0]}; |
| 21 | |
| 22 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().({}), $f), $f->({})scip-perl cpan Catmandu 1.2025 main/`->()`()., |
| 23 | "left unit monadic law"; |
| 24 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().({}), sub {$monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().(shiftscip-perl cpan Catmandu 1.2025 main/shift().)}), |
| 25 | $monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().({}), "right unit monadic law"; |
| 26 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().({}), $f), $g), |
| 27 | $monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($monad->unitscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/unit().({}), sub {$monad->bindscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Bind/identity/bind().($f->(scip-perl cpan Catmandu 1.2025 main/`->()`().$_scip-perl cpan Catmandu 1.2025 main/`@_`.[0])scip-perl cpan Catmandu 1.2025 main/`->()`()., $g)}), |
| 28 | "associative monadic law"; |
| 29 | |
| 30 | my $fixes = <<EOF; |
| 31 | do identity() |
| 32 | add_field(foo,bar) |
| 33 | end |
| 34 | EOF |
| 35 | |
| 36 | my $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 37 | |
| 38 | okscip-perl cpan Catmandu 1.2025 main/ok(). $fixer, 'create fixer'; |
| 39 | |
| 40 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({}), {foo => 'bar'}, 'testing add_field'; |
| 41 | |
| 42 | $fixes = <<EOF; |
| 43 | do identity() |
| 44 | end |
| 45 | EOF |
| 46 | |
| 47 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 48 | |
| 49 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({foo => 'bar'}), {foo => 'bar'}, |
| 50 | 'testing zero fix functions'; |
| 51 | |
| 52 | $fixes = <<EOF; |
| 53 | do identity() |
| 54 | unless exists(foo) |
| 55 | add_field(foo,bar) |
| 56 | end |
| 57 | end |
| 58 | EOF |
| 59 | |
| 60 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 61 | |
| 62 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({}), {foo => 'bar'}, 'testing unless'; |
| 63 | |
| 64 | $fixes = <<EOF; |
| 65 | do identity() |
| 66 | if exists(foo) |
| 67 | add_field(foo2,bar) |
| 68 | end |
| 69 | end |
| 70 | EOF |
| 71 | |
| 72 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 73 | |
| 74 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({foo => 'bar'}), {foo => 'bar', foo2 => 'bar'}, |
| 75 | 'testing if'; |
| 76 | |
| 77 | $fixes = <<EOF; |
| 78 | do identity() |
| 79 | reject exists(foo) |
| 80 | end |
| 81 | EOF |
| 82 | |
| 83 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 84 | |
| 85 | ok !definedscip-perl cpan Catmandu 1.2025 main/defined(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({foo => 'bar'}), 'testing reject'; |
| 86 | |
| 87 | $fixes = <<EOF; |
| 88 | do identity() |
| 89 | select exists(foo) |
| 90 | end |
| 91 | EOF |
| 92 | |
| 93 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 94 | |
| 95 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({foo => 'bar'}), {foo => 'bar'}, 'testing select'; |
| 96 | |
| 97 | $fixes = <<EOF; |
| 98 | do identity() |
| 99 | do identity() |
| 100 | do identity() |
| 101 | add_field(foo,bar) |
| 102 | end |
| 103 | end |
| 104 | end |
| 105 | EOF |
| 106 | |
| 107 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 108 | |
| 109 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({}), {foo => 'bar'}, 'testing nesting'; |
| 110 | |
| 111 | $fixes = <<EOF; |
| 112 | add_field(before,ok) |
| 113 | do identity() |
| 114 | add_field(inside,ok) |
| 115 | end |
| 116 | add_field(after,ok) |
| 117 | EOF |
| 118 | |
| 119 | $fixer = Catmandu::Fix->newscip-perl cpan Catmandu 1.2025 Catmandu/Fix/new().(fixes => [$fixes]); |
| 120 | |
| 121 | is_deeplyscip-perl cpan Catmandu 1.2025 main/is_deeply(). $fixscip-perl cpan Catmandu 1.2025 Catmandu/Fix/fix().er->fix({foo => 'bar'}), |
| 122 | {foo => 'bar', before => 'ok', inside => 'ok', after => 'ok'}, |
| 123 | 'before/after testing'; |
| 124 | |
| 125 | done_testing 14; |