forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix oppia#19428: Same unit types should be accepted in Number with un…
…its interaction (oppia#19706) * Fix oppia#19428: Same unit types should be accepted in Number with units interaction * add test * simplitfy logic * update isEqual logic * fix linter * fix linter * refactor code * add tests * add test for Units map * add test for map * update comment * update comment * update test * fix lint * remove duplicate unit * remove hard-coded validUnits array * remove hard-coded validUnits array * add type * add type * fix units * add types for mathjs * update code to handle prefixes * fix linter * undo changes to combine-tests.spec.ts * update test for prefix * add test to verify compleness of mapping * fix spelling mistake * break down long line * add more tests * add sorting case * add test * fix currency prefix issue * reuse function * fix linter * add unit * refactor code * fix linter * add comment
- Loading branch information
Showing
8 changed files
with
777 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
core/templates/domain/objects/objects-domain.constants.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2024 The Oppia Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS-IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @fileoverview unit tests for the units object domain constants. | ||
*/ | ||
|
||
import {all, create} from 'mathjs'; | ||
import {getCurrencyUnits} from './NumberWithUnitsObjectFactory'; | ||
import {ObjectsDomainConstants} from './objects-domain.constants'; | ||
|
||
describe('ObjectsDomainConstants', () => { | ||
let unitPrefixes: string[] = []; | ||
let currencyUnits: string[] = []; | ||
let mathjsUnits: string[] = []; | ||
const math = create(all); | ||
|
||
const isValidUnit = (unit: string): boolean => { | ||
return currencyUnits.includes(unit) || math.Unit.isValuelessUnit(unit); | ||
}; | ||
|
||
const isValidPrefix = (prefix: string): boolean => { | ||
return unitPrefixes.includes(prefix); | ||
}; | ||
|
||
const getUnitPrefixes = (): string[] => { | ||
const prefixes = math.Unit.PREFIXES; | ||
let prefixSet = new Set<string>(); | ||
for (const name in prefixes) { | ||
// Skip if prefix type is 'NONE'. | ||
if (name === 'NONE') { | ||
continue; | ||
} | ||
for (const prefix in prefixes[name]) { | ||
// Each prefix type has an empty key that we can ignore. | ||
if (prefix === '') { | ||
continue; | ||
} | ||
prefixSet.add(prefix); | ||
} | ||
} | ||
|
||
return Array.from(prefixSet); | ||
}; | ||
|
||
const getAllMathjsUnits = (): string[] => { | ||
return Object.keys(math.Unit.UNITS); | ||
}; | ||
|
||
beforeEach(() => { | ||
unitPrefixes = getUnitPrefixes(); | ||
currencyUnits = getCurrencyUnits(); | ||
mathjsUnits = getAllMathjsUnits(); | ||
}); | ||
|
||
it( | ||
'should check that every value in UNIT_TO_' + | ||
'NORMALIZED_UNIT_MAPPING is a valid unit', | ||
() => { | ||
Object.values( | ||
ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING | ||
).forEach(unit => { | ||
expect(isValidUnit(unit)).toBe(true); | ||
}); | ||
} | ||
); | ||
|
||
it( | ||
'should check that every key in UNIT_TO_' + | ||
'NORMALIZED_UNIT_MAPPING is a valid unit', | ||
() => { | ||
Object.keys( | ||
ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING | ||
).forEach(unit => { | ||
expect(isValidUnit(unit)).toBe(true); | ||
}); | ||
} | ||
); | ||
|
||
it( | ||
'should check that the UNIT_TO_NORMALIZED_UNIT_MAPPING contains' + | ||
'all units supported by mathjs', | ||
() => { | ||
const units = Object.keys( | ||
ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING | ||
) | ||
.filter((unit: string) => { | ||
return !currencyUnits.includes(unit); | ||
}) | ||
.sort(); | ||
expect(units).toEqual(mathjsUnits.sort()); | ||
} | ||
); | ||
|
||
it( | ||
'should check that every value in PREFIX_TO_' + | ||
'NORMALIZED_PREFIX_MAPPING is a valid prefix', | ||
() => { | ||
Object.values( | ||
ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING | ||
).forEach(prefix => { | ||
expect(isValidPrefix(prefix)).toBe(true); | ||
}); | ||
} | ||
); | ||
|
||
it( | ||
'should check that every key in PREFIX_TO_' + | ||
'NORMALIZED_PREFIX_MAPPING is a valid prefix', | ||
() => { | ||
Object.keys( | ||
ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING | ||
).forEach(prefix => { | ||
expect(isValidPrefix(prefix)).toBe(true); | ||
}); | ||
} | ||
); | ||
|
||
it( | ||
'should check that the PREFIX_TO_NORMALIZED_PREFIX_MAPPING contains' + | ||
'all prefixes supported by mathjs', | ||
() => { | ||
const prefixes = Object.keys( | ||
ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING | ||
).sort(); | ||
expect(prefixes).toEqual(unitPrefixes.sort()); | ||
} | ||
); | ||
}); |
Oops, something went wrong.