From de59f6bbbdb002ebec985fd63cccb1b81858a2d0 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 18 Feb 2020 00:18:16 -0500 Subject: [PATCH] Move input from a TaxReturn to a Form. This also associates every Line to a Form. --- src/Form.test.ts | 29 +++++++++++++++++++++++++++++ src/Form.ts | 15 ++++++++++++++- src/Line.test.ts | 22 ++++++++++++++++------ src/Line.ts | 5 ++++- src/TaxReturn.test.ts | 6 ------ src/TaxReturn.ts | 11 +---------- 6 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/Form.test.ts b/src/Form.test.ts index d3f2f6f..795fd85 100644 --- a/src/Form.test.ts +++ b/src/Form.test.ts @@ -51,3 +51,32 @@ test('add duplicate line', () => { expect(() => new TestForm()).toThrow(InconsistencyError); }); + +test('add line to two forms', () => { + const l = new ComputedLine('bad', () => 'bad'); + + class TestForm1 extends Form { + get name(): string { return '1'; } + + protected getLines() { return [ l ]; } + }; + class TestForm2 extends Form { + get name(): string { return '2'; } + + protected getLines() { return [ l ]; } + }; + + const f1 = new TestForm1(); + expect(() => new TestForm2()).toThrow(InconsistencyError); +}); + +test('input', () => { + class TestForm extends Form { + get name() { return '1040'; } + + protected getLines() { return []; } + }; + const f = new TestForm({ 'Filing Status': 'S' }); + expect(f.getInput('Filing Status')).toBe('S'); + expect(() => f.getInput('Unknown')).toThrow(NotFoundError); +}); diff --git a/src/Form.ts b/src/Form.ts index 7b9bf07..e638294 100644 --- a/src/Form.ts +++ b/src/Form.ts @@ -3,10 +3,12 @@ import { InconsistencyError, NotFoundError } from './Errors'; export default abstract class Form { private _lines: Line[] = []; + private _input?: object; abstract get name(): string; - constructor() { + constructor(input?: object) { + this._input = input; this.getLines().map(this.addLine.bind(this)); } @@ -17,9 +19,13 @@ export default abstract class Form { } private addLine(line: Line) { + if (line.form !== undefined) { + throw new InconsistencyError('Line is already in a Form'); + } try { this.getLine(line.id); } catch { + line.form = this; this._lines.push(line); return; } @@ -33,4 +39,11 @@ export default abstract class Form { } return lines[0]; } + + getInput(name: string): T { + if (!(name in this._input)) { + throw new NotFoundError(`No input with key ${name} on form ${this.name}`); + } + return this._input[name] as T; + } }; diff --git a/src/Line.test.ts b/src/Line.test.ts index e57888e..c0f76ef 100644 --- a/src/Line.test.ts +++ b/src/Line.test.ts @@ -53,12 +53,22 @@ test('reference line', () => { }); test('input line', () => { - const tr = new TaxReturn(2019, { 'key': 'value' }); + class TestForm extends Form { + get name() { return 'F1'; } - const l1 = new InputLine('1', 'key'); - expect(l1.value(tr)).toBe('value'); + protected getLines() { + return [ + new InputLine('1', 'key'), + new InputLine('2', 'key2') + ]; + } + }; + const tr = new TaxReturn(2019); + const f = new TestForm({ 'key': 'value' }); - const l2 = new InputLine('2', 'key2'); + expect(f.getLine('1').value(tr)).toBe('value'); + + const l2 = f.getLine('2'); expect(() => l2.value(tr)).toThrow(NotFoundError); }); @@ -83,8 +93,8 @@ test('line stack', () => { } }; - const tr = new TaxReturn(2019, { 'input': 100 }); - tr.addForm(new FormZ()); + const tr = new TaxReturn(2019); + tr.addForm(new FormZ({ 'input': 100 })); tr.addForm(new FormZ2()); const l = new ReferenceLine('32', 'Z-2', '2c'); diff --git a/src/Line.ts b/src/Line.ts index 92b6013..0dd1b32 100644 --- a/src/Line.ts +++ b/src/Line.ts @@ -1,9 +1,12 @@ import TaxReturn from './TaxReturn'; +import Form from './Form'; export abstract class Line { private _id: string; private _description?: string; + form: Form; + constructor(id: string, description?: string) { this._id = id; this._description = description; @@ -59,6 +62,6 @@ export class InputLine extends Line { } value(tr: TaxReturn): T { - return tr.getInput(this._input); + return this.form.getInput(this._input); } }; diff --git a/src/TaxReturn.test.ts b/src/TaxReturn.test.ts index fa5b8ce..74c82f4 100644 --- a/src/TaxReturn.test.ts +++ b/src/TaxReturn.test.ts @@ -96,9 +96,3 @@ test('get non-existent form', () => { expect(() => tr.getForm('form')).toThrow(NotFoundError); expect(tr.getForms('form')).toEqual([]); }); - -test('input', () => { - const tr = new TaxReturn(2019, { 'Filing Status': 'S' }); - expect(tr.getInput('Filing Status')).toBe('S'); - expect(() => tr.getInput('Unknown')).toThrow(NotFoundError); -}); diff --git a/src/TaxReturn.ts b/src/TaxReturn.ts index 82e97fa..18a0bbb 100644 --- a/src/TaxReturn.ts +++ b/src/TaxReturn.ts @@ -4,27 +4,18 @@ import { NotFoundError, InconsistencyError, UnsupportedFeatureError } from './Er export default class TaxReturn { private _year: number; - private _input: object; private _people: Person[] = []; private _forms: Form[] = []; - constructor(year: number, input?: object) { + constructor(year: number) { this._year = year; - this._input = input; } get year(): number { return this._year; } - getInput(name: string): T { - if (!(name in this._input)) { - throw new NotFoundError(`No input with key ${name}`); - } - return this._input[name] as T; - } - addPerson(person: Person) { if (person.relation == Relation.Dependent) { throw new UnsupportedFeatureError('Dependents are not supported'); -- 2.22.5