All files reader.test.ts

0% Statements 0/35
100% Branches 0/0
0% Functions 0/12
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                                                                         
import * as assert from "assert"
import * as fs from "fs"
import * as util from "util"
import { Reader } from "../src/reader"
 
describe("reader", () => {
  const content = Buffer.from("foo bar baz qux garply waldo fred plugh xyzzy thud mumble frobnozzle")
  const reader = new Reader(content)
 
  it("should have the offset at zero", () => {
    assert.equal(reader.offset, 0)
  })
 
  it("should read foo bar baz qux", () => {
    assert.deepEqual(reader.read(3), Buffer.from("foo"))
    assert.deepEqual(reader.read(4), Buffer.from(" bar"))
    assert.deepEqual(reader.read(4), Buffer.from(" baz"))
    assert.deepEqual(reader.read(4), Buffer.from(" qux"))
  })
 
  it("should peek", () => {
    assert.deepEqual(reader.peek(7), Buffer.from(" garply"))
    assert.deepEqual(reader.peek(7), Buffer.from(" garply"))
  })
 
  it("should jump to foo", () => {
    assert.deepEqual(reader.jump(0).read(3), Buffer.from("foo"))
    assert.deepEqual(reader.jump(0).read(3), Buffer.from("foo"))
  })
 
  it("should clamp to waldo", () => {
    reader.jump(23).clamp()
    assert.deepEqual(reader.jump(0).read(5), Buffer.from("waldo"))
    assert.deepEqual(reader.jump(0).read(5), Buffer.from("waldo"))
  })
 
  it("should unclamp", () => {
    reader.unclamp()
    assert.deepEqual(reader.jump(0).read(3), Buffer.from("foo"))
  })
 
  it("should decode", () => {
    assert.equal(reader.read(10, "utf8"), " bar baz q")
  })
 
  it("should skip", () => {
    reader.skip(10)
    assert.equal(reader.read(10, "utf8"), "waldo fred")
  })
 
  it("should error if reading past end", () => {
    assert.throws(() => reader.jump(1000).read(5), /EOF/)
    assert.equal(reader.offset, 1000)
  })
 
  it("should work with file buffer", async () => {
    const newReader = new Reader(await util.promisify(fs.readFile)(__filename))
    assert.equal(newReader.read(6, "utf8"), "import")
  })
})