* -------------------------------------------------------------------------
* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
import { safeStr, formatDecimal } from './Common';
describe('test function safeStr', () => {
it('test input string', () => {
const unSafeText = 'This is unsafe / " \' & <div>div</div>';
expect(safeStr(unSafeText)).toBe('This is unsafe / " ' & <div>div</div>');
});
it('test input number', () => {
const num = 100;
expect(safeStr(num)).toBe('100');
});
it('test ignore', () => {
const unSafeText = 'This is unsafe / " \' & <div>div</div>';
expect(safeStr(unSafeText, '')).toBe('This is unsafe / " ' & <div>div</div>');
expect(safeStr(unSafeText, '<')).toBe('This is unsafe / " ' & <div>div</div>');
expect(safeStr(unSafeText, '>')).toBe('This is unsafe / " ' & <div>div</div>');
expect(safeStr(unSafeText, '&')).toBe('This is unsafe / " ' & <div>div</div>');
expect(safeStr(unSafeText, '\'')).toBe('This is unsafe / " \' & <div>div</div>');
expect(safeStr(unSafeText, '"')).toBe('This is unsafe / " ' & <div>div</div>');
expect(safeStr(unSafeText, '/')).toBe('This is unsafe / " ' & <div>div</div>');
});
});
describe('test function formatDecimal', () => {
describe('Default fixed value (fixed = 2)', () => {
test('should return integers with two decimal places', () => {
expect(formatDecimal(123)).toBe(123);
expect(formatDecimal(123.4)).toBe(123.4);
expect(formatDecimal('456')).toBe(456);
});
test('should handle rounding correctly', () => {
expect(formatDecimal(1.235)).toBe(1.24);
expect(formatDecimal(1.234)).toBe(1.23);
});
test('should correctly truncate decimals beyond two places', () => {
expect(formatDecimal(123.456)).toBe(123.46);
expect(formatDecimal(987.65123)).toBe(987.65);
});
});
describe('Custom number of decimal places (fixed > 0)', () => {
test('when fixed = 3, should keep three decimal places and round correctly', () => {
expect(formatDecimal(1.2345, 3)).toBe(1.234);
expect(formatDecimal(1.2344, 3)).toBe(1.234);
expect(formatDecimal(10, 3)).toBe(10);
});
test('when fixed = 0, should return the nearest integer', () => {
expect(formatDecimal(1.5, 0)).toBe(2);
expect(formatDecimal(1.4, 0)).toBe(1);
expect(formatDecimal('2.51', 0)).toBe(3);
});
});
describe('Handling very small numbers (near zero)', () => {
test('should dynamically increase precision based on the first non-zero digit position', () => {
expect(formatDecimal(0.0123)).toBe(0.01);
expect(formatDecimal(0.00123)).toBe(0.0012);
expect(formatDecimal(0.00125, 2)).toBe(0.0013);
expect(formatDecimal(0.000005)).toBe(0.000005);
});
test('should handle small numbers with custom fixed combination', () => {
expect(formatDecimal(0.000123, 1)).toBe(0.0001);
expect(formatDecimal(0.0123, 5)).toBe(0.0123);
});
});
describe('Boundary conditions and special inputs', () => {
test('should return 0 when input is 0 or "0"', () => {
expect(formatDecimal(0)).toBe(0);
expect(formatDecimal('0')).toBe(0);
});
test('should correctly handle negative numbers', () => {
expect(formatDecimal(-1.235)).toBe(-1.24);
expect(formatDecimal(-0.00123)).toBe(-0.0012);
});
test('should return NaN for non-numeric or NaN inputs', () => {
expect(formatDecimal('abc')).toBeNaN();
expect(formatDecimal(NaN)).toBeNaN();
});
});
});