Introduce InputLine and the ability to acquire inputs from a TaxReturn.
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 18 Feb 2020 05:03:18 +0000 (00:03 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 18 Feb 2020 05:03:18 +0000 (00:03 -0500)
src/Line.test.ts
src/Line.ts
src/TaxReturn.test.ts
src/TaxReturn.ts

index 4dc6f803b07713da7d74da2610b69da35e4ac4e0..e57888e5c759c79a68a5693f367b5541aede09d8 100644 (file)
@@ -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<number>('y', 'Form 1', '7a');
   expect(() => l3.value(tr)).toThrow(NotFoundError);
 });
+
+test('input line', () => {
+  const tr = new TaxReturn(2019, { 'key': 'value' });
+
+  const l1 = new InputLine<string>('1', 'key');
+  expect(l1.value(tr)).toBe('value');
+
+  const l2 = new InputLine<string>('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<number>('2c', (tr: TaxReturn, l: Line<number>): 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<number>('32', 'Z-2', '2c');
+  expect(l.value(tr)).toBe(20);
+});
index e395d0bbf6dac79eb4fc11fff8a5987e87b303d2..92b601362d30c69f48ba167b7a15c6ee027a288b 100644 (file)
@@ -49,3 +49,16 @@ export class ReferenceLine<T> extends Line<T> {
     return tr.getForm(this._form).getLine(this._line).value(tr);
   }
 };
+
+export class InputLine<T> extends Line<T> {
+  private _input: string;
+
+  constructor(id: string, input: string, description?: string) {
+    super(id, description);
+    this._input = input;
+  }
+
+  value(tr: TaxReturn): T {
+    return tr.getInput<T>(this._input);
+  }
+};
index 74c82f427f74b60401947e3ddb11c5913dabd26f..fa5b8cecdbafc35236bae0ecda702de3dcd4d9ee 100644 (file)
@@ -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);
+});
index 1614a1c23e826704866939a630cb6e9c039e0292..82e97fa46bc7be16986cf0f0c75df639067ef32a 100644 (file)
@@ -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<T>(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');