1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import { Line, AccumulatorLine, InputLine, ReferenceLine, ComputedLine } from './Line';
7 import Form, { FormClass } from './Form';
8 import TaxReturn from './TaxReturn';
9 import { NotFoundError } from './Errors';
11 class TestTaxReturn extends TaxReturn {
12 readonly constants = undefined;
13 get year() { return 2019; }
14 get includeJointPersonForms() { return false; }
17 class ConstantLine<T> extends Line<T> {
21 super(`Constant ${k}`);
25 value(tr: TaxReturn): T {
30 test('computed line', () => {
31 const tr = new TestTaxReturn();
32 const l = new ComputedLine<number>(
33 (taxReturn: TaxReturn): number => {
34 expect(taxReturn).toBe(tr);
38 expect(l.value(tr)).toBe(42);
39 expect(l.description).toBe('Computed Line A');
42 test('reference line', () => {
43 class TestForm extends Form {
44 readonly name = 'Form 1';
46 '6b': new ConstantLine(12.34),
47 's': new ConstantLine('abc'),
51 const tr = new TestTaxReturn();
52 tr.addForm(new TestForm());
54 const l1 = new ReferenceLine(TestForm, '6b');
55 let n: number = l1.value(tr);
56 expect(n).toBe(12.34);
58 const l2 = new ReferenceLine(TestForm, 's');
59 let s: string = l2.value(tr);
60 expect(s).toBe('abc');
63 //const l3 = new ReferenceLine(TestForm, '7a');
64 //let n2: string = l1.value(tr);
65 //let s2: number = l2.value(tr);
68 test('self reference line', () => {
69 class OtherForm extends Form {
70 readonly name = 'Form A';
72 '6c': new ConstantLine(55)
75 class TestForm extends Form {
76 readonly name = 'Form 1';
78 'a': new ConstantLine(100.2),
79 'b': new ReferenceLine(OtherForm, '6c'),
80 'c': new ReferenceLine((TestForm as unknown) as FormClass<Form>, 'b'),
81 'd': new ReferenceLine(TestForm as any, 'b'),
85 const tr = new TestTaxReturn();
86 const f = new TestForm();
88 tr.addForm(new OtherForm());
90 expect(f.getValue(tr, 'a')).toBe(100.2);
91 expect(f.getValue(tr, 'b')).toBe(55);
92 expect(f.getValue(tr, 'c')).toBe(55);
93 expect(f.getValue(tr, 'd')).toBe(55);
96 test('input line', () => {
101 class TestForm extends Form<Input> {
102 readonly name = 'F1';
104 '1': new InputLine<Input>('key'),
105 '2': new InputLine<Input>('key2'),
106 '3': new InputLine<Input>('key2', undefined, 'FALLBACK')
109 const tr = new TestTaxReturn();
110 const f = new TestForm({ 'key': 'value' });
113 expect(f.getLine('1').value(tr)).toBe('value');
114 expect(f.getLine('1').id).toBe('1');
116 const l2 = f.getLine('2');
117 expect(() => l2.value(tr)).toThrow(NotFoundError);
119 expect(f.getLine('3').value(tr)).toBe('FALLBACK');
122 test('line stack', () => {
123 class FormZ extends Form<{'input': number}> {
126 '3': new InputLine<any, any>('input')
130 class FormZ2 extends Form {
131 readonly name = 'Z-2';
133 '2c': new ComputedLine<number>((tr: TaxReturn): any => {
134 return tr.getForm(FormZ).getLine('3').value(tr) * 0.2;
139 const tr = new TestTaxReturn();
140 tr.addForm(new FormZ({ 'input': 100 }));
141 tr.addForm(new FormZ2());
143 const l = new ReferenceLine(FormZ2, '2c');
144 expect(l.value(tr)).toBe(20);
147 test('accumulator line', () => {
148 class TestForm extends Form {
149 readonly name = 'Form B';
150 readonly supportsMultipleCopies = true;
152 g: new ConstantLine<number>(100.25)
156 const tr = new TestTaxReturn();
157 tr.addForm(new TestForm());
158 tr.addForm(new TestForm());
159 tr.addForm(new TestForm());
161 const l = new AccumulatorLine(TestForm, 'g');
162 expect(l.value(tr)).toBe(300.75);