<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase',
    'io.streams.FileOutputStream',
    'io.FileUtil',
    'io.TempFile'
  );

  /**
   * TestCase
   *
   * @see      xp://io.streams.FileOutputStream
   */
  class FileOutputStreamTest extends TestCase {
    protected 
      $file = NULL;
  
    /**
     * Sets up test case - creates temporary file
     *
     */
    public function setUp() {
      try {
        $this->file= new TempFile();
        FileUtil::setContents($this->file, 'Created by FileOutputStreamTest');
      } catch (IOException $e) {
        throw new PrerequisitesNotMetError('Cannot write temporary file', $e, array($this->file));
      }
    }
    
    /**
     * Tear down this test case - removes temporary file
     *
     */
    public function tearDown() {
      try {
        $this->file->isOpen() && $this->file->close();
        $this->file->unlink();
      } catch (IOException $ignored) {
        // Can't really do anything about it...
      }
    }
    
    /**
     * Test write() method
     *
     */
    #[@test]
    public function writing() {
      with ($stream= new FileOutputStream($this->file), $buffer= 'Created by '.$this->name); {
        $stream->write($buffer);
        $this->assertEquals($buffer, FileUtil::getContents($this->file));
      }
    }

    /**
     * Test write() method
     *
     */
    #[@test]
    public function appending() {
      with ($stream= new FileOutputStream($this->file, TRUE)); {
        $stream->write('!');
        $this->assertEquals('Created by FileOutputStreamTest!', FileUtil::getContents($this->file));
      }
    }

    /**
     * Test file remains open when FileOutputStream instance is deleted
     *
     */
    #[@test]
    public function delete() {
      with ($stream= new FileOutputStream($this->file)); {
        $this->assertTrue($this->file->isOpen());
        delete($stream);
        $this->assertTrue($this->file->isOpen());
      }
    }

    /**
     * Test opening a file output stream with a non-existant file name
     *
     */
    #[@test, @expect('io.IOException')]
    public function nonExistantFile() {
      new FileOutputStream('::NON-EXISTANT::');
    }

    /**
     * Test writing after stream has been closed
     *
     */
    #[@test, @expect('io.IOException')]
    public function writingAfterClose() {
      with ($stream= new FileOutputStream($this->file)); {
        $stream->close();
        $stream->write('');
      }
    }

    /**
     * Test closig an already closed stream
     *
     */
    #[@test, @expect('io.IOException')]
    public function doubleClose() {
      with ($stream= new FileOutputStream($this->file)); {
        $stream->close();
        $stream->close();
      }
    }<commit_msg>- use empty name as filename, to provoke error on both Un*x and W*ndows > ... and L*nux<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase',
    'io.streams.FileOutputStream',
    'io.FileUtil',
    'io.TempFile'
  );

  /**
   * TestCase
   *
   * @see      xp://io.streams.FileOutputStream
   */
  class FileOutputStreamTest extends TestCase {
    protected 
      $file = NULL;
  
    /**
     * Sets up test case - creates temporary file
     *
     */
    public function setUp() {
      try {
        $this->file= new TempFile();
        FileUtil::setContents($this->file, 'Created by FileOutputStreamTest');
      } catch (IOException $e) {
        throw new PrerequisitesNotMetError('Cannot write temporary file', $e, array($this->file));
      }
    }
    
    /**
     * Tear down this test case - removes temporary file
     *
     */
    public function tearDown() {
      try {
        $this->file->isOpen() && $this->file->close();
        $this->file->unlink();
      } catch (IOException $ignored) {
        // Can't really do anything about it...
      }
    }
    
    /**
     * Test write() method
     *
     */
    #[@test]
    public function writing() {
      with ($stream= new FileOutputStream($this->file), $buffer= 'Created by '.$this->name); {
        $stream->write($buffer);
        $this->assertEquals($buffer, FileUtil::getContents($this->file));
      }
    }

    /**
     * Test write() method
     *
     */
    #[@test]
    public function appending() {
      with ($stream= new FileOutputStream($this->file, TRUE)); {
        $stream->write('!');
        $this->assertEquals('Created by FileOutputStreamTest!', FileUtil::getContents($this->file));
      }
    }

    /**
     * Test file remains open when FileOutputStream instance is deleted
     *
     */
    #[@test]
    public function delete() {
      with ($stream= new FileOutputStream($this->file)); {
        $this->assertTrue($this->file->isOpen());
        delete($stream);
        $this->assertTrue($this->file->isOpen());
      }
    }

    /**
     * Test opening a file output stream with a non-existant file name
     *
     */
    #[@test, @expect('io.IOException')]
    public function nonExistantFile() {
      new FileOutputStream('');
    }

    /**
     * Test writing after stream has been closed
     *
     */
    #[@test, @expect('io.IOException')]
    public function writingAfterClose() {
      with ($stream= new FileOutputStream($this->file)); {
        $stream->close();
        $stream->write('');
      }
    }

    /**
     * Test closig an already closed stream
     *
     */
    #[@test, @expect('io.IOException')]
    public function doubleClose() {
      with ($stream= new FileOutputStream($this->file)); {
        $stream->close();
        $stream->close();
      }
    }
{'fraction_non_alphanumeric': 0.1596608972094666, 'fraction_numerical': 0.0, 'mean_word_length': 2.130458817025981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2565165', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1633, 'n_words': 387}