Skip to content

Commit

Permalink
Item14237: Basic reading of specs and caching of defaults.
Browse files Browse the repository at this point in the history
- First spec format parser Foswiki::Config::Spec::Format::data.

- Sample data spec file Spec.data in EmptyExtension

- Caching of defaults for fast fetching upon startup without the need to
reparse spec files.

- Basic spec format guessing.

- Bug fixes.

Other bugs are known and awaiting for their turn...
  • Loading branch information
vrurg committed Dec 16, 2016
1 parent 30cac9d commit 2e758cc
Show file tree
Hide file tree
Showing 17 changed files with 1,166 additions and 290 deletions.
13 changes: 13 additions & 0 deletions EmptyExtension/lib/Foswiki/Extension/EmptyExtension/Spec.data
@@ -0,0 +1,13 @@
#
-section => "Test section" => [
"Test.Key" => {
-type => "TEXT",
-default => "Empty plugin Test.Key",
},
"Test.Number" => {
-type => "NUMBER",
-default => 3.1415926,
},
],

# vim: ft=perl
186 changes: 126 additions & 60 deletions UnitTestContrib/test/unit/ConfigTests.pm
Expand Up @@ -2,6 +2,7 @@ package ConfigTests;

use Foswiki;
use Try::Tiny;
use Data::Dumper;

use Foswiki::Class;
extends qw( FoswikiTestCase );
Expand Down Expand Up @@ -93,49 +94,51 @@ sub test_specRegister {
$cfg->clear_data;

$cfg->spec(
__FILE__,
-section => Extensions => -text => "Just extensions" => [
-section => TestExt => -text => "Test extension" => [
-modprefix => 'Foswiki::Extension::TestExt',
'ANewKey.NewSubKey' => [
Valid => { -type => 'BOOL', },
Text => { -type => 'TEXT', },
],
'Extensions.TestExt' => {
Sample => { -type => 'INTEGER', },
StrKey => { -type => 'TEXT(32)', }
},
],
-section => SampleExt => -text => "Sample extension" => [
-modprefix => 'Foswiki::Extension::SampleExt',
'Extensions.SampleExt' => [
Option => {
-type => 'BOOL',
-default => 0,
},
Setting => {
-type => 'SELECT',

#-variants => [qw(one two three)],
},
'Sub.Setting.Deep' => [
'Opt.K1' => { -type => 'TEXT', },
'Opt.K2' => { -type => 'NUMBER', },
source => __FILE__,
specs => [
-section => Extensions => -text => "Just extensions" => [
-section => TestExt => -text => "Test extension" => [
-modprefix => 'Foswiki::Extension::TestExt',
'ANewKey.NewSubKey' => [
Valid => { -type => 'BOOL', },
Text => { -type => 'TEXT', },
],
],
-modprefix => 'Foswiki::Extension::OtherExt',
'Extensions.SampleExt' => [
Param => {
-type => 'NUMBER',
-default => 3.14,
},
OneOf => {
-type => 'PERL',
-default => { a => 1, b => 2, c => 3, },

#-expert => 1,
'Extensions.TestExt' => {
Sample => { -type => 'INTEGER', },
StrKey => { -type => 'TEXT(32)', }
},
],
-section => SampleExt => -text => "Sample extension" => [
-modprefix => 'Foswiki::Extension::SampleExt',
'Extensions.SampleExt' => [
Option => {
-type => 'BOOL',
-default => 0,
},
Setting => {
-type => 'SELECT',

#-variants => [qw(one two three)],
},
'Sub.Setting.Deep' => [
'Opt.K1' => { -type => 'TEXT', },
'Opt.K2' => { -type => 'NUMBER', },
],
],
-modprefix => 'Foswiki::Extension::OtherExt',
'Extensions.SampleExt' => [
Param => {
-type => 'NUMBER',
-default => 3.14,
},
OneOf => {
-type => 'PERL',
-default => { a => 1, b => 2, c => 3, },

#-expert => 1,
},
],
],
],
],
);
Expand Down Expand Up @@ -179,16 +182,58 @@ sub test_specRegister {
return;
}

sub test_specOnLocalData {
my $this = shift;

Foswiki::load_class('Foswiki::Config::DataHash');

my %data;

my $dataObj = tie %data, 'Foswiki::Config::DataHash', app => $this->app;

my $cfg = $this->app->cfg;

my $section = $this->create( 'Foswiki::Config::Section', name => 'Root', );

$cfg->spec(
source => __FILE__,
data => $dataObj,
section => $section,
specs => [
-section => Section => [
'Test1.Key1' => [
-type => 'TEXT',
-default => 'Default Key1',
],
'Test2.Key2' => [
-type => 'NUMBER',
-default => 3.1415926,
],
]
],
);

$this->assert_deep_equals(
{
Test1 => { Key1 => 'Default Key1', },
Test2 => { Key2 => 3.1415926, },
},
\%data
);
}

sub test_unknownKeyOption {
my $this = shift;

try {
$this->app->cfg->spec(
__FILE__,
-section => Section => [
'Test.Key' => [
-type => 'NUMBER',
-badOption => 'Value matters not...',
source => __FILE__,
specs => [
-section => Section => [
'Test.Key' => [
-type => 'NUMBER',
-badOption => 'Value matters not...',
],
],
],
);
Expand Down Expand Up @@ -216,17 +261,21 @@ sub test_defaultValue {
my $holder = $cfg->localize;
$cfg->clear_data;

my $defStr = "This is default";

$cfg->spec(
__FILE__,
-section => Section => [
TestKey => [
Key1 => [
-type => 'TEXT',
-default => "This is default",
],
Key2 => [
-type => 'NUMBER',
-default => 3.1415926,
source => __FILE__,
specs => [
-section => Section => [
TestKey => [
Key1 => [
-type => 'TEXT',
-default => $defStr,
],
Key2 => [
-type => 'NUMBER',
-default => 3.1415926,
],
],
],
],
Expand All @@ -236,15 +285,28 @@ sub test_defaultValue {

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} );
$this->assert_equals( $defStr, $cfgData->{TestKey}{Key1} );
$this->assert_equals( $defStr, $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( $defStr, $keyNode->default );
$this->assert_equals( "This is changed", $keyNode->value );

$keyNode->clear_value;

$this->assert_equals( $defStr, $cfgData->{TestKey}{Key1} );

$cfg->dataMode;

$this->assert_equals( $defStr, $cfg->data->{TestKey}{Key1},
"The value of TestKey.Key1 doesn't match spec's default after switching to data mode."
);
$this->assert_equals( 3.1415926, $cfg->data->{TestKey}{Key2},
"The value of TestKey.Key2 doesn't match spec's default after switching to data mode."
);
}

my %keyStructs = (
Expand Down Expand Up @@ -370,7 +432,11 @@ sub test_specFilesAttribute {

my $sf = $this->app->cfg->specFiles;

my $list = $sf->list;
foreach my $sfile ( @{ $sf->list } ) {
say STDERR $sfile->fmt, " -- ", $sfile->cacheFile->path;
}

return;
}

1;
7 changes: 4 additions & 3 deletions core/lib/Foswiki/App.pm
Expand Up @@ -110,9 +110,10 @@ has env => (
required => 1,
);
has extensions => (
is => 'ro',
lazy => 1,
builder => 'prepareExtensions',
is => 'ro',
lazy => 1,
predicate => 1,
builder => 'prepareExtensions',
);
has forms => (
is => 'ro',
Expand Down

0 comments on commit 2e758cc

Please sign in to comment.