Issues (2242)

node_modules/from2/test.js (3 issues)

1
var test = require('tape')
2
var path = require('path')
3
var from = require('./')
4
var fs   = require('fs')
5
6
var tmp = path.resolve(
7
  __dirname, 'tmp.txt'
8
)
9
10
function fromString(string) {
11
  return from(function(size, next) {
12
    if (string.length <= 0) return next(null, null)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
13
    var chunk = string.slice(0, size)
14
    string = string.slice(size)
15
    next(null, chunk)
16
  })
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
17
}
18
19
test('from2', function(t) {
20
  var contents = fs.readFileSync(__filename, 'utf8')
21
  var stream = fromString(contents)
22
23
  stream
24
    .pipe(fs.createWriteStream(tmp))
25
    .on('close', function() {
26
      t.equal(fs.readFileSync(tmp, 'utf8'), contents)
27
      fs.unlinkSync(tmp)
28
      t.end()
29
    })
30
})
31
32
test('old mode', function(t) {
33
  var contents = fs.readFileSync(__filename, 'utf8')
34
  var stream = fromString(contents)
35
  var buffer = ''
36
37
  stream.on('data', function(data) {
38
    buffer += data
39
  }).on('end', function() {
40
    t.equal(buffer, contents)
41
    t.end()
42
  })
43
})
44
45
test('destroy', function(t) {
46
  var stream = from(function(size, next) {
47
    process.nextTick(function() {
48
      next(null, 'no')
49
    })
50
  })
51
52
  stream.on('data', function(data) {
0 ignored issues
show
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
53
    t.ok(false)
54
  }).on('close', function() {
55
    t.ok(true)
56
    t.end()
57
  })
58
59
  stream.destroy()
60
})
61
62
test('arrays', function (t) {
63
  var input = ['a', 'b', 'c']
64
  var stream = from(input)
65
  var output = []
66
  stream.on('data', function (letter) {
67
    output.push(letter.toString())
68
  })
69
  stream.on('end', function () {
70
    t.deepEqual(input, output)
71
    t.end()
72
  })
73
})
74
75
test('obj arrays', function (t) {
76
  var input = [{foo:'a'}, {foo:'b'}, {foo:'c'}]
77
  var stream = from.obj(input)
78
  var output = []
79
  stream.on('data', function (letter) {
80
    output.push(letter)
81
  })
82
  stream.on('end', function () {
83
    t.deepEqual(input, output)
84
    t.end()
85
  })
86
})
87
88
89
test('arrays can emit errors', function (t) {
90
  var input = ['a', 'b', new Error('ooops'), 'c']
91
  var stream = from(input)
92
  var output = []
93
  stream.on('data', function (letter) {
94
    output.push(letter.toString())
95
  })
96
  stream.on('error', function(e){
97
    t.deepEqual(['a', 'b'], output)
98
    t.equal('ooops', e.message)
99
    t.end()
100
  })  
101
  stream.on('end', function () {
102
    t.fail('the stream should have errored')
103
  })
104
})
105
106
test('obj arrays can emit errors', function (t) {
107
  var input = [{foo:'a'}, {foo:'b'}, new Error('ooops'), {foo:'c'}]
108
  var stream = from.obj(input)
109
  var output = []
110
  stream.on('data', function (letter) {
111
    output.push(letter)
112
  })
113
  stream.on('error', function(e){
114
    t.deepEqual([{foo:'a'}, {foo:'b'}], output)
115
    t.equal('ooops', e.message)
116
    t.end()
117
  })
118
  stream.on('end', function () {
119
    t.fail('the stream should have errored')
120
  })
121
})
122
123
124