OSDN Git Service

testFunc_uri_pickup(): Added two more tests
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
index 8112909..6239dd6 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.18 2007/07/02 14:51:40 henoheno Exp $
+// $Id: SpamTest.php,v 1.24 2008/12/27 15:21:41 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -11,6 +11,41 @@ require_once('PHPUnit/PHPUnit.php');
 
 class SpamTest extends PHPUnit_TestCase
 {
+       function testFunc_delimiter_reverse()
+       {
+               // Simple
+               $this->assertEquals('com.example.bar.foo',
+                        delimiter_reverse('foo.bar.example.com'));
+
+               // A vector (an simple array)
+               $array =            array('foo.ba2r', 'foo.bar2');
+               $this->assertEquals(array('ba2r|foo', 'bar2|foo'),
+                       delimiter_reverse($array, '.', '|'));
+
+               // Note: array_map() vanishes all keys
+               $array =            array('FB' => 'foo.ba2r', 'FB2' => 'foo.bar2');
+               $this->assertEquals(array('ba2r|foo', 'bar2|foo'),
+                       delimiter_reverse($array, '.', '|'));
+
+               // A tree (recurse)
+               $array =            array('foo.ba2r', 'foo.bar2', array('john.doe', 'bob.dude'));
+               $this->assertEquals(array('ba2r|foo', 'bar2|foo', array('doe|john', 'dude|bob')),
+                       delimiter_reverse($array, '.', '|'));
+
+               // Nothing changes
+               $this->assertEquals('100', delimiter_reverse('100'));
+               $this->assertEquals(array(), delimiter_reverse(array()));
+
+               // Invalid cases
+               $this->assertEquals(FALSE, delimiter_reverse(TRUE));
+               $this->assertEquals(FALSE, delimiter_reverse(FALSE));
+               $this->assertEquals(FALSE, delimiter_reverse(NULL));
+               $this->assertEquals(FALSE, delimiter_reverse(100));
+               $this->assertEquals(FALSE, delimiter_reverse('100', FALSE));
+               $this->assertEquals(FALSE, delimiter_reverse('100', 0));
+               $this->assertEquals(FALSE, delimiter_reverse('100', '0', 0));
+       }
+
        function setup_string_null()
        {
                return array(
@@ -139,58 +174,70 @@ class SpamTest extends PHPUnit_TestCase
                );
        }
 
-       // And array_unique_recursive()
-       function testPhPFunc_array_merge_recursive()
+       function testFunc_array_merge_leaves()
        {
+               // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
                $array1 = array(1);
                $array2 = array(1);
                $result = array_merge_recursive($array1, $array2);
                $this->assertEquals(array(1, 1), $result);
                $result = array_unique_recursive($result);
                $this->assertEquals(array(1),    $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array(1),    $result);
 
                $array1 = array(2);
                $array2 = array(1);
-               $result = array(2, 1);
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(2, 1), $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array(1),    $result);
 
-               // All NUMERIC keys are always renumbered from 0
+               // All NUMERIC keys are always renumbered from 0?
                $array1 = array('10' => 'f3');
                $array2 = array('10' => 'f4');
-               $result = array('f3', 'f4');
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array(10 => 'f4'), $result);
 
                // One more thing ...
                $array1 = array('20' => 'f5');
                $array2 = array();
-               $result = array('f5');
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(0 => 'f5'), $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array(20 => 'f5'), $result);
 
-               // Non-numeric keys and values will be marged as you think
+               // Non-numeric keys and values will be marged as you think?
                $array1 = array('a' => 'f1');
                $array2 = array('a' => 'f2');
-               $result = array('a' => array('f1', 'f2'));
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array('a' => array('f1', 'f2')), $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('a' => 'f2'), $result);
 
-               // Non-numeric keys: An array and a value will be marged
+               // Non-numeric keys: An array and a value will be marged?
                $array1 = array('b' => array('k1'));
                $array2 = array('b' => 'k2');
-               $result = array('b' => array('k1', 'k2'));
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('b' => array(0 => 'k1')), $result);
 
-               // Combination
+               // Combination?
                $array1 = array(
                        2,
-                       'a' => 'f1',
+                       'a'  => 'f1',
                        '10' => 'f3',
                        '20' => 'f5',
-                       'b' => array('k1'),
+                       'b'  => array('k1'),
                );
                $array2 = array(
                        1,
-                       'a' => 'f2',
+                       'a'  => 'f2',
                        '10' => 'f4',
-                       'b' => 'k2',
+                       'b'  => 'k2',
                );
                $result = array (
                        2,
@@ -207,23 +254,65 @@ class SpamTest extends PHPUnit_TestCase
                        1,
                        'f4',
                );
-               $this->assertEquals($result, array_merge_recursive($array1, $array2));
+               $result2 = array (
+                        0  => 1,
+                       10  => 'f4',
+                       20  => 'f5',
+                       'a' => 'f2',
+                       'b' => array ('k1'),
+               );
+               $this->assertEquals($result,  array_merge_recursive($array1, $array2));
+               $this->assertEquals($result2, array_merge_leaves($array1, $array2));
 
-               // Values will not be unique
+               // Values will not be unique?
                $array1 = array(5, 4);
                $array2 = array(4, 5);
                $result = array_merge_recursive($array1, $array2);
                $this->assertEquals(array(5, 4, 4, 5), $result);
-               $result = array_unique_recursive($result);
-               $this->assertEquals(array(5, 4),       $result);
+               $this->assertEquals(array(5, 4),       array_unique_recursive($result));
+               $this->assertEquals(array(0=>4, 1=>5), array_merge_leaves($array1, $array2));
 
-               // One more thing ...
+               // One more thing ...?
                $array1 = array('b' => array('k3'));
                $array2 = array('b' => 'k3');
                $result = array_merge_recursive($array1, $array2);
                $this->assertEquals(array('b' => array('k3', 'k3')), $result);
                $result = array_unique_recursive($result);
                $this->assertEquals(array('b' => array('k3')),       $result);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('b' => array('k3')), $result);
+
+               // Preserve numeric keys?
+               $array1 = array('a' => array('' => NULL));
+               $array2 = array('a' => array(5  => NULL));
+               $array3 = array('a' => array(8  => NULL));
+               //
+               // BAD: PHP array_merge_recursive() don't preserve numeric keys
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result);       // 0?
+               $result = array_merge_recursive($array2, $array3);
+               $this->assertEquals(array('a' => array(5 => NULL,  6 => NULL)), $result);       // 6?
+               //
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result);       // 0?
+               $result = array_merge_leaves($array2, $array3);
+               $this->assertEquals(array('a' => array(5 => NULL,  8 => NULL)), $result);       // 6?
+
+               // Merging array leaves
+               $array1 = array('a' => TRUE);
+               $array2 = array('b' => FALSE);
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result);
+
+               $array1 = array('a' => TRUE);
+               $array2 = array('a' => array('aa' => TRUE));
+               $this->assertEquals($array2, array_merge_leaves($array1, $array2));
+               $this->assertEquals($array2, array_merge_leaves($array2, $array1));
+
+               $array1 = array('a' => array('a1' => TRUE));
+               $array2 = array('a' => array('a2' => FALSE));
+               $result = array_merge_leaves($array1, $array2);
+               $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result);
        }
 
        function testFunc_generate_glob_regex()
@@ -255,6 +344,38 @@ class SpamTest extends PHPUnit_TestCase
                //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
        }
 
+       function testFunc_is_ip()
+       {
+               // 1st argument: Null
+               foreach($this->setup_string_null() as $key => $value){
+                       $this->assertEquals(FALSE, is_ip($value), $key);
+               }
+
+               // IPv4
+               foreach(array(
+                               '192.168.1.1',
+                       ) as $value){
+                       $this->assertEquals(4,  is_ip($value), $key, '[' . $value . ']');
+               }
+
+               // IPv6
+               foreach(array(
+                               '::',                           // 0:0:0:0:0:0:0:0
+                               '::192.168.1.1',        // IPv4 within IPv6 network
+                       ) as $value){
+                       $this->assertEquals(6,  is_ip($value), $key, '[' . $value . ']');
+               }
+
+               // Invalid
+               foreach(array(
+                               '',
+                               '.',
+                       ) as $value){
+                       $this->assertEquals(FALSE,      is_ip($value), $key, '[' . $value . ']');
+               }
+
+       }
+
        function testFunc_get_blocklist()
        {
                if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
@@ -264,21 +385,21 @@ class SpamTest extends PHPUnit_TestCase
 
                // get_blocklist_add()
                $array = array();
-
+               //
                get_blocklist_add($array,   'foo', 'bar');
                $this->assertEquals(1,      count($array));
                $this->assertEquals('bar',  $array['foo']);
-
+               //
                get_blocklist_add($array,   'hoge', 'fuga');
                $this->assertEquals(2,      count($array));
                $this->assertEquals('bar',  $array['foo']);
                $this->assertEquals('fuga', $array['hoge']);
-
+               //
                get_blocklist_add($array,   -1, '*.txt');
                $this->assertEquals(3,      count($array));
                $this->assertEquals('bar',  $array['foo']);
                $this->assertEquals('fuga', $array['hoge']);
-               $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
+               $this->assertEquals('#^.*\.txt$#i', $array['*.txt']);
 
                // get_blocklist()
                // ALL
@@ -287,7 +408,7 @@ class SpamTest extends PHPUnit_TestCase
                $this->assertTrue(isset($array['goodhost']));
                // badhost
                $array = get_blocklist('B-1');
-               $this->assertTrue(isset($array['*.blogspot.com']));
+               $this->assertTrue(isset($array['Google.com']));
                // goodhost
                $array = get_blocklist('goodhost');
                $this->assertTrue(isset($array['IANA-examples']));