1 import { ComputedLine, Line } from './Line';
2 import TaxReturn from './TaxReturn';
3 import Form from './Form';
4 import { InconsistencyError, NotFoundError } from './Errors';
6 test('add and get line', () => {
7 const l = new ComputedLine<number>(() => 42);
9 class TestForm extends Form<TestForm['_lines']> {
10 readonly name = 'Test Form';
12 protected readonly _lines = { '1': l };
15 const f = new TestForm();
16 expect(f.getLine('1')).toBe(l);
19 test('get non-existent line', () => {
20 class TestForm extends Form<TestForm['_lines']> {
21 readonly name = 'Test';
22 protected readonly _lines = {};
25 const f = new TestForm();
26 const fAsAny: Form<any> = f;
27 expect(() => fAsAny.getLine('line')).toThrow(NotFoundError);
30 //expect(() => f.getLine('line')).toThrow(NotFoundError);
38 class TestForm extends Form<any, TestInput> {
39 readonly name = '1040';
41 protected readonly _lines = null;
44 const f = new TestForm({ filingStatus: 'S', money: 100.0 });
45 expect(f.getInput('filingStatus')).toBe('S');
48 test('get value', () => {
49 class TestForm extends Form<TestForm['_lines']> {
50 readonly name = 'Form';
52 protected readonly _lines = {
53 line: new ComputedLine<number>(() => 42),
57 const f = new TestForm();
58 const tr = new TaxReturn(2019);
59 expect(f.getValue(tr, 'line')).toBe(42);
62 //let s: string = f.getValue(tr, 'line');
64 const fAsAny: Form<any> = f;
65 expect(() => fAsAny.getValue(tr, 'other')).toThrow(NotFoundError);
67 //expect(() => f.getValue(tr, 'other')).toThrow(NotFoundError);
71 abstract class Form2<L extends { [key: string]: Line<any> } , I> {
72 abstract readonly name: string;
74 protected abstract readonly _lines: L;
75 protected abstract readonly _input?: I;
77 getLine<K extends keyof L>(key: K): L[K] {
78 return this._lines[key];
81 getInput<K extends keyof I>(key: K): I[K] {
82 return this._input[key];
85 getValue<T, K extends keyof L>(tr: TaxReturn, key: K): T {
86 const line = this.getLine(key);
87 return line.value(tr);
91 class FormG extends Form2<FormG['_lines'], FormG['_input']> {
94 protected readonly _lines = {
95 x: new ComputedLine('moo', () => 42),
96 z: new ComputedLine('moo', () => 36),
98 protected readonly _input = null;
102 getLineImpl<T, K extends keyof T>(lines: T, k: K): T[K] {
106 allLines(): FormG['_lines'] {
110 LINE = k => this.getLineImpl(this._lines, k);
112 testLine<K extends keyof ReturnType<FormG['allLines']>>(k: K): any {
113 return this.getLineImpl(this._lines, k);
117 test('testing', () => {
118 const g = new FormG();
119 let v = g.testLine('x'); //g.getLineImpl(g._lines, 'x');
120 let v2 = g.getValue(null, 'z');
121 throw new Error(`v = ${v2}`);