Add Perl6 version

This commit is contained in:
cono 2017-07-14 16:27:14 +03:00
parent de471eb175
commit a5309b3230
5 changed files with 164 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea/workspace.xml .idea/workspace.xml
*.pyc *.pyc
/perl6/lib/.precomp

66
perl6/lib/GildedRose.pm6 Normal file
View File

@ -0,0 +1,66 @@
use v6;
use Item;
class GildedRose {
has Item @.items;
method update_quality {
for @!items -> $item {
if ($item.name ne 'Aged Brie' && $item.name ne 'Backstage passes to a TAFKAL80ETC concert') {
if ( $item.quality > 0 ) {
if ( $item.name ne 'Sulfuras, Hand of Ragnaros' ) {
$item.quality = $item.quality - 1;
}
}
}
else {
if ( $item.quality < 50 ) {
$item.quality = $item.quality + 1;
if ( $item.name eq 'Backstage passes to a TAFKAL80ETC concert' )
{
if ( $item.sell_in < 11 ) {
if ( $item.quality < 50 ) {
$item.quality = $item.quality + 1;
}
}
if ( $item.sell_in < 6 ) {
if ( $item.quality < 50 ) {
$item.quality = $item.quality + 1;
}
}
}
}
}
if ( $item.name ne 'Sulfuras, Hand of Ragnaros' ) {
$item.sell_in = $item.sell_in - 1;
}
if ( $item.sell_in < 0 ) {
if ( $item.name ne 'Aged Brie' ) {
if ( $item.name ne
'Backstage passes to a TAFKAL80ETC concert' )
{
if ( $item.quality > 0 ) {
if ( $item.name ne 'Sulfuras, Hand of Ragnaros' ) {
$item.quality = $item.quality - 1;
}
}
}
else {
$item.quality = $item.quality - $item.quality;
}
}
else {
if ( $item.quality < 50 ) {
$item.quality = $item.quality + 1;
}
}
}
}
return;
}
};

11
perl6/lib/Item.pm6 Normal file
View File

@ -0,0 +1,11 @@
use v6;
class Item {
has Str $.name;
has Int $.sell_in is rw = 0;
has Int $.quality is rw = 0;
method Str {
"{$!name}, {$!sell_in}, {$!quality}"
}
};

16
perl6/test.p6 Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env perl6
use v6;
use Test;
use lib 'lib';
use GildedRose;
use Item;
my $item = Item.new(:name<foo>);
my $app = GildedRose.new(items => ($item));
$app.update_quality();
is $app.items[0].name, 'fixme', "first day pass";
done-testing;

70
perl6/texttest_fixture.p6 Executable file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env perl6
use v6;
use lib 'lib';
use GildedRose;
use Item;
print 'OMGHAI!', "\n";
my @items = (
Item.new(
name => '+5 Dexterity Vest',
sell_in => 10,
quality => 20
),
Item.new(
name => 'Aged Brie',
sell_in => 2,
quality => 0
),
Item.new(
name => 'Elixir of the Mongoose',
sell_in => 5,
quality => 7
),
Item.new(
name => 'Sulfuras, Hand of Ragnaros',
sell_in => 0,
quality => 80
),
Item.new(
name => 'Sulfuras, Hand of Ragnaros',
sell_in => -1,
quality => 80
),
Item.new(
name => 'Backstage passes to a TAFKAL80ETC concert',
sell_in => 15,
quality => 20
),
Item.new(
name => 'Backstage passes to a TAFKAL80ETC concert',
sell_in => 10,
quality => 49
),
Item.new(
name => 'Backstage passes to a TAFKAL80ETC concert',
sell_in => 5,
quality => 49
),
Item.new( # This Conjured item does not work properly yet
name => 'Conjured Mana Cake',
sell_in => 3,
quality => 6
),
);
sub MAIN(Int $days = 2) {
my $gilded-rose = GildedRose.new( items => @items );
for ^$days -> $day {
say "-------- day $day --------";
say 'name, sellIn, quality';
.Str.say for $gilded-rose.items;
"".say;
$gilded-rose.update_quality();
}
}