Issue1163 - Convert BashPrimaryExpressions to use template

master
Mailkov 2015-12-18 19:53:18 +00:00
parent da24a67c1e
commit f28f137e92
4 changed files with 168 additions and 65 deletions

View File

@ -13,8 +13,8 @@ secondary_example_queries 'bash if [[ "abc" -lt "cba" ]]';
description 'Bash Primary Expressions Help';
name 'Bash Help';
source 'http://tille.garrels.be/training/bash/ch07.html';
attribution github => [ 'http://github.com/mintsoft', 'Rob Emery' ];
attribution github => [ 'http://github.com/mintsoft', 'Rob Emery' ],
github => ['https://github.com/Mailkov', 'Melchiorre Alastra'];
category 'computing_tools';
topics 'sysadmin';
@ -22,43 +22,43 @@ zci answer_type => 'expression_description';
zci is_cached => 1;
our %if_description = (
'-a' => "true if ARG2 exists",
'-b' => "true if ARG2 exists and is a block-special file",
'-c' => "true if ARG2 exists and is a character-special file",
'-d' => "true if ARG2 exists and is a directory",
'-e' => "true if ARG2 exists",
'-f' => "true if ARG2 exists and is a regular file",
'-g' => "true if ARG2 exists and its SGID bit is set",
'-h' => "true if ARG2 exists and is a symbolic link",
'-k' => "true if ARG2 exists and its sticky bit is set",
'-p' => "true if ARG2 exists and is a named pipe (FIFO)",
'-r' => "true if ARG2 exists and is readable",
'-s' => "true if ARG2 exists and has a size greater than zero",
'-t' => "true if ARG2 descriptor FD is open and refers to a terminal",
'-u' => "true if ARG2 exists and its SUID (set user ID) bit is set",
'-w' => "true if ARG2 exists and is writable",
'-x' => "true if ARG2 exists and is executable",
'-O' => "true if ARG2 exists and is owned by the effective user ID",
'-G' => "true if ARG2 exists and is owned by the effective group ID",
'-L' => "true if ARG2 exists and is a symbolic link",
'-N' => "true if ARG2 exists and has been modified since it was last read",
'-S' => "true if ARG2 exists and is a socket",
'-o' => "true if shell option ARG2 is enabled",
'-z' => "true if the length of 'ARG2' is zero",
'-n' => "true if the length of 'ARG2' is non-zero",
'==' => "true if the strings ARG1 and ARG2 are equal",
'!=' => "true if the strings ARG1 and ARG2 are not equal",
'<' => "true if ARG1 string-sorts before ARG2 in the current locale",
'>' => "true if ARG1 string-sorts after ARG2 in the current locale",
'-eq' => "true if ARG1 and ARG2 are numerically equal",
'-ne' => "true if ARG1 and ARG2 are not numerically equal",
'-lt' => "true if ARG1 is numerically less than ARG2",
'-le' => "true if ARG1 is numerically less than or equal to ARG2",
'-gt' => "true if ARG1 is numerically greater than ARG2",
'-ge' => "true if ARG1 is numerically greater than or equal to ARG2",
'-nt' => "true if ARG1 has been changed more recently than ARG2 or if ARG1 exists and ARG2 does not",
'-ot' => "true if ARG1 is older than ARG2 or ARG2 exists and ARG1 does not",
'-ef' => "true if ARG1 and ARG2 refer to the same device and inode numbers"
'-a' => ["true if ", "ARG2", " exists"],
'-b' => ["true if ", "ARG2", " exists and is a block-special file"],
'-c' => ["true if ", "ARG2", " exists and is a character-special file"],
'-d' => ["true if ", "ARG2", " exists and is a directory"],
'-e' => ["true if ", "ARG2", " exists"],
'-f' => ["true if ", "ARG2", " exists and is a regular file"],
'-g' => ["true if ", "ARG2", " exists and its SGID bit is set"],
'-h' => ["true if ", "ARG2", " exists and is a symbolic link"],
'-k' => ["true if ", "ARG2", " exists and its sticky bit is set"],
'-p' => ["true if ", "ARG2", " exists and is a named pipe (FIFO)"],
'-r' => ["true if ", "ARG2", " exists and is readable"],
'-s' => ["true if ", "ARG2", " exists and has a size greater than zero"],
'-t' => ["true if ", "ARG2", " descriptor FD is open and refers to a terminal"],
'-u' => ["true if ", "ARG2", " exists and its SUID (set user ID) bit is set"],
'-w' => ["true if ", "ARG2", " exists and is writable"],
'-x' => ["true if ", "ARG2", " exists and is executable"],
'-O' => ["true if ", "ARG2", " exists and is owned by the effective user ID"],
'-G' => ["true if ", "ARG2", " exists and is owned by the effective group ID"],
'-L' => ["true if ", "ARG2", " exists and is a symbolic link"],
'-N' => ["true if ", "ARG2", " exists and has been modified since it was last read"],
'-S' => ["true if ", "ARG2", " exists and is a socket"],
'-o' => ["true if shell option ", "ARG2", " is enabled"],
'-z' => ["true if the length of '", "ARG2", "' is zero"],
'-n' => ["true if the length of '", "ARG2", "' is non-zero"],
'==' => ["true if the strings ", "ARG1", " and ", "ARG2", " are equal"],
'!=' => ["true if the strings ", "ARG1", " and ", "ARG2", " are not equal"],
'<' => ["true if ", "ARG1", " string-sorts before ", "ARG2", " in the current locale"],
'>' => ["true if ", "ARG1", " string-sorts after ", "ARG2", " in the current locale"],
'-eq' => ["true if ", "ARG1", " and ", "ARG2", " are numerically equal"],
'-ne' => ["true if ", "ARG1", " and ", "ARG2", " are not numerically equal"],
'-lt' => ["true if ", "ARG1", " is numerically less than ", "ARG2"],
'-le' => ["true if ", "ARG1", " is numerically less than or equal to ", "ARG2"],
'-gt' => ["true if ", "ARG1", " is numerically greater than ", "ARG2"],
'-ge' => ["true if ", "ARG1", " is numerically greater than or equal to ", "ARG2"],
'-nt' => ["true if ", "ARG1", " has been changed more recently than ", "ARG2", " or if ", "ARG1", " exists and ", "ARG2", " does not"],
'-ot' => ["true if ", "ARG1", " is older than ", "ARG2", " or ", "ARG2", " exists and ", "ARG1", " does not"],
'-ef' => ["true if ", "ARG1", " and ", "ARG2", " refer to the same device and inode numbers"]
);
handle remainder => sub {
@ -78,24 +78,49 @@ handle remainder => sub {
return unless ($op && $right_arg);
return unless $if_description{$op};
my $text_output = $if_description{$op};
my $text_output = join("",@{$if_description{$op}});
$text_output =~ s/^true/false/ if $not;
my $html_output = html_enc($text_output);
my $html_right_arg = html_enc($right_arg);
if ($left_arg) {
my $html_left_arg = html_enc($left_arg);
$text_output =~ s/ARG1/$left_arg/g;
$html_output =~ s/ARG1/<pre>$html_left_arg<\/pre>/g;
}
$text_output =~ s/ARG2/$right_arg/g;
$html_output =~ s/ARG2/<pre>$html_right_arg<\/pre>/g;
my $intro = "The Bash expression <pre>" . html_enc($_) . "</pre> results to";
return "$intro $text_output.", html => "$intro $html_output.", heading => html_enc($_) . " (Bash)";
my @results;
foreach my $el(@{$if_description{$op}}) {
my $temp;
if ($el eq 'ARG1') {
$temp->{text} = "";
$temp->{value} = $left_arg;
} elsif ($el eq 'ARG2') {
$temp->{text} = "";
$temp->{value} = $right_arg;
} else {
$temp->{text} = $el;
$temp->{value} = "";
}
push @results, $temp;
}
my $intro = "The Bash expression $_ results to";
return "$intro $text_output.",
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => {
intro => $_,
results => \@results
},
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
};
};
1;

View File

@ -1,4 +1,4 @@
.zci--answer .zci__body pre {
.zci--bash_primary_expressions pre {
padding: 0;
margin-right: 0;
display: inline;

View File

@ -0,0 +1,8 @@
The Bash expression <pre>{{intro}}</pre>
{{#each results}}
{{#if text}}
{{text}}
{{else}}
<pre>{{value}}</pre>
{{/if}}
{{/each}}

View File

@ -13,38 +13,108 @@ ddg_goodie_test(
],
"bash [ -a b ]" => test_zci(
qr/.+ true if b exists./,
html => qr/.+/,
heading => "[ -a b ] (Bash)",
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash [[ "abc" < "cba" ]]' => test_zci(
qr/.+ true if "abc" string-sorts before "cba" in the current locale./,
html => qr/.+/,
heading => '[[ &quot;abc&quot; &lt; &quot;cba&quot; ]] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash [ 2 -gt 1 ]' => test_zci(
qr/.+ true if 2 is numerically greater than 1./,
html => qr/.+/,
heading => '[ 2 -gt 1 ] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash [ ! hello == world ]' => test_zci(
qr/.+ false if the strings hello and world are equal./,
html => qr/.+/,
heading => '[ ! hello == world ] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash [[ /tmp/hello -nt /etc/test ]]' => test_zci (
qr#.+ true if /tmp/hello has been changed more recently than /etc/test or if /tmp/hello exists and /etc/test does not.#,
html => qr/.+/,
heading => '[[ /tmp/hello -nt /etc/test ]] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash [ -z hello ]' => test_zci(
qr/.+ true if the length of 'hello' is zero./,
html => qr/.+/,
heading => '[ -z hello ] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash if [[ "abc" -lt "cba" ]]' => test_zci(
qr/.+ true if "abc" is numerically less than "cba"./,
html => qr/.+/,
heading => '[[ &quot;abc&quot; -lt &quot;cba&quot; ]] (Bash)',
structured_answer => {
id => 'bash_primary_expressions',
name => 'Answer',
data => '-ANY-',
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bash_primary_expressions.content'
}
}
}
),
'bash if [ 1 -lt 2 -a 1 -lt 3 ]' => undef,
'bash if [ ![ 1 -lt 2 ] ]' => undef,