Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

testfiles, more documentation

  • Loading branch information...
commit 4486d06efcd32afabd796b92b6d8b5a4b51a2c05 1 parent f2b0f27
@mnunberg authored
Showing with 114 additions and 5 deletions.
  1. +2 −0  MANIFEST
  2. +56 −5 lib/Constant/Generate.pm
  3. +56 −0 t/00-all.t
View
2  MANIFEST
@@ -3,3 +3,5 @@ MANIFEST
Makefile.PL
README
lib/Constant/Generate.pm
+t/00-all.t
+test.pl
View
61 lib/Constant/Generate.pm
@@ -1,8 +1,8 @@
package Constant::Generate;
use strict;
use warnings;
-use Log::Fu { level => "debug" };
our $VERSION = 0.01;
+
#these two functions produce reverse mapping, one for simple constants, and
#one for bitfields
@@ -123,7 +123,6 @@ sub import {
}
if(($auto_export || $auto_export_ok) && defined $export_tag) {
- log_err("Hi!");
if(!defined ($h_etags ||= *{$reqpkg ."::EXPORT_TAGS"}{HASH}) ) {
die "Requested export with tags, but \%EXPORT_TAGS is not yet declared";
} else {
@@ -134,8 +133,6 @@ sub import {
if(%opts) {
die "Unknown keys " . join(",", keys %opts);
}
- use Data::Dumper;
- print Dumper(\%symhash);
}
__END__
@@ -290,4 +287,58 @@ objects corresponding to the appropriate variables.
If references are not used as values for these options, C<Constant::Generate>
will expect you to have defined these modules already, and otherwise die.
-=back
+=back
+
+=head3 EXPORTING
+
+This module also allows you to define a 'constants' module of your own, from which
+you can export constants to other files in your package. Figuring out the right
+exporter parameters is quite hairy, and the export options can natually
+be a bit tricky.
+
+In order to succesfully export symbols made by this module, you must specify
+either C<-export_ok> or C<-export> as hash options to C<import>. These correspond
+to the like-named variables documented by L<Exporter>.
+
+Additionally, export tags can be specified only if one of the C<export> flags is
+set to true (again, following the behavior of C<Exporter>). The auto-export
+feature is merely one of syntactical convenience, but these three forms are
+effectively equivalent
+
+Nicest way:
+
+ use base qw(Exporter);
+ our (@EXPORT, %EXPORT_TAGS);
+ use Constant::Generate
+ [qw(FOO BAR BAZ)],
+ -export => 1,
+ -tag => "some_constants"
+ ;
+
+A bit more explicit:
+
+ use base qw(Exporter);
+ use Constant::Generate
+ [qw(FOO BAR BAZ)],
+ -export => \our @EXPORT,
+ -export_tags => \our %EXPORT_TAGS,
+ -tag => "some_constants",
+ -mapname => "some_constants_to_str",
+ ;
+
+
+Or DIY
+
+ use base qw(Exporter);
+ our @EXPORT;
+ my @SYMS;
+ BEGIN {
+ @SYMS = qw(FOO BAR BAZ);
+ }
+
+ use Constant::Generate \@SYMS, -mapname => "some_constants_to_str";
+
+ push @EXPORT, @SYMS, "some_constants_to_str";
+ $EXPORT_TAGS{'some_constants'} = [@SYMS, "some_constants_to_str"];
+
+etc.
View
56 t/00-all.t
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+package Constants;
+use strict;
+use warnings;
+
+use base qw(Exporter);
+our (@EXPORT,@EXPORT_OK, %EXPORT_TAGS);
+use Constant::Generate
+ [qw(CONST_ONE CONST_TWO)],
+ -start_at => 1;
+
+use Test::More;
+
+ok(CONST_ONE == 1 && CONST_TWO == 2, "Integer counters");
+
+use Constant::Generate
+ [qw(FLAG_ONE FLAG_TWO)],
+ -type => "bitfield",
+ -mapname => "bit_to_str";
+
+ok(FLAG_ONE == 1 << 0 && FLAG_TWO == 1 << 1, "Bitfield counters");
+my $s = bit_to_str(FLAG_ONE|FLAG_TWO);
+ok($s =~ /ONE/ && $s =~ /TWO/, "Bitfield strings");
+
+use Constant::Generate {
+ FOO => 42,
+ BAR => 666,
+},
+ -type => "integer",
+ -mapname => "int_to_str";
+
+ok(FOO == 42 && BAR == 666, "Manual values");
+ok(int_to_str(FOO) eq 'FOO' && int_to_str(BAR) eq 'BAR', "Manual stringify");
+
+use Constant::Generate
+ [qw(EXPORTED_FOO EXPORTED_BAR EXPORTED_BAZ)],
+ -type => "bitfield",
+ -start_at => 4,
+ -tag => 'exconst',
+ -export_ok => 1,
+ -export_tags => 1;
+
+package User;
+use Test::More;
+
+BEGIN {
+ Constants->import(':exconst');
+}
+
+my $flags = EXPORTED_FOO | EXPORTED_BAR | EXPORTED_BAZ;
+ok(
+ exconst_to_str($flags) =~ /FOO/ &&
+ exconst_to_str($flags) =~ /BAR/ &&
+ exconst_to_str($flags) =~ /BAZ/,
+ "Exported");
+done_testing();
Please sign in to comment.
Something went wrong with that request. Please try again.