Changeset 13990


Ignore:
Timestamp:
02/15/12 11:24:29 (15 months ago)
Author:
PaulHarvey
Message:

Item10942: Unit tests, slightly reusable parser

Location:
trunk/SemanticLinksPlugin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SemanticLinksPlugin/lib/Foswiki/Plugins/SemanticLinksPlugin/Core.pm

    r13021 r13990  
    1313use Assert; 
    1414use Foswiki::Func ();    # The plugins API 
    15 use Foswiki::Plugins(); 
    1615 
    1716my %templates; 
     
    331330sub beforeSaveHandler { 
    332331    my ( $text, $topic, $web, $topicObject ) = @_; 
     332    my %DATA = _parse( $topicObject->getEmbeddedStoreForm(), $topicObject ); 
     333 
     334    foreach my $META ( keys %DATA ) { 
     335        $topicObject->putAll( $META, @{ $DATA{$META} } ); 
     336    } 
     337 
     338    return; 
     339} 
     340 
     341# Parse $text for links, using topic/web/topicObject as context 
     342sub _parse { 
     343    my ( $text, $topicObject ) = @_; 
     344    my $web   = $topicObject->web(); 
     345    my $topic = $topicObject->topic(); 
    333346 
    334347    init(); 
     
    341354    $topicObject->remove('SLVALUE'); 
    342355    $topicObject->remove('SLPROPERTY'); 
    343     $text = $topicObject->getEmbeddedStoreForm(); 
    344356 
    345357    # Expand prefs 
    346358    $text =~ s/(%([A-Z]+)%)/ 
    347359        Foswiki::Func::getPreferencesValue($2) || $hardvars{$2} || $1/gex; 
    348     semanticLinksSaveHandler( $text, $topic, $web, $topicObject ); 
    349     plainLinksSaveHandler( $text, $topic, $web, $topicObject ); 
    350  
    351     return; 
     360 
     361    return ( 
     362        semanticLinksSaveHandler( $text, $topic, $web, $topicObject ), 
     363        plainLinksSaveHandler( $text, $topic, $web, $topicObject ) 
     364    ); 
    352365} 
    353366 
     
    371384        ($Foswiki::regex{anchorRegex})?/ 
    372385        stashPlainLink('internal', 'autolink', ($1 || '') . $3, undef, $4)/gexm; 
    373     $topicObject->putAll( 'LINK', values %links ); 
    374  
    375     return; 
     386 
     387    return ( 'LINK' => [ values %links ] ); 
    376388} 
    377389 
     
    440452sub semanticLinksSaveHandler { 
    441453    my ( $text, $topic, $web, $topicObject ) = @_; 
     454    my %DATA; 
    442455    my @propertyaddresses; 
    443456 
     
    506519        } 
    507520        @SLVALUE = sort { $a->{propertyseq} <=> $b->{propertyseq} } @SLVALUE; 
    508         $topicObject->putAll( 'SLPROPERTY', @SLPROPERTY ); 
    509         $topicObject->putAll( 'SLVALUE',    @SLVALUE ); 
     521        $DATA{SLPROPERTY} = [@SLPROPERTY]; 
     522        $DATA{SLVALUE}    = [@SLVALUE]; 
    510523        @SLMETAVALUE = 
    511524          sort { $a->{propertyseq} <=> $b->{propertyseq} } @SLMETAVALUE; 
    512         $topicObject->putAll( 'SLMETAPROPERTY', @SLMETAPROPERTY ); 
    513         $topicObject->putAll( 'SLMETAVALUE',    @SLMETAVALUE ); 
     525        $DATA{SLMETAPROPERTY} = [@SLMETAPROPERTY]; 
     526        $DATA{SLMETAVALUE}    = [@SLMETAVALUE]; 
    514527 
    515528        # These are unused legacy types 
    516         $topicObject->putAll( 'SLPROPERTYVALUE', () ); 
    517         $topicObject->putAll( 'SLPROPERTIES',    () ); 
    518     } 
    519  
    520     return; 
     529        $DATA{SLPROPERTYVALUE} = []; 
     530        $DATA{SLPROPERTIES}    = []; 
     531    } 
     532 
     533    return %DATA; 
    521534} 
    522535 
     
    869882Foswiki - The Free and Open Source Wiki, http://foswiki.org/ 
    870883 
    871 Copyright (C) 2010-2011 Paul.W.Harvey@csiro.au, http://trin.org.au 
    872 Copyright (C) 2010-2011 Foswiki Contributors. Foswiki Contributors 
     884Copyright (C) 2010-2012 Paul.W.Harvey@csiro.au, http://trin.org.au 
     885Copyright (C) 2010-2012 Foswiki Contributors. Foswiki Contributors 
    873886are listed in the AUTHORS file in the root of this distribution. 
    874887NOTE: Please extend that file, not this notice. 
  • trunk/SemanticLinksPlugin/test/unit/SemanticLinksPlugin/SemanticLinksPluginTests.pm

    r9830 r13990  
    11# See bottom of file for license and copyright information 
     2package SemanticLinksPluginTests; 
    23use strict; 
    34use warnings; 
    45 
    5 package SemanticLinksPluginTests; 
    6  
    7 use FoswikiTestCase; 
    8 our @ISA = qw( FoswikiTestCase ); 
     6use FoswikiFnTestCase(); 
     7our @ISA = qw( FoswikiFnTestCase ); 
    98 
    109use strict; 
    1110use warnings; 
    12 use Foswiki; 
    13 use CGI; 
    14  
    15 my $foswiki; 
    16  
    17 sub new { 
    18     my $self = shift()->SUPER::new(@_); 
    19     return $self; 
    20 } 
    21  
    22 # Set up the test fixture 
     11 
    2312sub set_up { 
     13    my ($this) = @_; 
     14 
     15    $this->SUPER::set_up(); 
     16    $Foswiki::cfg{Plugins}{SemanticLinksPlugin}{Enabled} = 1; 
     17    $this->createNewFoswikiSession(); 
     18    require Foswiki::Plugins::SemanticLinksPlugin::Core; 
     19    Foswiki::Plugins::SemanticLinksPlugin::Core::init(); 
     20 
     21    return; 
     22} 
     23 
     24# SMELL: Why isn't ACRO linking? 
     25sub test_simple_plain_links { 
    2426    my $this = shift; 
    25  
    26     $this->SUPER::set_up(); 
    27  
    28     $Foswiki::Plugins::SESSION = $foswiki; 
    29 } 
    30  
    31 sub tear_down { 
    32     my $this = shift; 
    33     $this->SUPER::tear_down(); 
    34 } 
    35  
    36 sub test_self { 
    37     my $this = shift; 
     27    my $text = <<'HERE'; 
     28   * [[Bracketed.Link]] 
     29   * [[Bracketed.LinkWithTitle][with title]] 
     30   * WeblessWikiWord 
     31   * Web.WikiWord 
     32   * ACRO 
     33   * System.ACRONYM 
     34   * Foswiki:System.InterwikiPlugin 
     35   * [[Foswiki:System.BracketedInterwikiPlugin]] 
     36   * [[Foswiki:System.BracketedInterwikiPluginWithTitle][with title]] 
     37   * http://example.com/bare/link 
     38   * [[http://example.com/bracketed/link]] 
     39   * [[http://example.com/bracketed/link/with/title][with title]] 
     40   * [<nop>[nopBracketed.Link]] 
     41   * [<nop>[nopBracketed.LinkWithTitle][with title]] 
     42   * <nop>NopWeblessWikiWord 
     43   * <nop>NopWeb.WikiWord 
     44   * <nop>NOPACRO 
     45   * <nop>NopSystem.ACRONYM 
     46   * <nop>Foswiki:NopSystem.InterwikiPlugin 
     47   * [<nop>[Foswiki:NopSystem.BracketedInterwikiPlugin]] 
     48   * [<nop>[Foswiki:NopSystem.BracketedInterwikiPluginWithTitle][with title]] 
     49   * <nop>http://nopexample.com/bare/link 
     50   * [<nop>[http://nopexample.com/bracketed/link]] 
     51   * [<nop>[http://nopexample.com/bracketed/link/with/title][with title]] 
     52   * ![[ExBracketed.Link]] 
     53   * ![[ExBracketed.LinkWithTitle][with title]] 
     54   * !ExWeblessWikiWord 
     55   * !ExWeb.WikiWord 
     56   * !EXACRO 
     57   * !ExSystem.ACRONYM 
     58   * !Foswiki:ExSystem.InterwikiPlugin 
     59   * ![[Foswiki:ExSystem.BracketedInterwikiPlugin]] 
     60   * ![[Foswiki:ExSystem.BracketedInterwikiPluginWithTitle][with title]] 
     61   * !http://exexample.com/bare/link 
     62   * ![[http://exexample.com/bracketed/link]] 
     63   * ![[http://exexample.com/bracketed/link/with/title][with title]] 
     64HERE 
     65 
     66    # There's only 7 entries here, because SLP returns only *unique* web.topic 
     67    # and http:// links 
     68    my %expected_data = ( 
     69        'LINK' => [ 
     70            { 
     71                'topic'   => 'WikiWord', 
     72                'web'     => 'Web', 
     73                'name'    => 9, 
     74                'type'    => 'autolink', 
     75                'address' => 'Web.WikiWord', 
     76                'scope'   => 'internal' 
     77            }, 
     78            { 
     79                'name'    => 6, 
     80                'type'    => 'bracket', 
     81                'address' => 'http://example.com/bracketed/link/with/title', 
     82                'scope'   => 'external' 
     83            }, 
     84            { 
     85                'topic' => 'WeblessWikiWord', 
     86                'web'   => 'TemporarySemanticLinksPluginTestsUsersWeb', 
     87                'name'  => 8, 
     88                'type'  => 'autolink', 
     89                'address' => 
     90                  'TemporarySemanticLinksPluginTestsUsersWeb.WeblessWikiWord', 
     91                'scope' => 'internal' 
     92            }, 
     93            { 
     94                'topic'   => 'BracketedInterwikiPluginWithTitle', 
     95                'web'     => 'Foswiki:System', 
     96                'name'    => 4, 
     97                'type'    => 'bracket', 
     98                'address' => 'Foswiki:System.BracketedInterwikiPluginWithTitle', 
     99                'scope'   => 'internal' 
     100            }, 
     101            { 
     102                'topic'   => 'BracketedInterwikiPlugin', 
     103                'web'     => 'Foswiki:System', 
     104                'name'    => 3, 
     105                'type'    => 'bracket', 
     106                'address' => 'Foswiki:System.BracketedInterwikiPlugin', 
     107                'scope'   => 'internal' 
     108            }, 
     109            { 
     110                'name'    => 7, 
     111                'type'    => 'autolink', 
     112                'address' => 'http://example.com/bare/link', 
     113                'scope'   => 'external' 
     114            }, 
     115            { 
     116                'topic'   => 'ACRONYM', 
     117                'web'     => 'System', 
     118                'name'    => 10, 
     119                'type'    => 'autolink', 
     120                'address' => 'System.ACRONYM', 
     121                'scope'   => 'internal' 
     122            }, 
     123            { 
     124                'name'    => 5, 
     125                'type'    => 'bracket', 
     126                'address' => 'http://example.com/bracketed/link', 
     127                'scope'   => 'external' 
     128            }, 
     129            { 
     130                'topic'   => 'LinkWithTitle', 
     131                'web'     => 'Bracketed', 
     132                'name'    => 2, 
     133                'type'    => 'bracket', 
     134                'address' => 'Bracketed.LinkWithTitle', 
     135                'scope'   => 'internal' 
     136            }, 
     137            { 
     138                'topic'   => 'Link', 
     139                'web'     => 'Bracketed', 
     140                'name'    => 1, 
     141                'type'    => 'bracket', 
     142                'address' => 'Bracketed.Link', 
     143                'scope'   => 'internal' 
     144            } 
     145        ] 
     146    ); 
     147 
     148    foreach my $topic (qw(ACRONYM ACRO NOPACRO EXACRO)) { 
     149        my ($acronymTopicObj) = 
     150          Foswiki::Func::readTopic( $this->{test_web}, $topic ); 
     151        $acronymTopicObj->save(); 
     152        $acronymTopicObj->finish(); 
     153    } 
     154 
     155    my %actual_data = 
     156      Foswiki::Plugins::SemanticLinksPlugin::Core::_parse( $text, 
     157        $this->{test_topicObject} ); 
     158 
     159    $this->assert_deep_equals( \%expected_data, \%actual_data ); 
     160 
     161    # Check the save handler 
     162    $this->_check_save( $text, \%expected_data ); 
     163 
     164    return; 
     165} 
     166 
     167sub test_simple_semantic_links { 
     168    my ($this) = @_; 
     169    my $text = <<'HERE'; 
     170   * [[Property::Value]] 
     171   * [[:NonProperty::NonValue]] 
     172   * ![[ExProperty::ExValue]] 
     173   * [<nop>[NopProperty::NopValue]] 
     174   * [<nop>[:NopExProperty::NopValue]] 
     175HERE 
     176    my %expected_data = ( 
     177        'SLPROPERTYVALUE' => [], 
     178        'SLPROPERTY'      => [ 
     179            { 
     180                'num'  => 1, 
     181                'name' => 'Property' 
     182            } 
     183        ], 
     184        'LINK' => [ 
     185            { 
     186                'topic' => 'NonProperty::NonValue', 
     187                'web'   => 'TemporarySemanticLinksPluginTestsUsersWeb', 
     188                'name'  => 2, 
     189                'type'  => 'bracket', 
     190                'address' => 
     191'TemporarySemanticLinksPluginTestsUsersWeb.NonProperty::NonValue', 
     192                'scope' => 'internal' 
     193            }, 
     194            { 
     195                'topic'   => 'Value', 
     196                'web'     => 'TemporarySemanticLinksPluginTestsUsersWeb', 
     197                'name'    => 1, 
     198                'type'    => 'semantic', 
     199                'address' => 'TemporarySemanticLinksPluginTestsUsersWeb.Value', 
     200                'scope'   => 'internal' 
     201            } 
     202        ], 
     203        'SLMETAPROPERTY' => [], 
     204        'SLPROPERTIES'   => [], 
     205        'SLVALUE'        => [ 
     206            { 
     207                'propertyseq' => 1, 
     208                'valueweb'    => 'TemporarySemanticLinksPluginTestsUsersWeb', 
     209                'value'       => 'Value', 
     210                'name'        => 'Property__1', 
     211                'propertyweb' => 'TemporarySemanticLinksPluginTestsUsersWeb', 
     212                'valuetopic'  => 'Value', 
     213                'valueaddress' => 
     214                  'TemporarySemanticLinksPluginTestsUsersWeb.Value', 
     215                'propertyaddress' => 
     216                  'TemporarySemanticLinksPluginTestsUsersWeb.Property', 
     217                'property' => 'Property' 
     218            } 
     219        ], 
     220        'SLMETAVALUE' => [] 
     221    ); 
     222 
     223    # Check the links were processed correctly 
     224    my %actual_data = 
     225      Foswiki::Plugins::SemanticLinksPlugin::Core::_parse( $text, 
     226        $this->{test_topicObject} ); 
     227    $this->assert_deep_equals( \%expected_data, \%actual_data ); 
     228 
     229    # Check the save handler 
     230    $this->_check_save( $text, \%expected_data ); 
     231 
     232    return; 
     233} 
     234 
     235sub _check_save { 
     236    my ( $this, $text, $expected_data ) = @_; 
     237    my $checkObj; 
     238 
     239    $this->{test_topicObject}->text($text); 
     240    $this->{test_topicObject}->save(); 
     241    ($checkObj) = 
     242      Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} ); 
     243    foreach my $META ( 
     244        qw(SLMETAVALUE SLVALUE SLPROPERTIES SLMETAPROPERTY SLPROPERTY SLPROPERTYVALUE LINK) 
     245      ) 
     246    { 
     247        my @data = $checkObj->find($META); 
     248 
     249        $this->assert_deep_equals( $expected_data->{$META} || [], 
     250            \@data || [] ); 
     251    } 
     252    $checkObj->finish(); 
     253 
     254    return; 
    38255} 
    39256 
     
    42259Foswiki - The Free and Open Source Wiki, http://foswiki.org/ 
    43260 
    44 Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors 
     261Copyright (C) 2008-2012 Foswiki Contributors. Foswiki Contributors 
    45262are listed in the AUTHORS file in the root of this distribution. 
    46263NOTE: Please extend that file, not this notice. 
Note: See TracChangeset for help on using the changeset viewer.