|
|
|
|
@ -1,28 +1,32 @@
|
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
|
|
|
import { createUnit } from '../unit_engine';
|
|
|
|
|
import { createUnit } from '@/glob/unit_engine';
|
|
|
|
|
import { createLing } from '@/ling/engine';
|
|
|
|
|
function makeLing(locale = 'es') {
|
|
|
|
|
return createLing({} as any, locale as any);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('createUnit', () => {
|
|
|
|
|
|
|
|
|
|
describe('selectedSystem', () => {
|
|
|
|
|
|
|
|
|
|
it('usa metric por defecto', () => {
|
|
|
|
|
const unit = createUnit({}, {}, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, {}, 'es');
|
|
|
|
|
expect(unit.getSystem()).toBe('metric');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('acepta imperial en la configuración', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.getSystem()).toBe('imperial');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('setSystem cambia el sistema activo', () => {
|
|
|
|
|
const unit = createUnit({}, {}, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, {}, 'es');
|
|
|
|
|
unit.setSystem('imperial');
|
|
|
|
|
expect(unit.getSystem()).toBe('imperial');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('el sistema reactivo afecta a f()', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.f(33)).toContain('kg');
|
|
|
|
|
|
|
|
|
|
unit.setSystem('imperial');
|
|
|
|
|
@ -34,11 +38,13 @@ describe('createUnit', () => {
|
|
|
|
|
describe('locale reactivo via función', () => {
|
|
|
|
|
|
|
|
|
|
it('responde a cambios de locale sin recrear el módulo', () => {
|
|
|
|
|
const ling = makeLing('es');
|
|
|
|
|
let locale = 'es';
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, () => locale);
|
|
|
|
|
const unit = createUnit(ling, {}, { selectedSystem: 'metric' }, () => locale);
|
|
|
|
|
expect(unit.weight.f(1, 'name')).toBe('1 kilogramo');
|
|
|
|
|
|
|
|
|
|
locale = 'en';
|
|
|
|
|
ling.setLocale('en' as any);
|
|
|
|
|
expect(unit.weight.f(1, 'name')).toBe('1 kilogram');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -51,64 +57,59 @@ describe('createUnit', () => {
|
|
|
|
|
describe('weight', () => {
|
|
|
|
|
|
|
|
|
|
it('f() metric — unidad base kg', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.f(33)).toContain('kg');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() imperial — unidad base lb', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.weight.f(33)).toContain('lb');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() display name singular en es', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing('es'), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.f(1, 'name')).toBe('1 kilogramo');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() display name plural en es', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing('es'), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.f(5, 'name')).toBe('5 kilogramos');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() display name en en', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'en');
|
|
|
|
|
const unit = createUnit(makeLing('en'), {}, { selectedSystem: 'metric' }, 'en');
|
|
|
|
|
expect(unit.weight.f(1, 'name')).toBe('1 kilogram');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() display name imperial en en', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'en');
|
|
|
|
|
const unit = createUnit(makeLing('en'), {}, { selectedSystem: 'imperial' }, 'en');
|
|
|
|
|
expect(unit.weight.f(1, 'name')).toBe('1 pound');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fTo() convierte kg a lb', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'en');
|
|
|
|
|
const unit = createUnit(makeLing('en'), {}, { selectedSystem: 'imperial' }, 'en');
|
|
|
|
|
const result = unit.weight.fTo(1);
|
|
|
|
|
expect(result).toContain('lb');
|
|
|
|
|
expect(result).toContain('2.2');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fTo() con unidad destino explícita', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.fTo(1, 'oz')).toContain('oz');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fTo() sin conversión en metric devuelve kg', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.weight.fTo(33)).toContain('kg');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() kg a lb', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.weight.convert(1, 'kg', 'lb')).toBeCloseTo(2.2046, 2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() kg a g', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.weight.convert(1, 'kg', 'g')).toBeCloseTo(1000, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() misma unidad', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.weight.convert(33, 'kg', 'kg')).toBe(33);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -121,22 +122,22 @@ describe('createUnit', () => {
|
|
|
|
|
describe('length', () => {
|
|
|
|
|
|
|
|
|
|
it('f() metric — unidad base m', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.length.f(100)).toContain('m');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() imperial — unidad base mi', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.length.f(1609)).toContain('mi');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() m a km', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.length.convert(1000, 'm', 'km')).toBeCloseTo(1, 5);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() km a mi', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.length.convert(1.609344, 'km', 'mi')).toBeCloseTo(1, 3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -149,34 +150,34 @@ describe('createUnit', () => {
|
|
|
|
|
describe('temp', () => {
|
|
|
|
|
|
|
|
|
|
it('f() metric — °C', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.temp.f(100)).toContain('°C');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() imperial — °F', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.temp.f(212)).toContain('°F');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fTo() convierte °C a °F', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const result = unit.temp.fTo(100);
|
|
|
|
|
expect(result).toContain('°F');
|
|
|
|
|
expect(result).toContain('212');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() °C a °F', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.temp.convert(100, 'c', 'f')).toBeCloseTo(212, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() °C a K', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.temp.convert(0, 'c', 'k')).toBeCloseTo(273.15, 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() °F a °C', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.temp.convert(32, 'f', 'c')).toBeCloseTo(0, 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -189,22 +190,22 @@ describe('createUnit', () => {
|
|
|
|
|
describe('volume', () => {
|
|
|
|
|
|
|
|
|
|
it('f() metric — l', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.volume.f(2)).toContain('l');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() imperial — gal', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.volume.f(2)).toContain('gal');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() l a ml', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.volume.convert(1, 'l', 'ml')).toBeCloseTo(1000, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() l a gal', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.volume.convert(3.78541, 'l', 'gal')).toBeCloseTo(1, 2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -217,25 +218,25 @@ describe('createUnit', () => {
|
|
|
|
|
describe('area', () => {
|
|
|
|
|
|
|
|
|
|
it('f() metric — m²', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'metric' }, 'es');
|
|
|
|
|
expect(unit.area.f(100)).toContain('m²');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('f() imperial — ft²', () => {
|
|
|
|
|
const unit = createUnit({}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
const unit = createUnit(makeLing(), {}, { selectedSystem: 'imperial' }, 'es');
|
|
|
|
|
expect(unit.area.f(100)).toContain('ft²');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() m² a ft²', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.area.convert(1, 'm2', 'ft2')).toBeCloseTo(10.7639, 2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('convert() ha a m²', () => {
|
|
|
|
|
const unit = createUnit();
|
|
|
|
|
const unit = createUnit(makeLing());
|
|
|
|
|
expect(unit.area.convert(1, 'ha', 'm2')).toBeCloseTo(10000, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
});
|