Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

round factor tests #7515

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
71 changes: 70 additions & 1 deletion core/quantity/src/test/CompositeFormats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FormatterSpec } from "../Formatter/FormatterSpec";
import { Formatter } from "../Formatter/Formatter";
import { BasicUnit } from "../Unit";
import { TestUnitsProvider } from "./TestUtils/TestHelper";
import { FormatTraits } from "../core-quantity";
import { DecimalPrecision, FormatTraits } from "../core-quantity";

describe("Composite Formats tests:", () => {
it("Bad Composite unit order", async () => {
Expand Down Expand Up @@ -727,4 +727,73 @@ describe("Composite Formats tests:", () => {
await format.fromJSON(unitsProvider, compositeFormat).catch(() => { });
expect(JSON.stringify(format.toJSON().composite)).to.eql(`{"spacer":"","includeZero":true,"units":[{"name":"Units.KM"},{"name":"Units.M","label":""},{"name":"Units.CM","label":"CM"},{"name":"Units.MM","label":"'"}]}`);
});

it("Test roundFactor with Composite Format", async () => {
const unitsProvider = new TestUnitsProvider();

const formatData = {
composite: {
includeZero: true,
spacer: "",
units: [
{
label: "",
name: "Units.M",
},
],
},
formatTraits: ["applyRounding"],
type: "Decimal",
uomSeparator: "",
};

const format = new Format("test");
await format.fromJSON(unitsProvider, formatData).catch(() => { });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why catch here? Test should fail, so if the exception is thrown that should be fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm good point. This is how format is set up in all other tests as well. I guess it means we wanna proceed with whatever information was successfully loaded into the format (even in the future they change impl of loading format), rather than failing immediately due to an exception. So allow testing with partial info? it is a bit weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm can you quickly verify that's the case? If it is, that seems like a wonky test, relying on things that are not allowed out there in the wild to compose the test inputs...
If there is no error thrown, please remove the portion

assert.isTrue(format.hasUnits);

const testQuantityData = [
{ magnitude: 1, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0, result: "1" },
{ magnitude: 1, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.5, result: "1" },
{ magnitude: 1, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.1, result: "1" },
{ magnitude: 1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.1, result: "1.2" },
{ magnitude: 1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.05, precision: 1, result: "1.3" }, // apply rounding but precision is higher

{ magnitude: 1, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.6, result: "1.2" },
{ magnitude: 1, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.3, result: "0.9" },


{ magnitude: 987.65, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 100, result: "1000" },
{ magnitude: 987.65, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 1000, result: "1000" },
{ magnitude: 987.65, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 2000, result: "0" },

// negative value
{ magnitude: -1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.1, result: "-1.2" },
{ magnitude: -1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: -0.5, result: "-1" },
{ magnitude: -1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: 0.5, result: "-1" },
{ magnitude: 1.23, unit: { name: "Units.M", label: "m", contextId: "Units.LENGTH" }, roundFactor: -0.5, result: "1" }, // TODO: should this one give an error?

// with unit conversion
{ magnitude: 1, unit: { name: "Units.IN", label: "in", contextId: "Units.LENGTH" }, result: "0.0254" }, // round factor defaults to 0
{ magnitude: 1, unit: { name: "Units.IN", label: "in", contextId: "Units.LENGTH" }, roundFactor: 0.5, result: "0" },
{ magnitude: 1, unit: { name: "Units.IN", label: "in", contextId: "Units.LENGTH" }, roundFactor: 0.01, result: "0.03" },
];

for (const testEntry of testQuantityData) {
const unit = new BasicUnit(testEntry.unit.name, testEntry.unit.label, testEntry.unit.contextId);
if (testEntry.roundFactor)
format.roundFactor = testEntry.roundFactor;
if (testEntry.precision)
format.precision = testEntry.precision;
const spec = await FormatterSpec.create("test", format, unitsProvider, unit);
const formattedValue = Formatter.formatQuantity(testEntry.magnitude, spec);

assert.isTrue(formattedValue.length > 0);
assert.strictEqual(formattedValue, testEntry.result);

// reset format
format.roundFactor = 0;
format.precision = 12 as DecimalPrecision;
hl662 marked this conversation as resolved.
Show resolved Hide resolved
}
})

});
Loading