Permalink
Browse files

Convert managesieve test scripts to PHPUnit, add them to the suite

  • Loading branch information...
1 parent afa0b1d commit 4bb0bffefe3d0772f10bfd4acc3e8ccb1193211d @alecpl alecpl committed Aug 31, 2012
@@ -1,7 +0,0 @@
-
-clean:
- rm -f *.log *.php *.diff *.exp *.out
-
-
-test:
- pear run-tests *.phpt
@@ -0,0 +1,54 @@
+<?php
+
+class Parser extends PHPUnit_Framework_TestCase
+{
+
+ function setUp()
+ {
+ include_once dirname(__FILE__) . '/../lib/rcube_sieve_script.php';
+ }
+
+ /**
+ * Sieve script parsing
+ *
+ * @dataProvider data_parser
+ */
+ function test_parser($input, $output, $message)
+ {
+ $script = new rcube_sieve_script($input);
+ $result = $script->as_text();
+
+ $this->assertEquals(trim($result), trim($output), $message);
+ }
+
+ /**
+ * Data provider for test_parser()
+ */
+ function data_parser()
+ {
+ $dir_path = realpath(dirname(__FILE__) . '/src');
+ $dir = opendir($dir_path);
+ $result = array();
+
+ while ($file = readdir($dir)) {
+ if (preg_match('/^[a-z_]+$/', $file)) {
+ $input = file_get_contents($dir_path . '/' . $file);
+
+ if (file_exists($dir_path . '/' . $file . '.out')) {
+ $output = file_get_contents($dir_path . '/' . $file . '.out');
+ }
+ else {
+ $output = $input;
+ }
+
+ $result[] = array(
+ 'input' => $input,
+ 'output' => $output,
+ 'message' => "Error in parsing '$file' file",
+ );
+ }
+ }
+
+ return $result;
+ }
+}
@@ -0,0 +1,33 @@
+<?php
+
+class Tokenizer extends PHPUnit_Framework_TestCase
+{
+
+ function setUp()
+ {
+ include_once dirname(__FILE__) . '/../lib/rcube_sieve_script.php';
+ }
+
+ function data_tokenizer()
+ {
+ return array(
+ array(1, "text: #test\nThis is test ; message;\nMulti line\n.\n;\n", '"This is test ; message;\nMulti line"'),
+ array(0, '["test1","test2"]', '[["test1","test2"]]'),
+ array(1, '["test"]', '["test"]'),
+ array(1, '"te\\"st"', '"te\\"st"'),
+ array(0, 'test #comment', '["test"]'),
+ array(0, "text:\ntest\n.\ntext:\ntest\n.\n", '["test","test"]'),
+ array(1, '"\\a\\\\\\"a"', '"a\\\\\\"a"'),
+ );
+ }
+
+ /**
+ * @dataProvider data_tokenizer
+ */
+ function test_tokenizer($num, $input, $output)
+ {
+ $res = json_encode(rcube_sieve_script::tokenize($input, $num));
+
+ $this->assertEquals(trim($res), trim($output));
+ }
+}
@@ -1,120 +0,0 @@
---TEST--
-Main test of script parser
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-require ["fileinto","reject","envelope"];
-# rule:[spam]
-if anyof (header :contains "X-DSPAM-Result" "Spam")
-{
- fileinto "Spam";
- stop;
-}
-# rule:[test1]
-if anyof (header :comparator "i;ascii-casemap" :contains ["From","To"] "test@domain.tld")
-{
- discard;
- stop;
-}
-# rule:[test2]
-if anyof (not header :comparator "i;octet" :contains ["Subject"] "[test]", header :contains "Subject" "[test2]")
-{
- fileinto "test";
- stop;
-}
-# rule:[comments]
-if anyof (true) /* comment
- * "comment" #comment */ {
- /* comment */ stop;
-# comment
-}
-# rule:[reject]
-if size :over 5000K {
- reject "Message over 5MB size limit. Please contact me before sending this.";
-}
-# rule:[false]
-if false # size :over 5000K
-{
- stop; /* rule disabled */
-}
-# rule:[true]
-if true
-{
- stop;
-}
-fileinto "Test";
-# rule:[address test]
-if address :all :is "From" "nagios@domain.tld"
-{
- fileinto "domain.tld";
- stop;
-}
-# rule:[envelope test]
-if envelope :domain :is "From" "domain.tld"
-{
- fileinto "domain.tld";
- stop;
-}
-';
-
-$s = new rcube_sieve_script($txt);
-echo $s->as_text();
-
-// -------------------------------------------------------------------------------
-?>
---EXPECT--
-require ["fileinto","reject","envelope"];
-# rule:[spam]
-if header :contains "X-DSPAM-Result" "Spam"
-{
- fileinto "Spam";
- stop;
-}
-# rule:[test1]
-if header :contains ["From","To"] "test@domain.tld"
-{
- discard;
- stop;
-}
-# rule:[test2]
-if anyof (not header :comparator "i;octet" :contains "Subject" "[test]", header :contains "Subject" "[test2]")
-{
- fileinto "test";
- stop;
-}
-# rule:[comments]
-if true
-{
- stop;
-}
-# rule:[reject]
-if size :over 5000K
-{
- reject "Message over 5MB size limit. Please contact me before sending this.";
-}
-# rule:[false]
-if false # size :over 5000K
-{
- stop;
-}
-# rule:[true]
-if true
-{
- stop;
-}
-fileinto "Test";
-# rule:[address test]
-if address :all :is "From" "nagios@domain.tld"
-{
- fileinto "domain.tld";
- stop;
-}
-# rule:[envelope test]
-if envelope :domain :is "From" "domain.tld"
-{
- fileinto "domain.tld";
- stop;
-}
@@ -1,49 +0,0 @@
---TEST--
-Test of Sieve body extension (RFC5173)
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-require ["body","fileinto"];
-if body :raw :contains "MAKE MONEY FAST"
-{
- stop;
-}
-if body :content "text" :contains ["missile","coordinates"]
-{
- fileinto "secrets";
-}
-if body :content "audio/mp3" :contains ""
-{
- fileinto "jukebox";
-}
-if body :text :contains "project schedule"
-{
- fileinto "project/schedule";
-}
-';
-
-$s = new rcube_sieve_script($txt);
-echo $s->as_text();
-
-?>
---EXPECT--
-require ["body","fileinto"];
-if body :raw :contains "MAKE MONEY FAST"
-{
- stop;
-}
-if body :content "text" :contains ["missile","coordinates"]
-{
- fileinto "secrets";
-}
-if body :content "audio/mp3" :contains ""
-{
- fileinto "jukebox";
-}
-if body :text :contains "project schedule"
-{
- fileinto "project/schedule";
-}
@@ -1,28 +0,0 @@
---TEST--
-Test of Sieve vacation extension (RFC5232)
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-require ["imapflags"];
-# rule:[imapflags]
-if header :matches "Subject" "^Test$" {
- setflag "\\\\Seen";
- addflag ["\\\\Answered","\\\\Deleted"];
-}
-';
-
-$s = new rcube_sieve_script($txt, array('imapflags'));
-echo $s->as_text();
-
-?>
---EXPECT--
-require ["imapflags"];
-# rule:[imapflags]
-if header :matches "Subject" "^Test$"
-{
- setflag "\\Seen";
- addflag ["\\Answered","\\Deleted"];
-}
@@ -1,30 +0,0 @@
---TEST--
-Test of Sieve include extension
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-require ["include"];
-
-include "script.sieve";
-# rule:[two]
-if true
-{
- include :optional "second.sieve";
-}
-';
-
-$s = new rcube_sieve_script($txt, array(), array('variables'));
-echo $s->as_text();
-
-?>
---EXPECT--
-require ["include"];
-include "script.sieve";
-# rule:[two]
-if true
-{
- include :optional "second.sieve";
-}
@@ -1,19 +0,0 @@
---TEST--
-Test of Kolab's KEP:14 implementation
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-# EDITOR Roundcube
-# EDITOR_VERSION 123
-';
-
-$s = new rcube_sieve_script($txt, array('body'));
-echo $s->as_text();
-
-?>
---EXPECT--
-# EDITOR Roundcube
-# EDITOR_VERSION 123
@@ -1,25 +0,0 @@
---TEST--
-Test of prefix comments handling
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-# this is a comment
-# and the second line
-
-require ["variables"];
-set "b" "c";
-';
-
-$s = new rcube_sieve_script($txt, array(), array('variables'));
-echo $s->as_text();
-
-?>
---EXPECT--
-# this is a comment
-# and the second line
-
-require ["variables"];
-set "b" "c";
@@ -1,25 +0,0 @@
---TEST--
-Test of Sieve relational extension (RFC5231)
---SKIPIF--
---FILE--
-<?php
-include '../lib/rcube_sieve_script.php';
-
-$txt = '
-require ["relational","comparator-i;ascii-numeric"];
-# rule:[redirect]
-if header :value "ge" :comparator "i;ascii-numeric"
- ["X-Spam-score"] ["14"] {redirect "test@test.tld";}
-';
-
-$s = new rcube_sieve_script($txt);
-echo $s->as_text();
-
-?>
---EXPECT--
-require ["relational","comparator-i;ascii-numeric"];
-# rule:[redirect]
-if header :value "ge" :comparator "i;ascii-numeric" "X-Spam-score" "14"
-{
- redirect "test@test.tld";
-}
Oops, something went wrong.

0 comments on commit 4bb0bff

Please sign in to comment.