readlines.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. const lineByLine = require('../readlines.js');
  3. const path = require('path');
  4. const test = require('tape');
  5. test('should get all lines', (t) => {
  6. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/twoLineFile.txt'));
  7. t.equals(liner.next().toString('ascii'), 'hello', 'line 0: hello');
  8. t.equals(liner.next().toString('ascii'), 'hello2', 'line 1: hello2');
  9. t.equals(liner.next(), false, 'line 3: false');
  10. t.equals(liner.next(), false, 'line 4: false');
  11. t.equals(liner.fd, null, 'fd null');
  12. t.end();
  13. });
  14. test('should get all lines even if the file doesnt end with new line', (t) => {
  15. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/badEndFile.txt'));
  16. t.equals(liner.next().toString('ascii'), 'google.com', 'line 0: google.com');
  17. t.equals(liner.next().toString('ascii'), 'yahoo.com', 'line 1: yahoo.com');
  18. t.equals(liner.next(), false, 'line 3: false');
  19. t.equals(liner.fd, null, 'fd is null');
  20. t.end();
  21. });
  22. test('should get all lines if there is no new lines', (t) => {
  23. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/noNewLinesFile.txt'));
  24. t.equals(liner.next().toString('ascii'), 'no new line', 'line 0: no new line');
  25. t.equals(liner.next(), false, 'line 1: false');
  26. t.equals(liner.fd, null, 'fd is null');
  27. t.end();
  28. });
  29. test('should handle empty files', (t) => {
  30. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/emptyFile.txt'));
  31. t.equals(liner.next(), false, 'line 0: false');
  32. t.equals(liner.fd, null, 'line 0: false');
  33. t.end();
  34. });
  35. test('should read right between two chunks', (t) => {
  36. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/normalFile.txt'), {
  37. readChunk: 16
  38. });
  39. t.equals(liner.next().toString('ascii'), 'google.com', 'line 0: google.com');
  40. t.equals(liner.next().toString('ascii'), 'yahoo.com', 'line 1: yahoo.com');
  41. t.equals(liner.next().toString('ascii'), 'yandex.ru', 'line 2: yandex.ru');
  42. t.equals(liner.next(), false, 'line 3: false');
  43. t.equals(liner.fd, null, 'fs is null');
  44. t.end();
  45. });
  46. test('should read empty lines', (t) => {
  47. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/withEmptyLines.txt'));
  48. t.equals(liner.next().toString('ascii'), 'hello', 'line 0: hello');
  49. t.equals(liner.next().toString('ascii'), 'hello4', 'line 1: hello4');
  50. t.equals(liner.next().toString('ascii'), '', 'line 2: ');
  51. t.equals(liner.next().toString('ascii'), 'hello2', 'line 3: hello2');
  52. t.equals(liner.next().toString('ascii'), 'hello3', 'line 4: hello3');
  53. t.equals(liner.next(), false, 'line 5: false');
  54. t.equals(liner.fd, null, 'fs is null');
  55. t.end();
  56. });
  57. test('should reset and start from the beggining', (t) => {
  58. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/normalFile.txt'), {
  59. readChunk: 16
  60. });
  61. t.equals(liner.next().toString('ascii'), 'google.com', 'line 0: google.com');
  62. t.equals(liner.next().toString('ascii'), 'yahoo.com', 'line 1: yahoo.com');
  63. liner.reset()
  64. t.equals(liner.next().toString('ascii'), 'google.com', 'line 0: google.com');
  65. t.equals(liner.next().toString('ascii'), 'yahoo.com', 'line 1: yahoo.com');
  66. t.equals(liner.next().toString('ascii'), 'yandex.ru', 'line 2: yandex.ru');
  67. t.equals(liner.next(), false, 'line 3: false');
  68. t.equals(liner.fd, null, 'fd is null');
  69. t.end();
  70. });
  71. test('should read big lines', (t) => {
  72. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/bigLines.json'));
  73. t.ok(JSON.parse(liner.next()), 'line 0: valid JSON');
  74. t.ok(JSON.parse(liner.next()), 'line 1: valid JSON');
  75. t.ok(JSON.parse(liner.next()), 'line 2: valid JSON');
  76. t.equals(liner.next(), false, 'line 3: false');
  77. t.equals(liner.fd, null, 'fd is null');
  78. t.end();
  79. });
  80. test('Non-Latin Char JSON', (t) => {
  81. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/eiffel.geojson'));
  82. t.ok(JSON.parse(liner.next().toString()), 'line 0: valid JSON');
  83. t.equals(liner.fd, null, 'fd is null');
  84. t.end();
  85. });
  86. test('Manually Close', (t) => {
  87. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/normalFile.txt'));
  88. t.equals(liner.next().toString(), 'google.com', 'line 0: google.com');
  89. liner.close();
  90. t.equals(liner.fd, null, 'fd is null');
  91. t.equals(liner.next(), false, 'line after close: false');
  92. t.end();
  93. });
  94. test('should correctly processes NULL character in lines', (t) => {
  95. const liner = new lineByLine(path.resolve(__dirname, 'fixtures/withNULL.txt'));
  96. t.equals(liner.next().toString(), 'line without null', 'line 0: line without null');
  97. t.equals(liner.next().toString(), 'line wi'+String.fromCharCode(0)+'th null', 'line 1: line with null');
  98. t.equals(liner.next().toString(), 'another line without null', 'line 2: another line without null');
  99. t.equals(liner.fd, null, 'fd is null');
  100. t.end();
  101. })