-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';
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);
+});
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);
+ }
+};
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');