From 87fc97467f9ecb487e61b9d822ea425081315ddd Mon Sep 17 00:00:00 2001 From: jurra Date: Thu, 9 Jan 2025 12:44:16 +0100 Subject: [PATCH] With these commit we add resistors and wires and test their behavior using parametrized tests. --- src/domain/entities/Resistor.js | 28 +++++++++++++++++++++------- src/domain/entities/Wire.js | 8 ++++++++ tests/domain/Element.test.js | 8 ++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 src/domain/entities/Wire.js diff --git a/src/domain/entities/Resistor.js b/src/domain/entities/Resistor.js index 6dbc0fa..1478c55 100644 --- a/src/domain/entities/Resistor.js +++ b/src/domain/entities/Resistor.js @@ -1,9 +1,23 @@ -// src/domain/entities/Resistor.js -export class Resistor { - constructor(id, position, resistance) { - this.id = id; - this.type = 'resistor'; // Used for rendering or logic - this.position = position; // Position value object - this.resistance = resistance; // Resistance value object +import { Element } from './Element.js'; +import { Properties } from '../valueObjects/Properties.js'; + +/** + * Represents a Resistor in the circuit. + */ +export class Resistor extends Element { + /** + * Creates an instance of Resistor. + * + * @param {string} id - The unique identifier for the resistor. + * @param {Position[]} terminals - The two terminal positions for the resistor. + * @param {Label|null} label - The label of the resistor (optional). + * @param {Properties} properties - A container for the resistor's properties, including resistance. + */ + constructor(id, terminals, label = null, properties = new Properties({ resistance: "undefined" })) { + if (terminals.length !== 2) { + throw new Error("A Resistor must have exactly two terminals."); + } + super(id, terminals, label, properties); + this.type = 'resistor'; } } diff --git a/src/domain/entities/Wire.js b/src/domain/entities/Wire.js new file mode 100644 index 0000000..1703c64 --- /dev/null +++ b/src/domain/entities/Wire.js @@ -0,0 +1,8 @@ +import { Element } from './Element.js'; + +export class Wire extends Element { + constructor(id, terminals, label = null, properties = new Properties()) { + super(id, terminals, label, properties); + this.type = 'wire'; + } +} diff --git a/tests/domain/Element.test.js b/tests/domain/Element.test.js index 84ce8f2..1a01d45 100644 --- a/tests/domain/Element.test.js +++ b/tests/domain/Element.test.js @@ -1,5 +1,7 @@ import { expect } from 'chai'; import { Element } from '../../src/domain/entities/Element.js'; +import { Resistor } from '../../src/domain/entities/Resistor.js'; +import { Wire } from '../../src/domain/entities/Wire.js'; import { MockElement } from './MockElement.js'; // A mock element class for testing import { Position } from '../../src/domain/valueObjects/Position.js'; import { Label } from '../../src/domain/valueObjects/Label.js'; @@ -80,4 +82,10 @@ describe('Element Class Tests', () => { // Tests for MockElement testElementSubclass('MockElement', MockElement, { mockProperty: 42 }); + + // Tests for Resistor + testElementSubclass('Resistor', Resistor, { resistance: 100 }); + + // Tests for Wire + testElementSubclass('Wire', Wire, {}); }); \ No newline at end of file