import { Dict } from '../../types/types';
import { getMessages } from '../l10nBundle';
-import {
- enhanceConditionWithMeasure,
- formatMeasure,
- getMinDecimalsCountToBeDistinctFromThreshold,
- isPeriodBestValue,
-} from '../measures';
+import { enhanceConditionWithMeasure, formatMeasure, isPeriodBestValue } from '../measures';
import { mockQualityGateStatusCondition } from '../mocks/quality-gates';
import { mockMeasureEnhanced, mockMetric } from '../testMocks';
expect(formatMeasure(undefined, 'INT')).toBe('');
});
});
-
-describe('getMinDecimalsCountToBeDistinctFromThreshold', () => {
- it('should return default if no threshold', () => {
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.67, undefined)).toBe(1);
- });
-
- it('should return 1 if delta is 0', () => {
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.5, 2.5)).toBe(1);
- });
-
- it('should return 1 if the delta is larger than 0.1', () => {
- [0.1, 0.15, 0.2, 0.5, 0.8, 1].forEach((delta) => {
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.5 + delta, 2.5)).toBe(1);
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.5 - delta, 2.5)).toBe(1);
- });
- });
-
- it('should return enough precision to see the delta', () => {
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.55, 2.5)).toBe(2);
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.505, 2.5)).toBe(3);
- expect(getMinDecimalsCountToBeDistinctFromThreshold(2.5005, 2.5)).toBe(4);
- expect(getMinDecimalsCountToBeDistinctFromThreshold(85.01, 85)).toBe(2);
- expect(getMinDecimalsCountToBeDistinctFromThreshold(84.95, 85)).toBe(2);
- // eslint-disable-next-line no-loss-of-precision
- expect(getMinDecimalsCountToBeDistinctFromThreshold(84.999999999999554, 85)).toBe(
- '9999999999995'.length
- );
- // eslint-disable-next-line no-loss-of-precision
- expect(getMinDecimalsCountToBeDistinctFromThreshold(85.0000000000000954, 85)).toBe(
- '00000000000009'.length
- );
- // eslint-disable-next-line no-loss-of-precision
- expect(getMinDecimalsCountToBeDistinctFromThreshold(85.00000000000000009, 85)).toBe(1);
- });
-});
return type;
}
-/*
- * Conditional decimal count for QualityGate-impacting measures
- * (e.g. Coverage %)
- * Increase the number of decimals if the value is close to the threshold
- * We count the precision (number of 0's, i.e. log10 and round down) needed to show the difference
- * E.g. threshold 85, value 84.9993 -> delta = 0.0007, we need 4 decimals to see the difference
- * otherwise rounding will make it look like they are equal.
- */
-const DEFAULT_DECIMALS = 1;
-export function getMinDecimalsCountToBeDistinctFromThreshold(
- value: number,
- threshold: number | undefined
-): number {
- if (!threshold) {
- return DEFAULT_DECIMALS;
- }
- const delta = Math.abs(threshold - value);
- if (delta < 0.1 && delta > 0) {
- return -Math.floor(Math.log10(delta));
- }
- return DEFAULT_DECIMALS;
-}
-
function useFormatter(
value: string | number | undefined,
formatter: Formatter,