#!/usr/bin/env perl

package Prty::DestinationTree::Test;
use base qw/Prty::Test::Class/;

use strict;
use warnings;
use utf8;

# -----------------------------------------------------------------------------

sub test_loadClass : Init(1) {
    shift->useOk('Prty::DestinationTree');
}

# -----------------------------------------------------------------------------

sub unit_setup : Setup(2) Group(^unit_) {
    my $self = shift;

    my $fixDir = $self->testDir('fixture');
    Prty::Path->mkdir("$fixDir/a",-recursive=>1);
    Prty::Path->write("$fixDir/a/hello.c","main(){}\n");
    Prty::Path->write("$fixDir/a/hello.o","xyz\n");
    $self->ok(-d $fixDir,'Fixture-Verzeichnis erzeugt');

    my $tree = Prty::DestinationTree->new("$fixDir/a",
        -exclude=>qr/\.o$/,
        -quiet=>1,
    );
    $self->is(ref($tree),'Prty::DestinationTree');

    $self->set(fixDir=>$fixDir,tree=>$tree);
}

sub unit_test1 : Test(5) {
    my $self = shift;

    my ($tree,$fixDir) = $self->get(qw/tree fixDir/);

    my $written = $tree->addFile("$fixDir/a/hello.c","main(){}\n");
    $self->is($written,0,'Datei hat gleichen Inhalt');

    $written = $tree->addFile("$fixDir/a/hello.c","main(){printf()}\n");
    $self->is($written,1,'Datei hat verschiedenen Inhalt');

    my $created = $tree->addDir("$fixDir/a");
    $self->is($created,0,'Verzeichnis nicht erzeugt');

    $created = $tree->addDir("$fixDir/b");
    $self->is($created,1,'Verzeichnis erzeugt');

    my $n = $tree->cleanup;
    $self->is($n,0,'Keine Pfade entfernt');
}

sub unit_test2 : Test(2) {
    my $self = shift;

    my ($tree,$fixDir) = $self->get(qw/tree fixDir/);

    my $written = $tree->addFile("$fixDir/a/b/c/test.txt","1\n");
    $self->is($written,2,'Datei erzeugt');

    my $n = $tree->cleanup;
    $self->is($n,1,'Überzählige Datei entfernt');
}

sub unit_teardown : Teardown(2) Group(^unit_) {
    my $self = shift;

    my $dir = $self->get('fixDir');
    Prty::Path->delete($dir);
    $self->ok(!-e $dir,'Fixture-Verzeichnis gelöscht');

    $dir = $self->testDir;
    Prty::Path->delete($dir);
    $self->ok(!-e $dir,'Test-Verzeichnis gelöscht');
}

# -----------------------------------------------------------------------------

package main;
Prty::DestinationTree::Test->runTests;

# eof
