Skip to content

Instantly share code, notes, and snippets.

@thekid
Created July 10, 2011 09:44
Show Gist options
  • Save thekid/1074417 to your computer and use it in GitHub Desktop.
Save thekid/1074417 to your computer and use it in GitHub Desktop.
XP Framework: Patch for issue #28
diff --git a/core/src/main/php/io/File.class.php b/core/src/main/php/io/File.class.php
index 15b165a..635c757 100644
--- a/core/src/main/php/io/File.class.php
+++ b/core/src/main/php/io/File.class.php
@@ -323,7 +323,8 @@
* @throws io.IOException in case of an error
*/
public function readLine($bytes= 4096) {
- return chop($this->gets($bytes));
+ $bytes= $this->gets($bytes);
+ return FALSE === $bytes ? FALSE : chop($bytes);
}
/**
@@ -375,7 +376,7 @@
xp::gc(__FILE__);
throw $e;
}
- return $result;
+ return '' === $result ? FALSE : $result;
}
/**
diff --git a/core/src/main/php/io/streams/FileInputStream.class.php b/core/src/main/php/io/streams/FileInputStream.class.php
index ff28929..bbf47c9 100644
--- a/core/src/main/php/io/streams/FileInputStream.class.php
+++ b/core/src/main/php/io/streams/FileInputStream.class.php
@@ -33,7 +33,7 @@
* @return string
*/
public function read($limit= 8192) {
- return $this->file->read($limit);
+ return (string)$this->file->read($limit);
}
/**
diff --git a/core/src/test/php/net/xp_framework/unittest/io/FileIntegrationTest.class.php b/core/src/test/php/net/xp_framework/unittest/io/FileIntegrationTest.class.php
index 5180e10..81ee71c 100644
--- a/core/src/test/php/net/xp_framework/unittest/io/FileIntegrationTest.class.php
+++ b/core/src/test/php/net/xp_framework/unittest/io/FileIntegrationTest.class.php
@@ -208,6 +208,208 @@
*
*/
#[@test]
+ public function read0() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('', $this->file->read(0));
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data
+ *
+ */
+ #[@test]
+ public function readAfterEnd() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals($data, $this->file->read(strlen($data)));
+ $this->assertFalse($this->file->read(1));
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using gets()
+ *
+ */
+ #[@test]
+ public function gets() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals($data, $this->file->gets());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using gets()
+ *
+ */
+ #[@test]
+ public function gets0() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('', $this->file->gets(0));
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using gets()
+ *
+ */
+ #[@test]
+ public function getsTwoLines() {
+ with ($data= "Hello\nWorld\n"); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals("Hello\n", $this->file->gets());
+ $this->assertEquals("World\n", $this->file->gets());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using gets()
+ *
+ */
+ #[@test]
+ public function getsAfterEnd() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('Hello', $this->file->gets());
+ $this->assertFalse($this->file->gets());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readLine()
+ *
+ */
+ #[@test]
+ public function readLine() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals($data, $this->file->readLine());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readLine()
+ *
+ */
+ #[@test]
+ public function readLine0() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('', $this->file->readLine(0));
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readLine()
+ *
+ */
+ #[@test]
+ public function readLines() {
+ with ($data= "Hello\nWorld\n"); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('Hello', $this->file->readLine());
+ $this->assertEquals('World', $this->file->readLine());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readLine()
+ *
+ */
+ #[@test]
+ public function readLinesAfterEnd() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('Hello', $this->file->readLine());
+ $this->assertFalse($this->file->readLine());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readChar()
+ *
+ */
+ #[@test]
+ public function readChar() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals($data{0}, $this->file->readChar());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readChar()
+ *
+ */
+ #[@test]
+ public function readChars() {
+ with ($data= 'Hello'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals($data{0}, $this->file->readChar());
+ $this->assertEquals($data{1}, $this->file->readChar());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data using readChar()
+ *
+ */
+ #[@test]
+ public function readCharsAfterEnd() {
+ with ($data= 'H'); {
+ $this->writeData($this->file, $data);
+
+ $this->file->open(FILE_MODE_READ);
+ $this->assertEquals('H', $this->file->readChar());
+ $this->assertFalse($this->file->readChar());
+ $this->file->close();
+ }
+ }
+
+ /**
+ * Test writing to a file, then reading back the data
+ *
+ */
+ #[@test]
public function overwritingExistant() {
with ($data= 'Hello World', $appear= 'This should not appear'); {
$this->writeData($this->file, $appear);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment