From bf61af8a83394eb5e6450dd74eb9aa5dcb2ceafe Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 17 Feb 2020 17:17:10 -0500 Subject: [PATCH] Begin defining a TaxReturn and Person model. --- src/Form.ts | 2 ++ src/Person.test.ts | 15 +++++++++++++ src/Person.ts | 35 +++++++++++++++++++++++++++++ src/TaxReturn.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++++ src/TaxReturn.ts | 46 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 src/Form.ts create mode 100644 src/Person.test.ts create mode 100644 src/Person.ts create mode 100644 src/TaxReturn.test.ts create mode 100644 src/TaxReturn.ts diff --git a/src/Form.ts b/src/Form.ts new file mode 100644 index 0000000..1e2cb9a --- /dev/null +++ b/src/Form.ts @@ -0,0 +1,2 @@ +export default class Form { +}; diff --git a/src/Person.test.ts b/src/Person.test.ts new file mode 100644 index 0000000..f6fa6e2 --- /dev/null +++ b/src/Person.test.ts @@ -0,0 +1,15 @@ +import { Person, Relation } from './Person'; + +test('static constructors', () => { + let p = Person.self('Billy Bob'); + expect(p.name).toBe('Billy Bob'); + expect(p.relation).toBe(Relation.Self); + + p = Person.spouse('Sponge Bob'); + expect(p.name).toBe('Sponge Bob'); + expect(p.relation).toBe(Relation.Spouse); + + p = Person.dependent('Ted Bob'); + expect(p.name).toBe('Ted Bob'); + expect(p.relation).toBe(Relation.Dependent); +}); diff --git a/src/Person.ts b/src/Person.ts new file mode 100644 index 0000000..d376fc9 --- /dev/null +++ b/src/Person.ts @@ -0,0 +1,35 @@ +export enum Relation { + Self, + Spouse, + Dependent, +}; + +export class Person { + private _name: string; + private _relation: Relation; + + constructor(name: string, relation: Relation) { + this._name = name; + this._relation = relation; + } + + get name(): string { + return this._name; + } + + get relation(): Relation { + return this._relation; + } + + static self(name: string): Person { + return new Person(name, Relation.Self); + } + + static spouse(name: string): Person { + return new Person(name, Relation.Spouse); + } + + static dependent(name: string): Person { + return new Person(name, Relation.Dependent); + } +}; diff --git a/src/TaxReturn.test.ts b/src/TaxReturn.test.ts new file mode 100644 index 0000000..0c0aab6 --- /dev/null +++ b/src/TaxReturn.test.ts @@ -0,0 +1,51 @@ +import TaxReturn from './TaxReturn'; +import { Person } from './Person'; + +test('constructor', () => { + const tr = new TaxReturn(2019); + expect(tr.year).toBe(2019); +}); + +test('does not support Dependents', () => { + const tr = new TaxReturn(2019); + const p = Person.dependent('Baby'); + expect(() => tr.addPerson(p)).toThrow('Dependents are not supported'); +}); + +test('add more than one Self', () => { + const tr = new TaxReturn(2019); + const p1 = Person.self('A'); + tr.addPerson(p1); + const p2 = Person.self('B'); + expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse'); +}); + +test('add more than one Spouse', () => { + const tr = new TaxReturn(2019); + const p1 = Person.spouse('A'); + tr.addPerson(p1); + const p2 = Person.spouse('B'); + expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse'); +}); + +test('add Self and Spouse', () => { + const tr = new TaxReturn(2019); + const self = Person.self('Billy Bob'); + const spouse = Person.spouse('Jilly Bob'); + tr.addPerson(self); + tr.addPerson(spouse); + + expect(tr.getPerson('Billy')).toBe(self); + expect(tr.getPerson('Jilly')).toBe(spouse); + + expect(() => tr.getPerson('Bob')).toThrow('too imprecise'); +}); + +test('get non-existent person', () => { + const tr = new TaxReturn(2019); + const self = Person.self('Billy Bob'); + tr.addPerson(self); + + expect(tr.getPerson('Billy Bob')).toBe(self); + expect(() => tr.getPerson('Jilly')).toThrow('not found'); +}); diff --git a/src/TaxReturn.ts b/src/TaxReturn.ts new file mode 100644 index 0000000..192a41e --- /dev/null +++ b/src/TaxReturn.ts @@ -0,0 +1,46 @@ +import Form from './Form'; +import { Person, Relation } from './Person'; + +export default class TaxReturn { + private _year: number; + private _people: Person[] = []; + private _forms: Form[] = []; + + constructor(year: number) { + this._year = year; + } + + get year(): number { + return this._year; + } + + addPerson(person: Person) { + if (person.relation == Relation.Dependent) { + throw new UnsupportedFeatureError('Dependents are not supported'); + } + if (person.relation == Relation.Self || person.relation == Relation.Spouse) { + const others = this._people.filter(p => p.relation == person.relation); + if (others.length > 0) { + throw new InconsistencyError('Cannot have more than one Self or Spouse'); + } + } + this._people.push(person); + } + + getPerson(name: RegExp | string): Person { + const people = this._people.filter(p => p.name.search(name) !== -1); + if (people.length != 1) { + throw new Error(`Person ${name} not found or too imprecise`); + } + return people[0]; + } + + addForm(form: Form) { + } +}; + +export class InconsistencyError extends Error { +}; + +export class UnsupportedFeatureError extends Error { +}; -- 2.43.5