UNPKG

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