import { assert, describe, expect, it } from 'vitest' import { LineData, parseLine } from './parser' describe(`LineData`, () => { it(`is an object`, () => { const lineData = LineData() expect(lineData).toBeTypeOf(`object`) }) it(`has two properties`, () => { const lineData = LineData() expect(Object.keys(lineData).length).to.equal(2) }) it(`'type' is an integer initialized to zero`, () => { const lineData = LineData() expect(lineData.level).to.equal(0) }) it(`'level' is an integer initialized to zero`, () => { const lineData = LineData() expect(lineData.type).to.equal(0) }) }) describe(`parseLine`, () => { it(`Requres 'line' to be a string`, () => { const lineData = LineData() expect(() => parseLine(0, lineData)).toThrowError(`'line' must be a string`) expect(() => parseLine([], lineData)).toThrowError(`'line' must be a string`) expect(() => parseLine({}, lineData)).toThrowError(`'line' must be a string`) expect(() => parseLine(null, lineData)).toThrowError(`'line' must be a string`) expect(() => parseLine(true, lineData)).toThrowError(`'line' must be a string`) expect(() => parseLine(() => {}, lineData)).toThrowError(`'line' must be a string`) }) it(`Requres 'lineData' to be an object with a numeric level and type property`, () => { const lineData = LineData() expect(() => parseLine(``, 0)).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, [])).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, {})).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, null)).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, true)).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, () => {})).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, { level: '', type: 0 })).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) expect(() => parseLine(``, { level: 0, type: null })).toThrowError(`'lineData' must be an object with 'type' and 'level' integer properties`) }) it(`Requres 'indent' to be a single-character string`, () => { const lineData = LineData() expect(() => parseLine(``, lineData, 0)).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, [])).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, {})).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, null)).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, true)).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, () => {})).toThrowError(`'indent' must be a single-character string`) expect(() => parseLine(``, lineData, ` `)).toThrowError(`'indent' must be a single-character string`) }) it(`Outputs { type: 0, level: 0} for a blank line at indent level 0`, () => { const line = `` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(0) expect(lineData.level).to.equal(0) }) it(`Outputs { type: 1, level: 1} for line with a single space at indent level 1`, () => { const line = ` ` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(1) }) it(`Outputs { type: 1, level: 2} for line with two spaces`, () => { const line = ` ` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(2) }) it(`Outputs { type: 1, level: 0} for a normal line at indent level 0`, () => { const line = `line 1` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(0) }) it(`Outputs { type: 1, level: 1} for a normal line at indent level 1`, () => { const line = ` line 1` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(1) }) it(`Outputs { type: 1, level: 1} for a normal line at indent level 1`, () => { const line = ` line 1` const lineData = LineData() parseLine(line, lineData) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(2) }) it(`Outputs { type: 1, level: 1} for a normal line at indent level 1 indented with tabs`, () => { const line = `\tline 1` const lineData = LineData() parseLine(line, lineData, `\t`) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(1) }) it(`Outputs { type: 1, level: 2} for a normal line at indent level 1 indented with tabs`, () => { const line = `\t\tline 1` const lineData = LineData() parseLine(line, lineData, `\t`) expect(lineData.type).to.equal(1) expect(lineData.level).to.equal(2) }) it(`Nests a normal line under a preceding normal line`, () => { const lines = [ 'line 1', ' line 2' ] const lineData = LineData() const results = lines.map(line => { parseLine(line, lineData) return {...lineData} }) expect(results).to.deep.equal([ { type: 1, level: 0 }, { type: 1, level: 1 } ]) }) it(`Nests multiple normal line under a preceding normal line`, () => { const lines = [ 'line 1', ' line 2', ' line 3', ' line 4', ] const lineData = LineData() const results = lines.map(line => { parseLine(line, lineData) return {...lineData} }) expect(results).to.deep.equal([ { type: 1, level: 0 }, { type: 1, level: 1 }, { type: 1, level: 1 }, { type: 1, level: 1 } ]) }) it(`Nests an empty line under a preceding normal line`, () => { const lines = [ 'line 1', '' ] const lineData = LineData() const results = lines.map(line => { parseLine(line, lineData) return {...lineData} }) expect(results).to.deep.equal([ { type: 1, level: 0 }, { type: 0, level: 1 } ]) }) it(`Nests multiple empty lines under a preceding normal line`, () => { const lines = [ 'line 1', '', '', '', ] const lineData = LineData() const results = lines.map(line => { parseLine(line, lineData) return {...lineData} }) expect(results).to.deep.equal([ { type: 1, level: 0 }, { type: 0, level: 1 }, { type: 0, level: 1 }, { type: 0, level: 1 } ]) }) })