From 84bb65dbe02e68dcb7820ec6f79d69df2e3ce811 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 18 Feb 2020 00:03:18 -0500 Subject: [PATCH] Introduce InputLine and the ability to acquire inputs from a TaxReturn. --- src/Line.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- src/Line.ts | 13 +++++++++++++ src/TaxReturn.test.ts | 6 ++++++ src/TaxReturn.ts | 12 +++++++++++- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Line.test.ts b/src/Line.test.ts index 4dc6f80..e57888e 100644 --- a/src/Line.test.ts +++ b/src/Line.test.ts @@ -1,4 +1,4 @@ -import { Line, ReferenceLine, ComputedLine } from './Line'; +import { Line, InputLine, ReferenceLine, ComputedLine } from './Line'; import Form from './Form'; import TaxReturn from './TaxReturn'; import { NotFoundError } from './Errors'; @@ -51,3 +51,42 @@ test('reference line', () => { const l3 = new ReferenceLine('y', 'Form 1', '7a'); expect(() => l3.value(tr)).toThrow(NotFoundError); }); + +test('input line', () => { + const tr = new TaxReturn(2019, { 'key': 'value' }); + + const l1 = new InputLine('1', 'key'); + expect(l1.value(tr)).toBe('value'); + + const l2 = new InputLine('2', 'key2'); + expect(() => l2.value(tr)).toThrow(NotFoundError); +}); + +test('line stack', () => { + class FormZ extends Form { + get name() { return 'Z'; } + + protected getLines() { + return [ new InputLine('3', 'input') ]; + } + }; + + class FormZ2 extends Form { + get name() { return 'Z-2'; } + + protected getLines() { + return [ + new ComputedLine('2c', (tr: TaxReturn, l: Line): any => { + return tr.getForm('Z').getLine('3').value(tr) * 0.2; + }) + ]; + } + }; + + const tr = new TaxReturn(2019, { 'input': 100 }); + tr.addForm(new FormZ()); + tr.addForm(new FormZ2()); + + const l = new ReferenceLine('32', 'Z-2', '2c'); + expect(l.value(tr)).toBe(20); +}); diff --git a/src/Line.ts b/src/Line.ts index e395d0b..92b6013 100644 --- a/src/Line.ts +++ b/src/Line.ts @@ -49,3 +49,16 @@ export class ReferenceLine extends Line { return tr.getForm(this._form).getLine(this._line).value(tr); } }; + +export class InputLine extends Line { + private _input: string; + + constructor(id: string, input: string, description?: string) { + super(id, description); + this._input = input; + } + + value(tr: TaxReturn): T { + return tr.getInput(this._input); + } +}; diff --git a/src/TaxReturn.test.ts b/src/TaxReturn.test.ts index 74c82f4..fa5b8ce 100644 --- a/src/TaxReturn.test.ts +++ b/src/TaxReturn.test.ts @@ -96,3 +96,9 @@ 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 1614a1c..82e97fa 100644 --- a/src/TaxReturn.ts +++ b/src/TaxReturn.ts @@ -4,17 +4,27 @@ 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) { + constructor(year: number, input?: object) { 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.43.5