blib/lib/Mac/OSVersion/Lite.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 95 SCAR 42.8
CC
8
Cov
0%
new
20.0
_init_by_version_string
12.0
_init_by_current_version
6.0
CRAP 72.0
1 package Mac::OSVersion::Lite;
2 use strict;
3 use warnings;
4 use utf8;
5  
6 our $VERSION = "0.09";
7  
8 use constant VERSION_FORMAT => qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<point>[0-9]+))?/;
9 use constant MAC_VERSION_NAMES => {
10 mojave => "10.14",
11 high_sierra => "10.13",
12 sierra => "10.12",
13 el_capitan => "10.11",
14 yosemite => "10.10",
15 mavericks => "10.9",
16 mountain_lion => "10.8",
17 lion => "10.7",
18 snow_leopard => "10.6",
19 leopard => "10.5",
20 tiger => "10.4",
21 };
22  
23 use overload (
24 q{""} => \&as_string,
25 q{<=>} => \&_cmp,
26 fallback => 1,
27 );
28  
29 sub major { shift->{major} }
30 sub minor { shift->{minor} }
31  
32 sub new {
33 my $class = shift;
34 my $self = bless {} => $class;
35  
36 $self->_init_by_current_version if @_ == 0;
37 $self->_init_by_version_string(@_) if @_ == 1;
38 $self->_init_by_version_numbers(@_) if @_ >= 2;
39  
40 return $self;
41 }
42  
43 sub _init_by_current_version {
44 my $self = shift;
45 my $command = '/usr/bin/sw_vers -productVersion';
46 my $version = `$command`;
47  
48 die "Command \`$command\` failed: $version (exit code: $?)\n" if $? != 0;
49  
50 $self->_init_by_version_string($version);
51 }
52  
53 sub _init_by_version_string {
54 my ($self, $string) = @_;
55  
56 if (defined MAC_VERSION_NAMES->{$string}) {
57 $string = MAC_VERSION_NAMES->{$string};
58 }
59  
60 die "Invalid format: $string\n" unless $string =~ qr/^@{[VERSION_FORMAT]}$/;
61  
62 $self->{major} = $+{major};
63 $self->{minor} = $+{minor} // 0;
64 }
65  
66 sub _init_by_version_numbers {
67 my ($self, $major, $minor) = @_;
68  
69 $self->{major} = $major // 0;
70 $self->{minor} = $minor // 0;
71 }
72  
73 sub name {
74 my $self = shift;
75 my %map = reverse %{ MAC_VERSION_NAMES() };
76 return $map{$self->as_string};
77 }
78  
79 sub as_string {
80 my $self = shift;
81 return $self->{major}.'.'.$self->{minor};
82 }
83  
84 sub _cmp {
85 my ($self, $other) = @_;
86  
87 return $self->{major} <=> $other->{major} if $self->{major} != $other->{major};
88 return $self->{minor} <=> $other->{minor};
89 }
90  
91 1;
92 __END__
93  
94 =encoding utf-8
95  
96 =head1 NAME
97  
98 Mac::OSVersion::Lite - It's the lightweight version object for Mac OS X
99  
100 =head1 SYNOPSIS
101  
102 use Mac::OSVersion::Lite;
103 use feature qw/say/;
104  
105 my $version = Mac::OSVersion::Lite->new;
106 say $version->major; # 10
107 say $version->minor; # 11
108 say $version->name; # el_capitan
109  
110 =head1 DESCRIPTION
111  
112 Mac::OSVersion::Lite is the lightweight version object for Mac OS X with auto detection.
113  
114 =head1 METHODS
115  
116 =head2 CLASS METHODS
117  
118 =head3 C<new()>
119  
120 Create new C<Mac::OSVersion::Lite> instance with auto detection.
121  
122 =head3 C<new($version_string)>
123  
124 Create new C<Mac::OSVersion::Lite> instance from a version string.
125 C<Mac::OSVersion::Lite-E<gt>new('10.11')> equals C<Mac::OSVersion::Lite-E<gt>new(10, 11)>.
126  
127 =head3 C<new($major, $minor = 0)>
128  
129 Create new C<Mac::OSVersion::Lite> instance from version numbers.
130 C<Mac::OSVersion::Lite-E<gt>new(10, 11)> equals C<Mac::OSVersion::Lite-E<gt>new('10.11')>.
131  
132 =head2 METHODS
133  
134 =head3 C<major>
135  
136 Get the major version number.
137  
138 =head3 C<minor>
139  
140 Return the minor version number.
141  
142 =head3 C<E<lt>=E<gt>>
143  
144 Compare two C<SemVer::V2::Strict> instances.
145  
146 =head3 C<"">
147  
148 Convert a C<SemVer::V2::Strict> instance to string.
149  
150 =head3 C<as_string()>
151  
152 Convert a C<SemVer::V2::Strict> instance to string.
153  
154 =head1 SEE ALSO
155  
156 =over
157  
158 =item * L<Mac::OSVersion>
159  
160 =back
161  
162 =head1 LICENSE
163  
164 The MIT License (MIT)
165  
166 Copyright (c) 2017-2019 Pine Mizune
167  
168 Permission is hereby granted, free of charge, to any person obtaining a copy
169 of this software and associated documentation files (the "Software"), to deal
170 in the Software without restriction, including without limitation the rights
171 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
172 copies of the Software, and to permit persons to whom the Software is
173 furnished to do so, subject to the following conditions:
174  
175 The above copyright notice and this permission notice shall be included in
176 all copies or substantial portions of the Software.
177  
178 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
179 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
180 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
181 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
182 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
183 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
184 THE SOFTWARE.
185  
186 =head1 AUTHOR
187  
188 Pine Mizune E<lt>pinemz@gmail.comE<gt>
189  
190 =cut
191