Skip to content

Commit

Permalink
Item14237: More tests for keys parsing and validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed Dec 8, 2016
1 parent 66cb062 commit 5b3f9ca
Showing 1 changed file with 232 additions and 6 deletions.
238 changes: 232 additions & 6 deletions UnitTestContrib/test/unit/ConfigTests.pm
Expand Up @@ -86,10 +86,14 @@ sub test_triggerConfigMode {
);
}

sub test_specSimple {
sub test_specRegister {
my $this = shift;

$this->app->cfg->spec(
my $cfg = $this->app->cfg;
my $holder = $cfg->localize;
$cfg->clear_data;

$cfg->spec(
__FILE__,
-section => Extensions => -text => "Just extensions" => [
-section => TestExt => -text => "Test extension" => [
Expand All @@ -111,8 +115,9 @@ sub test_specSimple {
-default => 0,
},
Setting => {
-type => 'SELECT',
-variants => [qw(one two three)],
-type => 'SELECT',

#-variants => [qw(one two three)],
},
'Sub.Setting.Deep' => [
'Opt.K1' => { -type => 'TEXT', },
Expand All @@ -128,16 +133,237 @@ sub test_specSimple {
OneOf => {
-type => 'PERL',
-default => { a => 1, b => 2, c => 3, },
-expert => 1,

#-expert => 1,
},
],
],
],
);

my $cfgData = $this->app->cfg->data;
my $expectedData = {
ANewKey => {
NewSubKey => {
Valid => undef,
Text => undef,
},
},
Extensions => {
TestExt => {
Sample => undef,
StrKey => undef,
},
SampleExt => {
Option => 0,
Setting => undef,
Sub => {
Setting =>
{ Deep => { Opt => { K1 => undef, K2 => undef, }, }, },
},
Param => 3.14,
OneOf => { a => 1, b => 2, c => 3, },
},
},
};

$this->assert_deep_equals( $expectedData, $cfg->data,
"Config structure mismatch with specs definition" );

my $sec = $cfg->rootSection->sections->[0];
$this->assert_equals( "Extensions", $sec->name );
$this->assert_equals( "Just extensions", $sec->text );
$this->assert_equals( "TestExt", $sec->sections->[0]->name );
$this->assert_equals( "Test extension", $sec->sections->[0]->text );
$this->assert_equals( "SampleExt", $sec->sections->[1]->name );
$this->assert_equals( "Sample extension", $sec->sections->[1]->text );

return;
}

sub test_unknownKeyOption {
my $this = shift;

try {
$this->app->cfg->spec(
__FILE__,
-section => Section => [
'Test.Key' => [
-type => 'NUMBER',
-badOption => 'Value matters not...',
],
],
);

$this->assert( 0,
"A bad option must raise an exception but it didn't" );
}
catch {
my $e = Foswiki::Exception::Fatal->transmute( $_, 0 );

if ( $e->isa('Foswiki::Exception::Config::BadSpecData') ) {
$this->assert_matches(
"Unknown key option 'badOption' \\(key 'Test.Key' is part of section 'Section'\\)",
$e
);
}
else { $e->rethrow; }
};
}

sub test_defaultValue {
my $this = shift;

my $cfg = $this->app->cfg;
my $holder = $cfg->localize;
$cfg->clear_data;

$cfg->spec(
__FILE__,
-section => Section => [
TestKey => [
Key1 => [
-type => 'TEXT',
-default => "This is default",
],
Key2 => [
-type => 'NUMBER',
-default => 3.1415926,
],
],
],
);

my $cfgData = $cfg->data;

my $keyNode = $cfg->getKeyNode('TestKey.Key1');

$this->assert_equals( "This is default", $cfgData->{TestKey}{Key1} );
$this->assert_equals( "This is default", $keyNode->default );
$this->assert_equals( 3.1415926, $cfgData->{TestKey}{Key2} );

$cfg->data->{TestKey}{Key1} = "This is changed";
$this->assert_equals( "This is changed", $cfgData->{TestKey}{Key1} );

$this->assert_equals( "This is default", $keyNode->default );
$this->assert_equals( "This is changed", $keyNode->value );
}

my %keyStructs = (
Straigt => [ 'A' .. 'M' ],
ComplexDeep => [
undef, [ qw(A B), [ [ 'C', [], 'D', [ 'E' .. 'J', undef ] ], undef ] ],
qw(K L M)
],
StraightString => [ join( '.', 'A' .. 'M' ) ],
ComplexWithStrings1 => [
'A' .. 'D', ['E.F.G'],
[ undef, [ 'H.I', [ undef, ['J'], ], 'K', ['L'] ], undef ], 'M'
],
ComplexWithStrings2 => [
[
'A' .. 'D',
['E.F.G'],
[ undef, [ 'H.I', [ undef, ['J'], ], 'K', ['L'] ], undef ], 'M'
]
],
SingleArrayElem => [ [ join( '.', 'A' .. 'M' ) ] ],
HashString => [ '{' . join( '}{', 'A' .. 'M' ) . '}' ],
MixedStrings =>
[ '{' . join( '}{', 'A' .. 'D' ) . '}', join( '.', 'E' .. 'M' ) ],
ComplexWithMixedStrings => [
[
'A' .. 'D',
['{E}{F}{G}'],
[ undef, [ 'H.I', [ undef, ['{J}'], ], 'K', ['L'] ], undef ], 'M'
]
],
);

my %emptyVariants = (
NoArgs => [],
EmptyList => [ [] ],
Undef => [undef],
EmptyString => [''],
SingleDot => ['.'],
);

my %badKeyArgs = (
InvalidChars => {
data => ['A.B.C.$name.(name).na}me'],
badKey => '$name',
},
HasRef => {
data => [ 'A.B.C', \%emptyVariants ],
badKey => \%emptyVariants,
},
);

sub test_keyParsing {
my $this = shift;

my @keys;
while ( my ( $variant, $keyData ) = each %keyStructs ) {
@keys = $this->app->cfg->parseKeys(@$keyData);
$this->assert_deep_equals( [ 'A' .. 'M' ],
\@keys, "Failed variant: $variant" );
}

# Test empty variants.
while ( my ( $variant, $keyData ) = each %emptyVariants ) {
@keys = $this->app->cfg->parseKeys(@$keyData);
$this->assert_num_equals( 0, scalar(@keys),
"parseKeys() for "
. $variant
. " variant must return empty array" );
}
}

sub test_arg2keys {
my $this = shift;

my @keys;
while ( my ( $variant, $keyData ) = each %keyStructs ) {
@keys = $this->app->cfg->arg2keys(@$keyData);
$this->assert_deep_equals( [ 'A' .. 'M' ],
\@keys, "Failed variant: $variant" );
}

while ( my ( $variant, $keyData ) = each %emptyVariants ) {
try {
@keys = $this->app->cfg->arg2keys(@$keyData);
$this->assert( 0, "Empty variant $variant should have failed" );
}
catch {
my $e = Foswiki::Exception::Fatal->transmute( $_, 0 );
$this->assert_equals(
"No valid config keys found in the method arguments",
$e->text,
"Expected fatal exception with particular error message but got: "
. $e
);
};
}

while ( my ( $variant, $keyData ) = each %badKeyArgs ) {
try {
@keys = $this->app->cfg->arg2keys( @{ $keyData->{data} } );
$this->assert( 0, "Invalid variant $variant should have failed" );
}
catch {
my $e = Foswiki::Exception::Fatal->transmute( $_, 0 );
$this->assert(
$e->isa('Foswiki::Exception::Config::InvalidKeyName'),
"Expected exception Foswiki::Exception::Config::InvalidKeyName, got "
. ref($e) . ":"
. $e
);
$this->assert_equals( $keyData->{badKey}, $e->keyName,
"Expected key to fail: "
. ( $keyData->{badKey} // '*undef*' )
. "; but got: "
. ( $e->keyName // '*undef*' ) );
};
}
}

1;

0 comments on commit 5b3f9ca

Please sign in to comment.