Config-Manager

 view release on metacpan or  search on metacpan

t/01_____config.t  view on Meta::CPAN

################################################################################
# Werte setzen und auswerten                                                   #
################################################################################

$conf = Config::Manager::Conf->new();
# Wert setzen und wieder auslesen
$conf->set('SPU', 'TMP1', 'C:\tmp') ? ok(): nok();
$conf->get('SPU', 'TMP1') eq 'C:\tmp' ? ok(): nok();
$conf->set('SPU', 'TMP2', 'D:\tmp') ? ok() : nok();
$conf->get('SPU', 'TMP2') eq 'D:\tmp' ? ok() : nok();
# Wert in anderen Abschnitt setzen und wieder auslesen
$conf->set('KM', 'TMP1', 'C:\temp') ? ok() : nok();
$conf->get('KM', 'TMP1') eq 'C:\temp' ? ok() : nok();
$conf->set('KM', 'TMP2', 'D:\temp') ? ok() : nok();
$conf->get('KM', 'TMP2') eq 'D:\temp' ? ok() : nok();
$conf->set('KM', 'TMP3', 'X:\temp') ? ok() : nok();
$conf->get('KM', 'TMP3') eq 'X:\temp' ? ok() : nok();
# Die Werte in SPU muessen nach wie vor stimmen
$conf->get('SPU', 'TMP1') eq 'C:\tmp' ? ok() : nok();
$conf->get('SPU', 'TMP2') eq 'D:\tmp' ? ok() : nok();
$conf->get('SPU', 'TMP1') eq 'C:\tmp' ? ok() : nok();
$conf->get('SPU', 'TMP2') eq 'D:\tmp' ? ok() : nok();
$conf->get('KM', 'TMP1') eq 'C:\temp' ? ok() : nok();
$conf->get('KM', 'TMP2') eq 'D:\temp' ? ok() : nok();
# Zugriff auf undefinierten Wert
$conf->get('SPU', 'TRALALA') ? nok() : ok();
$conf->error() eq "Configuration constant \$[SPU]{TRALALA} not found" ? ok() : nok();
$conf->get('SPX', 'TMP1') ? nok() : ok();
$conf->error() eq "Configuration constant \$[SPX]{TMP1} not found" ? ok() : nok();
$conf->get('SPX', 'TRALALA') ? nok() : ok();
$conf->error() eq "Configuration constant \$[SPX]{TRALALA} not found" ? ok() : nok();
$conf->get('TRALALA') ? nok() : ok();
$conf->error() eq "Configuration constant \$[DEFAULT]{TRALALA} not found" ? ok() : nok();

################################################################################
# Konstruktor auf Instanz anwenden statt auf Klasse                            #
################################################################################

$conf = $conf->new();
$conf->get('SPU', 'TMP1') ? nok() : ok();
$conf->error() eq "Configuration constant \$[SPU]{TMP1} not found" ? ok() : nok();

################################################################################
# Einlesen und Auswerten einer korrekten Datei sowie Kommandozeilenoptionen    #
################################################################################

# Ich mache alle Tests sowohl auf einem frischen Objekt ($conf) als auch auf
# der Klasse, d.h. auf der Default-Instanz.

############################################################
# Daten einlesen, Kommandozeilenoptionen setzen
############################################################

# Fuer die Default-Instanz dasselbe wie in conf(), aber mit Pruefungen.
# Dateien einlesen
Config::Manager::Conf->add('t/conf_private.ini', 't/conf_public.ini') ? ok() : nok();
# Kommandozeilenargumente setzen
Config::Manager::Conf->set('INSTDIR', 'inst') ? ok() : nok();

############################################################
# Einfache Werte in Default-Section abfragen
############################################################

# Unbekannter Wert
defined conf()->get('TRALALA') ? nok() : ok();
defined Config::Manager::Conf->get('TRALALA') ? nok() : ok();

# Einfacher Wert
conf()->get('SYSDIR') eq 'sys' ? ok() : nok();
Config::Manager::Conf->get('SYSDIR') eq 'sys' ? ok() : nok();
# Einfacher Wert aus conf_private.ini
conf()->get('DRIVE') eq 'C:' ? ok() : nok();
Config::Manager::Conf->get('DRIVE') eq 'C:' ? ok() : nok();
# Einfacher Wert aus Kommandozeile
conf()->get('INSTDIR') eq 'inst' ? ok() : nok();
Config::Manager::Conf->get('INSTDIR') eq 'inst' ? ok() : nok();

# Einfacher Wert, Leerzeichen am Zeilenende ignoriert
conf()->get('WRKDIR') eq 'wrk' ? ok() : nok();
Config::Manager::Conf->get('WRKDIR') eq 'wrk' ? ok() : nok();
# Einfacher Wert, Leerzeichen am Zeilenanfang ignoriert
conf()->get('TMPDIR') eq 'tmp' ? ok() : nok();
Config::Manager::Conf->get('TMPDIR') eq 'tmp' ? ok() : nok();

# Leerer Wert
conf()->get('EMPTY') eq '' ? ok() : nok();
Config::Manager::Conf->get('EMPTY') eq '' ? ok() : nok();

# Leerzeichen
conf()->get('SPACES1') eq '   ' ? ok() : nok();
Config::Manager::Conf->get('SPACES1') eq '   ' ? ok() : nok();
conf()->get('SPACES2') eq '   ' ? ok() : nok();
Config::Manager::Conf->get('SPACES2') eq '   ' ? ok() : nok();

# Wert mit Gleichheitszeichen
conf()->get('ORWELL') eq '2+2=5' ? ok() : nok();
Config::Manager::Conf->get('ORWELL') eq '2+2=5' ? ok() : nok();

# Einfache Anfuehrungszeichen: Keine Sonderbedeutung
conf()->get('SINGLE_QUOTES') eq "'tralala'" ? ok() : nok();
Config::Manager::Conf->get('SINGLE_QUOTES') eq "'tralala'" ? ok() : nok();
# Doppelte Anfuehrungszeichen sind Begrenzer
conf()->get('QUOTES') eq 'tralala' ? ok() : nok();
Config::Manager::Conf->get('QUOTES') eq 'tralala' ? ok() : nok();
# Anfuehrungszeichen am Anfang: Keine Sonderbedeutung
conf()->get('FIRST_QUOTE') eq '"tralala' ? ok() : nok();
Config::Manager::Conf->get('FIRST_QUOTE') eq '"tralala' ? ok() : nok();
# Anfuehrungszeichen am Ende: Keine Sonderbedeutung
conf()->get('LAST_QUOTE') eq 'tralala"' ? ok() : nok();
Config::Manager::Conf->get('LAST_QUOTE') eq 'tralala"' ? ok() : nok();
# Anfuehrungszeichen in der Mitte: Keine Sonderbedeutung
conf()->get('MID_QUOTE1') eq 'tra"lala' ? ok() : nok();
Config::Manager::Conf->get('MID_QUOTE1') eq 'tra"lala' ? ok() : nok();
conf()->get('MID_QUOTE2') eq 'tra"lala' ? ok() : nok();
Config::Manager::Conf->get('MID_QUOTE2') eq 'tra"lala' ? ok() : nok();
conf()->get('MID_QUOTE3') eq ' " ' ? ok() : nok();
Config::Manager::Conf->get('MID_QUOTE3') eq ' " ' ? ok() : nok();

# Escape-Sequenzen: Dollar
conf()->get('ESC_D') eq '$SYS' ? ok() : nok();
Config::Manager::Conf->get('ESC_D') eq '$SYS' ? ok() : nok();
# Escape-Sequenzen: Backslash und Substitution
conf()->get('ESC_BS') eq '\C:/sys' ? ok() : nok();
Config::Manager::Conf->get('ESC_BS') eq '\C:/sys' ? ok() : nok();
# Escape-Sequenzen: Backslash und Dollar
conf()->get('ESC_BD') eq '\$SYS' ? ok() : nok();
Config::Manager::Conf->get('ESC_BD') eq '\$SYS' ? ok() : nok();
# Escape-Sequenzen: Backslash, Backslash, Substitution
conf()->get('ESC_BBS') eq '\\\\C:/sys' ? ok() : nok();
Config::Manager::Conf->get('ESC_BBS') eq '\\\\C:/sys' ? ok() : nok();
# Escape-Sequenzen: Backslash, Backslash, Dollar
conf()->get('ESC_BBD') eq '\\\\$SYS' ? ok() : nok();
Config::Manager::Conf->get('ESC_BBD') eq '\\\\$SYS' ? ok() : nok();

# Substitution: Nur schliessende Klammer
conf()->get('S_C') eq 'C:/sys}' ? ok() : nok();
Config::Manager::Conf->get('S_C') eq 'C:/sys}' ? ok() : nok();
# Qualifizierte Substitution: Nur schliessende Klammer
conf()->get('SQ_C') eq 'a_hello}' ? ok() : nok();
Config::Manager::Conf->get('SQ_C') eq 'a_hello}' ? ok() : nok();

# Gross- und Kleinschreibung fuer Schluessel
conf()->get('A') eq 'a' ? ok() : nok();
Config::Manager::Conf->get('A') eq 'a' ? ok() : nok();
conf()->get('a') eq 'ach ja' ? ok() : nok();
Config::Manager::Conf->get('a') eq 'ach ja' ? ok() : nok();

# Schluessel darf auch mit $ beginnen
conf()->get('SONDERLOCKE') eq 'Key faengt mit $ an' ? ok() : nok();
Config::Manager::Conf->get('SONDERLOCKE') eq 'Key faengt mit $ an' ? ok() : nok();

############################################################
# Substitutionen in Default-Section abfragen
############################################################

# Einfache Substitution von zwei Werten
conf()->get('SYS') eq 'C:/sys' ? ok() : nok();
Config::Manager::Conf->get('SYS') eq 'C:/sys' ? ok() : nok();
conf()->get('TMP') eq 'C:/tmp' ? ok() : nok();
Config::Manager::Conf->get('TMP') eq 'C:/tmp' ? ok() : nok();

# Rekursive Substitution
conf()->get('WRK') eq 'C:/sys/wrk' ? ok() : nok();
Config::Manager::Conf->get('WRK') eq 'C:/sys/wrk' ? ok() : nok();
conf()->get('INST') eq 'C:/sys/inst' ? ok() : nok();
Config::Manager::Conf->get('INST') eq 'C:/sys/inst' ? ok() : nok();
conf()->get('PATH') eq 'C:/sys/wrk;C:/sys/inst' ? ok() : nok();
Config::Manager::Conf->get('PATH') eq 'C:/sys/wrk;C:/sys/inst' ? ok() : nok();

############################################################
# Zyklen erkennen und abbrechen
############################################################

# Die duemmste Art, einen Zyklus zu produzieren
$conf = conf();
defined $conf->get('SIMPLE_LOOP') ? nok() : ok();
$conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #84 [DEFAULT]: \$[DEFAULT]{SIMPLE_LOOP} = \"This is a \$SIMPLE_LOOP\"" ? ok() : nok();
defined Config::Manager::Conf->get('SIMPLE_LOOP') ? nok() : ok();
Config::Manager::Conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #84 [DEFAULT]: \$[DEFAULT]{SIMPLE_LOOP} = \"This is a \$SIMPLE_LOOP\"" ? ok() : nok();

# Etwas weniger auffaellig
$conf = conf();
defined $conf->get('LOOP') ? nok() : ok();
$conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #87 [DEFAULT]: \$[DEFAULT]{LOOP} = \"\$THIS will loop\"" ? ok() : nok();
defined Config::Manager::Conf->get('LOOP') ? nok() : ok();
Config::Manager::Conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #87 [DEFAULT]: \$[DEFAULT]{LOOP} = \"\$THIS will loop\"" ? ok() : nok();

# Unauffaellig
$conf = conf();
defined $conf->get('THAT') ? nok() : ok();
$conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #85 [DEFAULT]: \$[DEFAULT]{THIS} = \"This \$WILL loop\"" ? ok() : nok();
defined Config::Manager::Conf->get('THAT') ? nok() : ok();
Config::Manager::Conf->error() eq "Infinite recursion in file 't/conf_public.ini' line #85 [DEFAULT]: \$[DEFAULT]{THIS} = \"This \$WILL loop\"" ? ok() : nok();

############################################################
# Qualifizierter Zugriff
############################################################

# Wert ist in der Default-Section und zwei weiteren Sections definiert
conf()->get('EVERYWHERE') eq 'default' ? ok() : nok();
Config::Manager::Conf->get('EVERYWHERE') eq 'default' ? ok() : nok();
conf()->get('A', 'EVERYWHERE') eq 'at a' ? ok() : nok();
Config::Manager::Conf->get('A', 'EVERYWHERE') eq 'at a' ? ok() : nok();
conf()->get('B', 'EVERYWHERE') eq 'at b' ? ok() : nok();
Config::Manager::Conf->get('B', 'EVERYWHERE') eq 'at b' ? ok() : nok();

# Wert ist in zwei Sections, aber nicht in der Default-Section definiert
$conf = conf();
defined $conf->get('HELLO') ? nok() : ok();
$conf->error() eq "Configuration constant \$[DEFAULT]{HELLO} not found" ? ok() : nok();
defined Config::Manager::Conf->get('HELLO') ? nok() : ok();
Config::Manager::Conf->error() eq "Configuration constant \$[DEFAULT]{HELLO} not found" ? ok() : nok();
conf()->get('A', 'HELLO') eq 'a_hello' ? ok() : nok();

t/01_____config.t  view on Meta::CPAN

conf()->get('WHO', 'uvw') eq 'fritzfritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'NAME') eq 'Fritz Fischer' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'name') eq 'Fritz Fischer' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'IQ') eq '0' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'XYZ') eq 'fritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'xyz') eq 'fritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'XXX') eq 'fritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'xxx') eq 'fritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'UVW') eq 'fritzfritz' ? ok() : nok();
Config::Manager::Conf->get('WHO', 'uvw') eq 'fritzfritz' ? ok() : nok();

################################################################################
# Zugriff auf Umgebungsvariablen                                               #
################################################################################

$ENV{DONALD} = 'Duck';
# Zugriff auf Umgebungsvariable
conf()->get('ENV', 'DONALD') eq 'Duck' ? ok() : nok();
Config::Manager::Conf->get('ENV', 'DONALD') eq 'Duck' ? ok() : nok();
# Qualifizierter Name erforderlich
$conf = conf();
defined $conf->get('DONALD') ? nok() : ok();
$conf->error() eq "Configuration constant \$[DEFAULT]{DONALD} not found" ? ok() : nok();
defined Config::Manager::Conf->get('DONALD') ? nok() : ok();
Config::Manager::Conf->error() eq "Configuration constant \$[DEFAULT]{DONALD} not found" ? ok() : nok();
# Umgebungsvariable nicht gefunden
$conf = conf();
defined $conf->get('ENV', 'DAGOBERT') ? nok() : ok();
$conf->error() eq "Configuration constant \$[ENV]{DAGOBERT} not found" ? ok() : nok();
defined Config::Manager::Conf->get('ENV', 'DAGOBERT') ? nok() : ok();
Config::Manager::Conf->error() eq "Configuration constant \$[ENV]{DAGOBERT} not found" ? ok() : nok();
# Substitution unter Verwendung einer Umgebungsvariablen
$conf = conf();
$conf->set('SUBST_ENV1', 'Dagobert $[ENV]DONALD') ? ok() : nok();
$conf->get('SUBST_ENV1') eq 'Dagobert Duck' ? ok() : nok();
Config::Manager::Conf->set('SUBST_ENV1', 'Dagobert $[ENV]DONALD') ? ok() : nok();
Config::Manager::Conf->get('SUBST_ENV1') eq 'Dagobert Duck' ? ok() : nok();
# Nochmal Substitution, aber {}-Syntax verwenden
$conf = conf();
$conf->set('SUBST_ENV2', 'Dagobert $[ENV]{DONALD}') ? ok() : nok();
$conf->get('SUBST_ENV2') eq 'Dagobert Duck' ? ok() : nok();
Config::Manager::Conf->set('SUBST_ENV2', 'Dagobert $[ENV]{DONALD}') ? ok() : nok();
Config::Manager::Conf->get('SUBST_ENV2') eq 'Dagobert Duck' ? ok() : nok();
# Substitution unter Verwendung einer fehlenden Umgebungsvariablen
$conf = conf();
$conf->set('SUBST_NOENV', 'Donald $[ENV]DAGOBERT') ? ok() : nok();
defined $conf->get('SUBST_NOENV') ? nok() : ok();
$conf->error() eq "Configuration constant \$[ENV]{DAGOBERT} not found" ? ok() : nok();
Config::Manager::Conf->set('SUBST_NOENV', 'Donald $[ENV]DAGOBERT') ? ok() : nok();
defined Config::Manager::Conf->get('SUBST_NOENV') ? nok() : ok();
Config::Manager::Conf->error() eq "Configuration constant \$[ENV]{DAGOBERT} not found" ? ok() : nok();

################################################################################
# Zugriff auf SPECIAL-Werte                                                    #
################################################################################

############################################################
# Jahr, Monat, Tag
############################################################

# Ich kann hier aus naheliegenden Gruenden nicht auf konkrete Werte abfragen,
# daher pruefe ich nur, ob die Werte ueberhaupt vorhanden sind und ob ihr Format
# dem entspricht, was ich erwarte.

# Zugriff auf YEAR
$result = conf()->get('SPECIAL', 'YEAR');
length($result) == 4 ? ok() : nok();
$result >= 2000 ? ok() : nok();
$result = Config::Manager::Conf->get('SPECIAL', 'YEAR');
length($result) == 4 ? ok() : nok();
$result >= 2000 ? ok() : nok();
# Zugriff auf YY
$result = conf()->get('SPECIAL', 'YY');
length($result) == 2 ? ok() : nok();
$result = Config::Manager::Conf->get('SPECIAL', 'YY');
length($result) == 2 ? ok() : nok();
# Zugriff auf CC (sollte noch einige Zeit das 20. Jahrhundert liefern :-)
conf()->get('SPECIAL', 'CC') == 20 ? ok() : nok();
Config::Manager::Conf->get('SPECIAL', 'CC') == 20 ? ok() : nok();

# Zugriff auf MONTH
$result = conf()->get('SPECIAL', 'MONTH');
length($result) == 2 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 12 ? ok() : nok();
$result = Config::Manager::Conf->get('SPECIAL', 'MONTH');
length($result) == 2 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 12 ? ok() : nok();
# Zugriff auf DAY
$result = conf()->get('SPECIAL', 'DAY');
length($result) == 2 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 31 ? ok() : nok();
$result = Config::Manager::Conf->get('SPECIAL', 'DAY');
length($result) == 2 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 31 ? ok() : nok();
# Zugriff auf YDAY
$result = conf()->get('SPECIAL', 'YDAY');
length($result) == 3 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 366 ? ok() : nok();
$result = Config::Manager::Conf->get('SPECIAL', 'YDAY');
length($result) == 3 ? ok() : nok();
$result >= 1 ? ok() : nok();
$result <= 366 ? ok() : nok();

############################################################
# Name des Betriebssystems
############################################################

conf()->get('SPECIAL', 'OS') eq $^O ? ok() : nok();
Config::Manager::Conf->get('SPECIAL', 'OS') eq $^O ? ok() : nok();

############################################################
# Scope
############################################################

conf()->get('SPECIAL', 'SCOPE') eq 'NONE' ? ok() : nok();
conf()->scope() eq 'NONE' ? ok() : nok();



( run in 0.660 second using v1.01-cache-2.11-cpan-364913b4093 )