From 3d56c1253b9618b50ba22c9d61ad7ac3ce1763d2 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 10 Feb 2008 01:33:40 +0000 Subject: [PATCH] Add version extraction functionality git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1536 48356398-32a2-884e-a903-53898d9a118a --- extras/ConfigSchema/StringHashReverseAdapter.php | 11 ++++++++++- .../StringHashReverseAdapterTest.php | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/extras/ConfigSchema/StringHashReverseAdapter.php b/extras/ConfigSchema/StringHashReverseAdapter.php index b953e9a9..c9d513a5 100644 --- a/extras/ConfigSchema/StringHashReverseAdapter.php +++ b/extras/ConfigSchema/StringHashReverseAdapter.php @@ -112,7 +112,16 @@ class ConfigSchema_StringHashReverseAdapter } public function extractVersion($description) { - return array($description, false); + $regex = '/This directive (?:has been|was) available since (\d+\.\d+\.\d+)\./'; + $regex = str_replace(' ', '\s+', $regex); // allow any number of spaces between statements + $ok = preg_match($regex, $description, $matches); + if ($ok) { + $version = $matches[1]; + } else { + $version = false; + } + $description = preg_replace($regex, '', $description, 1); + return array($description, $version); } } diff --git a/tests/ConfigSchema/StringHashReverseAdapterTest.php b/tests/ConfigSchema/StringHashReverseAdapterTest.php index 4f156966..f33ed100 100644 --- a/tests/ConfigSchema/StringHashReverseAdapterTest.php +++ b/tests/ConfigSchema/StringHashReverseAdapterTest.php @@ -8,7 +8,7 @@ class ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase $schema->addNamespace('Ns', 'Description of ns.'); $schema->addNamespace('Ns2', 'Description of ns2.'); $schema->add('Ns', 'Dir', 'dairy', 'string', - "Description of default.\n"); + "Description of default.\nThis directive has been available since 1.2.0."); $schema->addAllowedValues('Ns', 'Dir', array('dairy', 'meat')); $schema->addValueAliases('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy')); $schema->addAlias('Ns', 'Dir2', 'Ns', 'Dir'); @@ -40,6 +40,7 @@ class ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase $expect = array( 'ID' => 'Ns.Dir', 'TYPE' => 'string', + 'VERSION' => '1.2.0', 'DEFAULT' => "'dairy'", 'DESCRIPTION' => "Description of default.\n", 'ALLOWED' => "'dairy', 'meat'", @@ -84,7 +85,18 @@ class ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase } function assertExtraction($desc, $expect_desc, $expect_version) { - + $adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema()); + list($result_desc, $result_version) = $adapter->extractVersion($desc); + $this->assertIdentical($result_desc, $expect_desc); + $this->assertIdentical($result_version, $expect_version); + } + + function testExtractSimple() { + $this->assertExtraction("Desc.\nThis directive has been available since 2.0.0.", "Desc.\n", '2.0.0'); + } + + function testExtractMultiline() { + $this->assertExtraction("Desc.\nThis directive was available\n since 23.4.333.", "Desc.\n", '23.4.333'); } }