PageRenderTime 31ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/split2/test.js

https://gitlab.com/JTAlv/versioning
JavaScript | 392 lines | 288 code | 104 blank | 0 comment | 2 complexity | 304c75ad6b1663d2dba4ab868637df39 MD5 | raw file
  1. 'use strict'
  2. var test = require('tape')
  3. var split = require('./')
  4. var callback = require('callback-stream')
  5. var Buffer = require('safe-buffer').Buffer
  6. var strcb = callback.bind(null, { decodeStrings: false })
  7. var objcb = callback.bind(null, { objectMode: true })
  8. test('split two lines on end', function (t) {
  9. t.plan(2)
  10. var input = split()
  11. input.pipe(strcb(function (err, list) {
  12. t.error(err)
  13. t.deepEqual(list, ['hello', 'world'])
  14. }))
  15. input.end('hello\nworld')
  16. })
  17. test('split two lines on two writes', function (t) {
  18. t.plan(2)
  19. var input = split()
  20. input.pipe(strcb(function (err, list) {
  21. t.error(err)
  22. t.deepEqual(list, ['hello', 'world'])
  23. }))
  24. input.write('hello')
  25. input.write('\nworld')
  26. input.end()
  27. })
  28. test('split four lines on three writes', function (t) {
  29. t.plan(2)
  30. var input = split()
  31. input.pipe(strcb(function (err, list) {
  32. t.error(err)
  33. t.deepEqual(list, ['hello', 'world', 'bye', 'world'])
  34. }))
  35. input.write('hello\nwor')
  36. input.write('ld\nbye\nwo')
  37. input.write('rld')
  38. input.end()
  39. })
  40. test('accumulate multiple writes', function (t) {
  41. t.plan(2)
  42. var input = split()
  43. input.pipe(strcb(function (err, list) {
  44. t.error(err)
  45. t.deepEqual(list, ['helloworld'])
  46. }))
  47. input.write('hello')
  48. input.write('world')
  49. input.end()
  50. })
  51. test('split using a custom string matcher', function (t) {
  52. t.plan(2)
  53. var input = split('~')
  54. input.pipe(strcb(function (err, list) {
  55. t.error(err)
  56. t.deepEqual(list, ['hello', 'world'])
  57. }))
  58. input.end('hello~world')
  59. })
  60. test('split using a custom regexp matcher', function (t) {
  61. t.plan(2)
  62. var input = split(/~/)
  63. input.pipe(strcb(function (err, list) {
  64. t.error(err)
  65. t.deepEqual(list, ['hello', 'world'])
  66. }))
  67. input.end('hello~world')
  68. })
  69. test('support an option argument', function (t) {
  70. t.plan(2)
  71. var input = split({ highWaterMark: 2 })
  72. input.pipe(strcb(function (err, list) {
  73. t.error(err)
  74. t.deepEqual(list, ['hello', 'world'])
  75. }))
  76. input.end('hello\nworld')
  77. })
  78. test('support a mapper function', function (t) {
  79. t.plan(2)
  80. var a = { a: '42' }
  81. var b = { b: '24' }
  82. var input = split(JSON.parse)
  83. input.pipe(objcb(function (err, list) {
  84. t.error(err)
  85. t.deepEqual(list, [a, b])
  86. }))
  87. input.write(JSON.stringify(a))
  88. input.write('\n')
  89. input.end(JSON.stringify(b))
  90. })
  91. test('split lines windows-style', function (t) {
  92. t.plan(2)
  93. var input = split()
  94. input.pipe(strcb(function (err, list) {
  95. t.error(err)
  96. t.deepEqual(list, ['hello', 'world'])
  97. }))
  98. input.end('hello\r\nworld')
  99. })
  100. test('splits a buffer', function (t) {
  101. t.plan(2)
  102. var input = split()
  103. input.pipe(strcb(function (err, list) {
  104. t.error(err)
  105. t.deepEqual(list, ['hello', 'world'])
  106. }))
  107. input.end(Buffer.from('hello\nworld'))
  108. })
  109. test('do not end on undefined', function (t) {
  110. t.plan(2)
  111. var input = split(function (line) { })
  112. input.pipe(strcb(function (err, list) {
  113. t.error(err)
  114. t.deepEqual(list, [])
  115. }))
  116. input.end(Buffer.from('hello\nworld'))
  117. })
  118. test('has destroy method', function (t) {
  119. t.plan(1)
  120. var input = split(function (line) { })
  121. input.on('close', function () {
  122. t.ok(true, 'close emitted')
  123. t.end()
  124. })
  125. input.destroy()
  126. })
  127. test('support custom matcher and mapper', function (t) {
  128. t.plan(4)
  129. var a = { a: '42' }
  130. var b = { b: '24' }
  131. var input = split('~', JSON.parse)
  132. t.equal(input.matcher, '~')
  133. t.equal(typeof input.mapper, 'function')
  134. input.pipe(objcb(function (err, list) {
  135. t.notOk(err, 'no errors')
  136. t.deepEqual(list, [a, b])
  137. }))
  138. input.write(JSON.stringify(a))
  139. input.write('~')
  140. input.end(JSON.stringify(b))
  141. })
  142. test('support custom matcher and options', function (t) {
  143. t.plan(6)
  144. var input = split('~', { highWaterMark: 1024 })
  145. t.equal(input.matcher, '~')
  146. t.equal(typeof input.mapper, 'function')
  147. t.equal(input._readableState.highWaterMark, 1024)
  148. t.equal(input._writableState.highWaterMark, 1024)
  149. input.pipe(strcb(function (err, list) {
  150. t.error(err)
  151. t.deepEqual(list, ['hello', 'world'])
  152. }))
  153. input.end('hello~world')
  154. })
  155. test('support mapper and options', function (t) {
  156. t.plan(6)
  157. var a = { a: '42' }
  158. var b = { b: '24' }
  159. var input = split(JSON.parse, { highWaterMark: 1024 })
  160. t.ok(input.matcher instanceof RegExp, 'matcher is RegExp')
  161. t.equal(typeof input.mapper, 'function')
  162. t.equal(input._readableState.highWaterMark, 1024)
  163. t.equal(input._writableState.highWaterMark, 1024)
  164. input.pipe(objcb(function (err, list) {
  165. t.error(err)
  166. t.deepEqual(list, [a, b])
  167. }))
  168. input.write(JSON.stringify(a))
  169. input.write('\n')
  170. input.end(JSON.stringify(b))
  171. })
  172. test('split utf8 chars', function (t) {
  173. t.plan(2)
  174. var input = split()
  175. input.pipe(strcb(function (err, list) {
  176. t.error(err)
  177. t.deepEqual(list, ['烫烫烫', '锟斤拷'])
  178. }))
  179. var buf = Buffer.from('烫烫烫\r\n锟斤拷', 'utf8')
  180. for (var i = 0; i < buf.length; ++i) {
  181. input.write(buf.slice(i, i + 1))
  182. }
  183. input.end()
  184. })
  185. test('split utf8 chars 2by2', function (t) {
  186. t.plan(2)
  187. var input = split()
  188. input.pipe(strcb(function (err, list) {
  189. t.error(err)
  190. t.deepEqual(list, ['烫烫烫', '烫烫烫'])
  191. }))
  192. var str = '烫烫烫\r\n烫烫烫'
  193. var buf = Buffer.from(str, 'utf8')
  194. for (var i = 0; i < buf.length; i += 2) {
  195. input.write(buf.slice(i, i + 2))
  196. }
  197. input.end()
  198. })
  199. test('split lines when the \n comes at the end of a chunk', function (t) {
  200. t.plan(2)
  201. var input = split()
  202. input.pipe(strcb(function (err, list) {
  203. t.error(err)
  204. t.deepEqual(list, ['hello', 'world'])
  205. }))
  206. input.write('hello\n')
  207. input.end('world')
  208. })
  209. test('truncated utf-8 char', function (t) {
  210. t.plan(2)
  211. var input = split()
  212. input.pipe(strcb(function (err, list) {
  213. t.error(err)
  214. t.deepEqual(list, ['烫' + Buffer.from('e7', 'hex').toString()])
  215. }))
  216. var str = '烫烫'
  217. var buf = Buffer.from(str, 'utf8')
  218. input.write(buf.slice(0, 3))
  219. input.end(buf.slice(3, 4))
  220. })
  221. test('maximum buffer limit', function (t) {
  222. t.plan(1)
  223. var input = split({ maxLength: 2 })
  224. input.pipe(strcb(function (err, list) {
  225. t.ok(err)
  226. }))
  227. input.write('hey')
  228. })
  229. test('readable highWaterMark', function (t) {
  230. var input = split()
  231. t.equal(input._readableState.highWaterMark, 16)
  232. t.end()
  233. })
  234. test('maxLength < chunk size', function (t) {
  235. t.plan(2)
  236. var input = split({ maxLength: 2 })
  237. input.pipe(strcb(function (err, list) {
  238. t.error(err)
  239. t.deepEqual(list, ['a', 'b'])
  240. }))
  241. input.end('a\nb')
  242. })
  243. test('maximum buffer limit w/skip', function (t) {
  244. t.plan(2)
  245. var input = split({ maxLength: 2, skipOverflow: true })
  246. input.pipe(strcb(function (err, list) {
  247. t.error(err)
  248. t.deepEqual(list, ['a', 'b', 'c'])
  249. }))
  250. input.write('a\n123')
  251. input.write('456')
  252. input.write('789\nb\nc')
  253. input.end()
  254. })
  255. test("don't modify the options object", function (t) {
  256. t.plan(2)
  257. var options = {}
  258. var input = split(options)
  259. input.pipe(strcb(function (err, list) {
  260. t.error(err)
  261. t.same(options, {})
  262. }))
  263. input.end()
  264. })
  265. test('mapper throws flush', function (t) {
  266. t.plan(1)
  267. var error = new Error()
  268. var input = split(function () {
  269. throw error
  270. })
  271. input.on('error', (err, list) => {
  272. t.same(err, error)
  273. })
  274. input.end('hello')
  275. })
  276. test('mapper throws on transform', function (t) {
  277. t.plan(2)
  278. var error = new Error()
  279. var input = split(function (l) {
  280. throw error
  281. })
  282. input.on('error', (err) => {
  283. t.same(err, error)
  284. })
  285. input.write('a')
  286. input.write('\n')
  287. input.end('b')
  288. })