files.spec.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* eslint no-unused-vars:off */
  2. var fs = require('fs')
  3. var path = require('path')
  4. var stream = require('readable-stream')
  5. var expect = require('chai').expect
  6. var parse = require('../index')
  7. describe('File parsing', function () {
  8. it('should parse the file by path', function (done) {
  9. parse.file(path.resolve(__dirname, 'fixtures/sample.js'), function (err, parsed) {
  10. if (err) { done(err) }
  11. expect(parsed)
  12. .to.be.an('array')
  13. expect(parsed.length)
  14. .to.eq(4)
  15. expect(parsed[0].description)
  16. .to.eq('File description')
  17. expect(parsed[1].tags[0].tag)
  18. .to.eq('class')
  19. expect(parsed[2].tags[0].tag)
  20. .to.eq('property')
  21. expect(parsed[3].tags[0].tag)
  22. .to.eq('method')
  23. done()
  24. })
  25. })
  26. it('should path the error if file dows not exist', function (done) {
  27. parse.file('does/not/exists', function (err, parsed) {
  28. expect(err)
  29. .to.be.instanceof(Error)
  30. done()
  31. })
  32. })
  33. it('should return `Transform` stream', function (done) {
  34. var count = 0
  35. var readable = fs.createReadStream(path.resolve(__dirname, 'fixtures/sample.js'), {encoding: 'utf8'})
  36. var writable = new stream.Writable({objectMode: true})
  37. writable._write = function (data, encoding, done) {
  38. count++
  39. done()
  40. }
  41. readable
  42. .on('error', done)
  43. .pipe(parse.stream())
  44. .on('error', done)
  45. .pipe(writable)
  46. .on('error', done)
  47. .on('finish', function () {
  48. expect(count)
  49. .to.eq(4)
  50. done()
  51. })
  52. })
  53. })