Commit daebb598690c47600cf033be77acaad1ae8c4a70

Russell Belfer 2014-01-27T14:57:03

Add PHP tests and fix bug in PHP builtin driver

diff --git a/src/userdiff.h b/src/userdiff.h
index 2257035..9318b54 100644
--- a/src/userdiff.h
+++ b/src/userdiff.h
@@ -186,7 +186,7 @@ PATTERNS("csharp",
 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
 
 PATTERNS("php",
-	 "^[ \t]*((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*)$",
+	 "^[ \t]*(((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*))$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
diff --git a/tests/resources/userdiff/.gitted/index b/tests/resources/userdiff/.gitted/index
index df041cf..b69d5cf 100644
Binary files a/tests/resources/userdiff/.gitted/index and b/tests/resources/userdiff/.gitted/index differ
diff --git a/tests/resources/userdiff/.gitted/info/refs b/tests/resources/userdiff/.gitted/info/refs
index 261695f..b074314 100644
--- a/tests/resources/userdiff/.gitted/info/refs
+++ b/tests/resources/userdiff/.gitted/info/refs
@@ -1 +1 @@
-802734c801bffc466d69876bc4814f747aaac4fe	refs/heads/master
+60e3f7b244a5305e2c9fa4ef0e897f3b14f3b8dd	refs/heads/master
diff --git a/tests/resources/userdiff/.gitted/objects/info/packs b/tests/resources/userdiff/.gitted/objects/info/packs
index 6970fd7..0c5fc2a 100644
--- a/tests/resources/userdiff/.gitted/objects/info/packs
+++ b/tests/resources/userdiff/.gitted/objects/info/packs
@@ -1,2 +1,2 @@
-P pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack
+P pack-1652578900ac63564f2a24b9714529821276ceb9.pack
 
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx b/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx
deleted file mode 100644
index 6d723f5..0000000
Binary files a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx and /dev/null differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack b/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack
deleted file mode 100644
index 5b263e2..0000000
Binary files a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack and /dev/null differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx
new file mode 100644
index 0000000..6f4381c
Binary files /dev/null and b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack
new file mode 100644
index 0000000..39bd1d8
Binary files /dev/null and b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack differ
diff --git a/tests/resources/userdiff/.gitted/packed-refs b/tests/resources/userdiff/.gitted/packed-refs
index 6d24ee4..802f67c 100644
--- a/tests/resources/userdiff/.gitted/packed-refs
+++ b/tests/resources/userdiff/.gitted/packed-refs
@@ -1,2 +1,2 @@
 # pack-refs with: peeled fully-peeled 
-802734c801bffc466d69876bc4814f747aaac4fe refs/heads/master
+60e3f7b244a5305e2c9fa4ef0e897f3b14f3b8dd refs/heads/master
diff --git a/tests/resources/userdiff/.gitted/refs/dummy-marker.txt b/tests/resources/userdiff/.gitted/refs/dummy-marker.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/resources/userdiff/.gitted/refs/dummy-marker.txt
diff --git a/tests/resources/userdiff/after/file.php b/tests/resources/userdiff/after/file.php
new file mode 100644
index 0000000..967d646
--- /dev/null
+++ b/tests/resources/userdiff/after/file.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+    protected $generator;
+    protected $maxRetries;
+	protected $moreStuff;
+    protected $uniques = array();
+
+    public function __construct(Generator $generator, $maxRetries)
+    {
+        $this->generator = $generator;
+        $this->maxRetries = $maxRetries + 1;
+    }
+
+    /**
+     * Catch and proxy all generator calls but return only unique values
+     */
+    public function __get($attribute)
+    {
+        return $this->__call($attribute, array());
+    }
+
+    /**
+     * Catch and proxy all generator calls with arguments but return only unique values
+     */
+    public function __call($name, $arguments)
+    {
+        $i = 0;
+        if (!isset($this->uniques[$name])) {
+            $this->uniques[$name] = array();
+        }
+        do {
+            $res = call_user_func_array(array($this->generator, $name), $arguments);
+            $i++;
+            if ($i >= $this->maxRetries) {
+                throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+            }
+        } while (in_array($res, $this->uniques[$name]));
+        $this->uniques[$name][]= $res;
+
+        return $res;
+    }
+}
diff --git a/tests/resources/userdiff/before/file.php b/tests/resources/userdiff/before/file.php
new file mode 100644
index 0000000..63250ad
--- /dev/null
+++ b/tests/resources/userdiff/before/file.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+    protected $generator;
+    protected $maxRetries;
+    protected $uniques = array();
+
+    public function __construct(Generator $generator, $maxRetries)
+    {
+        $this->generator = $generator;
+        $this->maxRetries = $maxRetries;
+    }
+
+    /**
+     * Catch and proxy all generator calls but return only unique values
+     */
+    public function __get($attribute)
+    {
+        return $this->__call($attribute, array());
+    }
+
+    /**
+     * Catch and proxy all generator calls with arguments but return only unique values
+     */
+    public function __call($name, $arguments)
+    {
+        if (!isset($this->uniques[$name])) {
+            $this->uniques[$name] = array();
+        }
+        $i = 0;
+        do {
+            $res = call_user_func_array(array($this->generator, $name), $arguments);
+            $i++;
+            if ($i > $this->maxRetries) {
+                throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+            }
+        } while (in_array($res, $this->uniques[$name]));
+        $this->uniques[$name][]= $res;
+
+        return $res;
+    }
+}
diff --git a/tests/resources/userdiff/expected/driver/diff.php b/tests/resources/userdiff/expected/driver/diff.php
new file mode 100644
index 0000000..9711b5b
--- /dev/null
+++ b/tests/resources/userdiff/expected/driver/diff.php
@@ -0,0 +1,26 @@
+diff --git a/files/file.php b/files/file.php
+index 63250ad..967d646 100644
+--- a/files/file.php
++++ b/files/file.php
+@@ -12,2 +12,3 @@ class UniqueGenerator
+     protected $maxRetries;
++	protected $moreStuff;
+     protected $uniques = array();
+@@ -17,3 +18,3 @@ public function __construct(Generator $generator, $maxRetries)
+         $this->generator = $generator;
+-        $this->maxRetries = $maxRetries;
++        $this->maxRetries = $maxRetries + 1;
+     }
+@@ -33,10 +34,10 @@ public function __call($name, $arguments)
+     {
++        $i = 0;
+         if (!isset($this->uniques[$name])) {
+             $this->uniques[$name] = array();
+         }
+-        $i = 0;
+         do {
+             $res = call_user_func_array(array($this->generator, $name), $arguments);
+             $i++;
+-            if ($i > $this->maxRetries) {
++            if ($i >= $this->maxRetries) {
+                 throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
diff --git a/tests/resources/userdiff/expected/nodriver/diff.php b/tests/resources/userdiff/expected/nodriver/diff.php
new file mode 100644
index 0000000..e77c094
--- /dev/null
+++ b/tests/resources/userdiff/expected/nodriver/diff.php
@@ -0,0 +1,26 @@
+diff --git a/files/file.php b/files/file.php
+index 63250ad..967d646 100644
+--- a/files/file.php
++++ b/files/file.php
+@@ -12,2 +12,3 @@ class UniqueGenerator
+     protected $maxRetries;
++	protected $moreStuff;
+     protected $uniques = array();
+@@ -17,3 +18,3 @@ class UniqueGenerator
+         $this->generator = $generator;
+-        $this->maxRetries = $maxRetries;
++        $this->maxRetries = $maxRetries + 1;
+     }
+@@ -33,10 +34,10 @@ class UniqueGenerator
+     {
++        $i = 0;
+         if (!isset($this->uniques[$name])) {
+             $this->uniques[$name] = array();
+         }
+-        $i = 0;
+         do {
+             $res = call_user_func_array(array($this->generator, $name), $arguments);
+             $i++;
+-            if ($i > $this->maxRetries) {
++            if ($i >= $this->maxRetries) {
+                 throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
diff --git a/tests/resources/userdiff/files/file.php b/tests/resources/userdiff/files/file.php
new file mode 100644
index 0000000..967d646
--- /dev/null
+++ b/tests/resources/userdiff/files/file.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+    protected $generator;
+    protected $maxRetries;
+	protected $moreStuff;
+    protected $uniques = array();
+
+    public function __construct(Generator $generator, $maxRetries)
+    {
+        $this->generator = $generator;
+        $this->maxRetries = $maxRetries + 1;
+    }
+
+    /**
+     * Catch and proxy all generator calls but return only unique values
+     */
+    public function __get($attribute)
+    {
+        return $this->__call($attribute, array());
+    }
+
+    /**
+     * Catch and proxy all generator calls with arguments but return only unique values
+     */
+    public function __call($name, $arguments)
+    {
+        $i = 0;
+        if (!isset($this->uniques[$name])) {
+            $this->uniques[$name] = array();
+        }
+        do {
+            $res = call_user_func_array(array($this->generator, $name), $arguments);
+            $i++;
+            if ($i >= $this->maxRetries) {
+                throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+            }
+        } while (in_array($res, $this->uniques[$name]));
+        $this->uniques[$name][]= $res;
+
+        return $res;
+    }
+}