|
| 1 | +import Ellipse from '../Ellipse' |
| 2 | + |
| 3 | +describe('Ellipse', () => { |
| 4 | + describe('Constructor', () => { |
| 5 | + test('creates an ellipse with valid dimensions', () => { |
| 6 | + const ellipse = new Ellipse(5, 10) |
| 7 | + expect(ellipse).toBeInstanceOf(Ellipse) |
| 8 | + expect(ellipse.radiusX).toBe(5) |
| 9 | + expect(ellipse.radiusY).toBe(10) |
| 10 | + }) |
| 11 | + |
| 12 | + test('throws an error if any dimension is invalid', () => { |
| 13 | + expect(() => new Ellipse(-5, 10)).toThrow( |
| 14 | + 'radiusX must be a positive number.' |
| 15 | + ) |
| 16 | + expect(() => new Ellipse(5, -10)).toThrow( |
| 17 | + 'radiusY must be a positive number.' |
| 18 | + ) |
| 19 | + expect(() => new Ellipse(NaN, 10)).toThrow( |
| 20 | + 'radiusX must be a positive number.' |
| 21 | + ) |
| 22 | + expect(() => new Ellipse(5, undefined)).toThrow( |
| 23 | + 'radiusY must be a positive number.' |
| 24 | + ) |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + describe('Area Calculation', () => { |
| 29 | + test('calculates area correctly', () => { |
| 30 | + const ellipse = new Ellipse(5, 10) |
| 31 | + expect(ellipse.area()).toBeCloseTo(Math.PI * 5 * 10) // Area = π * rX * rY |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('Circumference Calculation', () => { |
| 36 | + test('calculates circumference correctly', () => { |
| 37 | + const ellipse = new Ellipse(5, 10) |
| 38 | + expect(ellipse.circumference()).toBeCloseTo( |
| 39 | + Math.PI * (3 * (5 + 10) - Math.sqrt((3 * 5 + 10) * (5 + 3 * 10))) |
| 40 | + ) // Circumference using Ramanujan's approximation |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('Getters', () => { |
| 45 | + test('radiusX getter returns correct value', () => { |
| 46 | + const ellipse = new Ellipse(5, 10) |
| 47 | + expect(ellipse.radiusX).toBe(5) |
| 48 | + }) |
| 49 | + |
| 50 | + test('radiusY getter returns correct value', () => { |
| 51 | + const ellipse = new Ellipse(5, 10) |
| 52 | + expect(ellipse.radiusY).toBe(10) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('String Representation', () => { |
| 57 | + test('returns correct string representation', () => { |
| 58 | + const ellipse = new Ellipse(5, 10) |
| 59 | + expect(ellipse.toString()).toBe( |
| 60 | + `Ellipse: radiusX = 5, radiusY = 10, area = ${ |
| 61 | + Math.PI * 5 * 10 |
| 62 | + }, circumference = ${ |
| 63 | + Math.PI * (3 * (5 + 10) - Math.sqrt((3 * 5 + 10) * (5 + 3 * 10))) |
| 64 | + }` |
| 65 | + ) |
| 66 | + }) |
| 67 | + }) |
| 68 | +}) |
0 commit comments