addon-sdk/source/test/test-text-streams.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-text-streams.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const file = require("sdk/io/file");
     1.9 +const { pathFor } = require("sdk/system");
    1.10 +const { Loader } = require("sdk/test/loader");
    1.11 +
    1.12 +const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used.");
    1.13 +
    1.14 +// This should match the constant of the same name in text-streams.js.
    1.15 +const BUFFER_BYTE_LEN = 0x8000;
    1.16 +
    1.17 +exports.testWriteRead = function (assert) {
    1.18 +  let fname = dataFileFilename();
    1.19 +
    1.20 +  // Write a small string less than the stream's buffer size...
    1.21 +  let str = "exports.testWriteRead data!";
    1.22 +  let stream = file.open(fname, "w");
    1.23 +  assert.ok(!stream.closed, "stream.closed after open should be false");
    1.24 +  stream.write(str);
    1.25 +  stream.close();
    1.26 +  assert.ok(stream.closed, "stream.closed after close should be true");
    1.27 +  assert.throws(function () stream.close(),
    1.28 +                    STREAM_CLOSED_ERROR,
    1.29 +                    "stream.close after already closed should raise error");
    1.30 +  assert.throws(function () stream.write("This shouldn't be written!"),
    1.31 +                    STREAM_CLOSED_ERROR,
    1.32 +                    "stream.write after close should raise error");
    1.33 +
    1.34 +  // ... and read it.
    1.35 +  stream = file.open(fname);
    1.36 +  assert.ok(!stream.closed, "stream.closed after open should be false");
    1.37 +  assert.equal(stream.read(), str,
    1.38 +                   "stream.read should return string written");
    1.39 +  assert.equal(stream.read(), "",
    1.40 +                   "stream.read at EOS should return empty string");
    1.41 +  stream.close();
    1.42 +  assert.ok(stream.closed, "stream.closed after close should be true");
    1.43 +  assert.throws(function () stream.close(),
    1.44 +                    STREAM_CLOSED_ERROR,
    1.45 +                    "stream.close after already closed should raise error");
    1.46 +  assert.throws(function () stream.read(),
    1.47 +                    STREAM_CLOSED_ERROR,
    1.48 +                    "stream.read after close should raise error");
    1.49 +
    1.50 +  // Write a big string many times the size of the stream's buffer and read it.
    1.51 +  // Since it comes after the previous test, this also ensures that the file is
    1.52 +  // truncated when it's opened for writing.
    1.53 +  str = "";
    1.54 +  let bufLen = BUFFER_BYTE_LEN;
    1.55 +  let fileSize = bufLen * 10;
    1.56 +  for (let i = 0; i < fileSize; i++)
    1.57 +    str += i % 10;
    1.58 +  stream = file.open(fname, "w");
    1.59 +  stream.write(str);
    1.60 +  stream.close();
    1.61 +  stream = file.open(fname);
    1.62 +  assert.equal(stream.read(), str,
    1.63 +                   "stream.read should return string written");
    1.64 +  stream.close();
    1.65 +
    1.66 +  // The same, but write and read in chunks.
    1.67 +  stream = file.open(fname, "w");
    1.68 +  let i = 0;
    1.69 +  while (i < str.length) {
    1.70 +    // Use a chunk length that spans buffers.
    1.71 +    let chunk = str.substr(i, bufLen + 1);
    1.72 +    stream.write(chunk);
    1.73 +    i += bufLen + 1;
    1.74 +  }
    1.75 +  stream.close();
    1.76 +  stream = file.open(fname);
    1.77 +  let readStr = "";
    1.78 +  bufLen = BUFFER_BYTE_LEN;
    1.79 +  let readLen = bufLen + 1;
    1.80 +  do {
    1.81 +    var frag = stream.read(readLen);
    1.82 +    readStr += frag;
    1.83 +  } while (frag);
    1.84 +  stream.close();
    1.85 +  assert.equal(readStr, str,
    1.86 +                   "stream.write and read in chunks should work as expected");
    1.87 +
    1.88 +  // Read the same file, passing in strange numbers of bytes to read.
    1.89 +  stream = file.open(fname);
    1.90 +  assert.equal(stream.read(fileSize * 100), str,
    1.91 +                   "stream.read with big byte length should return string " +
    1.92 +                   "written");
    1.93 +  stream.close();
    1.94 +
    1.95 +  stream = file.open(fname);
    1.96 +  assert.equal(stream.read(0), "",
    1.97 +                   "string.read with zero byte length should return empty " +
    1.98 +                   "string");
    1.99 +  stream.close();
   1.100 +
   1.101 +  stream = file.open(fname);
   1.102 +  assert.equal(stream.read(-1), "",
   1.103 +                   "string.read with negative byte length should return " +
   1.104 +                   "empty string");
   1.105 +  stream.close();
   1.106 +
   1.107 +  file.remove(fname);
   1.108 +};
   1.109 +
   1.110 +exports.testWriteAsync = function (assert, done) {
   1.111 +  let fname = dataFileFilename();
   1.112 +  let str = "exports.testWriteAsync data!";
   1.113 +  let stream = file.open(fname, "w");
   1.114 +  assert.ok(!stream.closed, "stream.closed after open should be false");
   1.115 +
   1.116 +  // Write.
   1.117 +  stream.writeAsync(str, function (err) {
   1.118 +    assert.equal(this, stream, "|this| should be the stream object");
   1.119 +    assert.equal(err, undefined,
   1.120 +                     "stream.writeAsync should not cause error");
   1.121 +    assert.ok(stream.closed, "stream.closed after write should be true");
   1.122 +    assert.throws(function () stream.close(),
   1.123 +                      STREAM_CLOSED_ERROR,
   1.124 +                      "stream.close after already closed should raise error");
   1.125 +    assert.throws(function () stream.writeAsync("This shouldn't work!"),
   1.126 +                      STREAM_CLOSED_ERROR,
   1.127 +                      "stream.writeAsync after close should raise error");
   1.128 +
   1.129 +    // Read.
   1.130 +    stream = file.open(fname, "r");
   1.131 +    assert.ok(!stream.closed, "stream.closed after open should be false");
   1.132 +    let readStr = stream.read();
   1.133 +    assert.equal(readStr, str,
   1.134 +                     "string.read should yield string written");
   1.135 +    stream.close();
   1.136 +    file.remove(fname);
   1.137 +    done();
   1.138 +  });
   1.139 +};
   1.140 +
   1.141 +exports.testUnload = function (assert) {
   1.142 +  let loader = Loader(module);
   1.143 +  let file = loader.require("sdk/io/file");
   1.144 +
   1.145 +  let filename = dataFileFilename("temp");
   1.146 +  let stream = file.open(filename, "w");
   1.147 +
   1.148 +  loader.unload();
   1.149 +  assert.ok(stream.closed, "stream should be closed after module unload");
   1.150 +};
   1.151 +
   1.152 +// Returns the name of a file that should be used to test writing and reading.
   1.153 +function dataFileFilename() {
   1.154 +  return file.join(pathFor("ProfD"), "test-text-streams-data");
   1.155 +}
   1.156 +
   1.157 +require('sdk/test').run(exports);

mercurial