UNPKG

3.16 kBJavaScriptView Raw
1var fs = require('fs');
2var readLine = require('../readline.js');
3var test = require("tap").test;
4
5test("test reading lines",function(t){
6 console.error("reading large file line by line asserts may take a while");
7 var rl = readLine('./fixtures/afile.txt');
8 rl.on("line", function (line,linecount){
9 t.ok(null !== line && undefined !== line);
10 });
11 rl.on("end",function (){
12 t.end();
13 });
14
15});
16
17test("numbers", function (t){
18 var rl = readLine('./fixtures/nmbr.txt');
19 var answer = 28;
20 var i=0;
21 rl.on("line", function (line){
22 var num = Number(line);
23 console.error(num);
24 i+=num;
25
26 });
27 rl.on("end", function (){
28 console.error(i,answer);
29 t.ok(answer === i, "answered");
30 t.end();
31 });
32});
33
34
35test("errors", function (t){
36 var rl = readLine("./Idontexist");
37 rl.on('error', function (e){
38 t.ok(e);
39 t.end();
40 });
41 rl.on('end', function (){
42 t.end();
43 });
44 rl.on('close', function(){
45 t.end();
46 });
47});
48
49
50test("line count", function(t){
51 var rl = readLine('./fixtures/nmbr.txt');
52 var expect = 7;
53 var actual = 0;
54 rl.on("line", function (line, ln){
55 console.log("line",line,ln);
56 actual=ln;
57 });
58 rl.on("end", function (){
59 t.ok(actual === expect,"line count is correct");
60 t.end();
61 });
62});
63
64test("byte count after first line", function(t){
65 var rl = readLine('./fixtures/nmbr.txt');
66 var actual = 0;
67 var expect;
68 rl.on("line", function (line, ln, byteCount){
69 if (expect === undefined) {
70 expect = line.length;
71 console.log("byte count",byteCount);
72 actual=byteCount;
73
74 t.ok(actual === expect,"byte count is correct");
75 t.end();
76 }
77 });
78});
79
80test("byte count", function(t){
81 var rl = readLine('./fixtures/nmbr.txt');
82 var expect = fs.statSync('./fixtures/nmbr.txt').size;
83 var actual = 0;
84 rl.on("line", function (line, ln, byteCount){
85 console.log("byte count",byteCount);
86 actual=byteCount;
87 });
88 rl.on("end", function (){
89 t.ok(actual === expect,"byte count is correct");
90 t.end();
91 });
92});
93
94test("processing error passed on", function(t){
95 var rl = readLine('./fixtures/nmbr.txt');
96 var lastError;
97 var lineCalls = 0;
98
99 rl.on("line", function (line, ln, byteCount){
100 lineCalls++;
101 if (ln === 7) {
102 throw new Error('fake error');
103 }
104 });
105 rl.on("error", function (err){
106 if (!lastError) {
107 lastError = err;
108 }
109 });
110
111 rl.on("end", function (){
112 t.ok(lastError.message === 'fake error','error is passed on');
113 t.ok(lineCalls === 7, 'line count ok');
114 t.end();
115 });
116});
117
118test("test ascii file reading",function(t){
119 var iconv = require('iconv-lite');
120 var testFileValidationKeywords = {
121 1: 'папка',
122 3: 'телефон',
123 11: 'электричество',
124 14: 'дерево'
125 };
126
127 var rl = readLine('./fixtures/file-in-win1251.txt', {
128 retainBuffer: true
129 });
130 rl.on("line", function (data,linecount){
131 var line = iconv.decode(data, 'win1251');
132 t.ok(!testFileValidationKeywords[linecount] || line.indexOf(testFileValidationKeywords[linecount]) > -1);
133 });
134 rl.on("end",function (){
135 t.end();
136 });
137
138});
\No newline at end of file