Skip to content

Commit

Permalink
Lose the "lib" prefix in tests
Browse files Browse the repository at this point in the history
Allow for version libraries
  • Loading branch information
jonathanstowe committed Dec 24, 2015
1 parent d142e8c commit a6daf87
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion META.info
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"Test"
],
"description" : "Quick hack to determine whether a shared libray is present",
"version" : "v0.0.5"
"version" : "v0.0.6"
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ are possible.

use LibraryCheck;

if !library-exists('libsndfile') {
die "Cannot load libsndfile";
if !library-exists('sndfile', v1) {
die "Cannot load sndfile";
}

The case above can be more simply written as:

library-check('libsndfile', :exception);
library-check('sndfile',v1, :exception);

Which will throw an ```X::NoLibrary``` exception rather than return False.

Expand Down
15 changes: 11 additions & 4 deletions lib/LibraryCheck.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LibraryCheck - check for the existence of a shared library
use LibraryCheck;
if !library-exists('libsndfile') {
if !library-exists('sndfile', v1) {
die "Cannot load libsndfile";
}
Expand All @@ -24,7 +24,14 @@ This module provides a mechanism that will determine whether a named
shared library is available and can be used by NativeCall.
It exports a single function 'library-exists' that returns a boolean to
indicate whether the named shared library can be loaded and used.
indicate whether the named shared library can be loaded and used.
The library name should be supplied without any (OS dependent,) "lib"
prefix and no extension, (so e.g. 'crypt' for 'libcrypt.so' etc.)
The second positional argument is a L<Version> object that indicates the
version of the library that is required, it defaults to C<v1>, if you don't
care which version exists then it possible to pass a new Version object
without an version parts (i.e. C<Version.new()>.)
If the ':exception' adverb is passed to C<library-exists> then an
exception (C<X::NoLibrary>) will be thrown if the library isn't availabe
Expand Down Expand Up @@ -52,12 +59,12 @@ module LibraryCheck {
}
}

sub library-exists(Str $lib, :$exception --> Bool) is export {
sub library-exists(Str $lib, Version $v = v1, :$exception --> Bool) is export {
my $rc = True;

use MONKEY-SEE-NO-EVAL;
my $name = ("a".."z","A".."Z").flat.pick(15).join("");
my $f = EVAL("sub $name\(\) is native(\{'$lib'\}) \{ * \}");
my $f = EVAL("sub $name\(\) is native('$lib', { $v.gist }) \{ * \}");
try {
$f();
CATCH {
Expand Down
8 changes: 4 additions & 4 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use lib 'lib';
use LibraryCheck;

todo("this is not at all cross platform");
ok(library-exists('libcrypt'), "ok for a known existing library");
ok(!library-exists('libXzippyYayaya'), "not ok for a bogus one");
ok(library-exists('crypt'), "ok for a known existing library");
ok(!library-exists('XzippyYayaya'), "not ok for a bogus one");
todo("this is not at all cross platform");
lives-ok { library-exists('libcrypt', :exception) }, "ok for a known existing library with :exception";
throws-like { library-exists('libXzippyYayaya', :exception) }, X::NoLibrary, "bogus one throws X::NoLibrary with :exception";
lives-ok { library-exists('crypt', :exception) }, "ok for a known existing library with :exception";
throws-like { library-exists('XzippyYayaya', :exception) }, X::NoLibrary, "bogus one throws X::NoLibrary with :exception";

done-testing();
# vim: expandtab shiftwidth=4 ft=perl6

0 comments on commit a6daf87

Please sign in to comment.