UNPKG

2.07 kBJavaScriptView Raw
1const rll = require("../index.js");
2const chai = require("chai");
3const chaiAsPromised = require("chai-as-promised");
4
5chai.use(chaiAsPromised);
6
7const expect = chai.expect;
8const assert = chai.assert;
9
10
11describe("#equals", function() {
12 it("return all lines when asked for more than the file has", function() {
13 return rll.read("test/numbered", 15)
14 .then((lines) => {
15 var length = lines.split(/\n|\r/).length;
16 expect(length).to.equal(10 + 1);
17 })
18 });
19
20 it("return last line when asked for 1", function() {
21 return rll.read("test/numbered", 1)
22 .then((lines) => {
23 var length = lines.split(/\n|\r/).length;
24 var trimmedStringLength = lines.trim().length;
25 expect(length).to.equal(1 + 1);
26 expect(trimmedStringLength).to.equal(3);
27 })
28 });
29
30 it("return last 2 lines when asked for 2", function() {
31 return rll.read("test/numbered", 2)
32 .then((lines) => {
33 var length = lines.split(/\n|\r/).length;
34 expect(length).to.equal(2 + 1);
35 })
36 });
37
38 it("return last 2 lines when asked for 2 and missing trailing new line", function() {
39 return rll.read("test/numbered_no_trailing_new_line", 2)
40 .then((lines) => {
41 var length = lines.split(/\n|\r/).length;
42 expect(length).to.equal(2);
43 })
44 });
45
46 it("Expect and error to be thrown if the file does not exist", function() {
47 return assert.isRejected(rll.read("test/non_existant_file_name", 1), "file does not exist");
48 });
49
50 it("should the new line characters used by the file, when using non standard new line characters", function() {
51 return rll.read("test/non_standard_new_lines", 30)
52 .then((lines) => {
53 var length = lines.split(/\n\r/).length;
54 expect(length).to.equal(3 + 1);
55 })
56 });
57
58});
59
60var print_results = function(title, data) {
61 console.log(JSON.stringify({
62 title: title,
63 encoded_data: JSON.stringify(data),
64 data: data,
65 lines: data.split(/\n|\r/).length,
66 split: data.split(/\n|\r/),
67 trim: data.trim(),
68 length: data.length,
69 trimmed_length: data.trim().length
70 }, null, 2));
71}
72// var test = this.test;
73// print_results(test.title, lines);