perlancar / perl-Getopt-Long-More

1 stars 1 forks source link

Tau.feat.ghi 021 deprecate handler in favor of destination #22

Closed tabulon closed 5 years ago

tabulon commented 5 years ago

Hi @Perlancar,

As promised, here's a PR that is solely concentrated on #21 (handler => destination).

The PR contains 6 commits, described below, so as to ease cherry picking, if desired.

The last state reflects what I propose.

This small change does bring 2 new dependencies though...

Both of those are easy be remove at this time, if we want to. But they would probably come in handy later on, given the direction where things are headed.

Cheers, Tabulo[n]?


The GORY DETAILS

General REMARKS

The term handler practically dissapeared from sources

The map_args() function

You will notice addition of a utility package [Getopt::Long::More::Util] in the code; which currently contains a new little function map_args() which is employed for handling property deprecations and aliases.

I have done it in this way because it's a bit more generic, and also because I already had the bulk of it somewhere :-).

If desired, the body of map_args() can easily be incoporated into ::OptSpec::new(), or rewritten for just handler; but something tells me that we'll have to cater for synonyms of other stuff later on, if we ever wish to support easy migration from the many existing and disparate modules.

That being said, map_args() approach does carry a cost tag of a function call, though.

The COMMITS, explained

Changes BEFORE adding NEW TESTS

  1. 57e35fd: Implement #21: handler -> destination

    • The actual changes to code and docs for implementing #21.
    • No new tests.
    • Existing tests still pass, but some cause deprecation warnings for handler, as expected.
  2. 9ece031: Implement #21: handler->destination + adjust tests

    • ++ Adjust previously existing tests so that they use destination instead of handler. => Existing tests continue to pass; and this time without any warnings are triggered by the tests themselves.
      • Still, no new tests.
  3. de9b0b1: Implement #21: handler->destination + adjust tests + readjust docs ++ A few improvements to the docs (mainly concerning destination)

The NEW TESTS

The last three commits are mere variations of the same thing; i.e. adding a couple of tests to ensure that the deprecation itself is properly handled and warned about.

The essence is best captured in the last version, which goes as follows:

subtest "optspec: 'handler' is deprecated -> lives, but warns" => sub {
    warnings_exist {
      lives_ok { optspec( handler => sub { } ) }
    }
    [qr/\Whandler\W.*deprecated/],
    "optspec: 'handler' is deprecated -> warns";
};

subtest "optspec: Illegal to provide both 'destination' and its deprecated alias 'handler' -> dies" => sub {
    local *STDERR = \*STDOUT; # supress the depecation warning before 'die' => Just prettier test output.
    dies_ok { optspec(destination => sub {}, handler => sub {} ) };
};
  1. c6c76b2: Add tests specific to the deprecation of "handler"

    • Hand crafted version of the above tests
  2. 58b3590: Use Test::Warnings for some recent tests

    • Same thing with use [Test::Warnings], hence shorter and prettier,
  3. cfbbf3e: Use Test::Warn for some recent tests

    • Same thing with use [Test::Warn]; even shorter and prettier,