increase test coverage

extend test coverage
This commit is contained in:
JFH
2025-12-01 01:22:26 +01:00
parent a37fbac749
commit fa380402e1
52 changed files with 3813 additions and 169 deletions

View File

@@ -1,15 +1,12 @@
import { strict as assert } from 'node:assert'
import 'pathseg'
import { NS } from '../../packages/svgcanvas/core/namespaces.js'
import * as utilities from '../../packages/svgcanvas/core/utilities.js'
import * as math from '../../packages/svgcanvas/core/math.js'
import * as path from '../../packages/svgcanvas/core/path.js'
import setAssertionMethods from '../../support/assert-close.js'
import * as units from '../../packages/svgcanvas/core/units.js'
// eslint-disable-next-line
chai.use(setAssertionMethods)
describe('utilities bbox', function () {
/**
* Create an SVG element for a mock.
@@ -21,6 +18,39 @@ describe('utilities bbox', function () {
Object.entries(jsonMap.attr).forEach(([attr, value]) => {
elem.setAttribute(attr, value)
})
const numFromAttr = (attr, fallback = 0) => Number(jsonMap.attr[attr] ?? fallback)
const calcBBox = () => {
const tag = (jsonMap.element || '').toLowerCase()
switch (tag) {
case 'path': {
const d = jsonMap.attr.d || ''
const nums = (d.match(/-?\\d*\\.?\\d+/g) || []).map(Number)
if (nums.length >= 4) {
const xs = nums.filter((_, i) => i % 2 === 0)
const ys = nums.filter((_, i) => i % 2 === 1)
return {
x: Math.min(...xs),
y: Math.min(...ys),
width: Math.max(...xs) - Math.min(...xs),
height: Math.max(...ys) - Math.min(...ys)
}
}
return { x: 0, y: 0, width: 0, height: 0 }
}
case 'rect':
return { x: numFromAttr('x'), y: numFromAttr('y'), width: numFromAttr('width'), height: numFromAttr('height') }
case 'line': {
const x1 = numFromAttr('x1'); const x2 = numFromAttr('x2'); const y1 = numFromAttr('y1'); const y2 = numFromAttr('y2')
return { x: Math.min(x1, x2), y: Math.min(y1, y2), width: Math.abs(x2 - x1), height: Math.abs(y2 - y1) }
}
default:
return { x: 0, y: 0, width: 0, height: 0 }
}
}
const bbox = calcBBox()
elem.getBBox = () => {
return { ...bbox }
}
return elem
}
let mockaddSVGElementsFromJsonCallCount = 0