Skip to content

Commit

Permalink
feat:add value utilities documentation and sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
AnsonSIDAN committed Aug 19, 2024
1 parent 24427cb commit d90879c
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 5 deletions.
58 changes: 53 additions & 5 deletions packages/mesh-common/test/data/value/comparator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ describe("MeshValue class", () => {
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.geq(target)).toBe(true);
});

it("should return false if there is missing unit in the target value", () => {
const value = new MeshValue({ lovelace: 20n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.geq(target)).toBe(false);
});

it("should return false if there is missing unit in the value.value", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ lovelace: 20n });
expect(value.geq(target)).toBe(true);
});

it("should return false if there is missing unit in both value.value and target.value", () => {
const value = new MeshValue({ lovelace: 20n, somethingelse: 10n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.geq(target)).toBe(false);
});
});
describe("geqUnit", () => {
it("should return true if the value is greater than or equal to the target value for a specific unit", () => {
Expand All @@ -46,8 +64,14 @@ describe("MeshValue class", () => {

it("should return false if the unit does not exist in value.value", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ somethingElse: 5n });
expect(value.geqUnit("somethingElse", target)).toBe(false);
const target = new MeshValue({ lovelace: 20n });
expect(value.geqUnit(mockUnit, target)).toBe(false);
});

it("should return false if the unit does not exist in other.value", () => {
const value = new MeshValue({ lovelace: 20n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.geqUnit(mockUnit, target)).toBe(false);
});
});
describe("leq", () => {
Expand All @@ -68,6 +92,24 @@ describe("MeshValue class", () => {
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.leq(target)).toBe(true);
});

it("should return false if there is missing unit in the target value", () => {
const value = new MeshValue({ lovelace: 20n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.leq(target)).toBe(true);
});

it("should return false if there is missing unit in the value.value", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ lovelace: 20n });
expect(value.leq(target)).toBe(false);
});

it("should return false if there is missing unit in both value.value and target.value", () => {
const value = new MeshValue({ lovelace: 20n, somethingelse: 10n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.leq(target)).toBe(false);
});
});
describe("leqUnit", () => {
it("should return true if the value is less than or equal to the target value for a specific unit", () => {
Expand All @@ -91,10 +133,16 @@ describe("MeshValue class", () => {
expect(value.leqUnit(mockUnit, target)).toBe(true);
});

it("should return false if the unit does not exist in value.value", () => {
it("should return false if the unit does not exist in other.value", () => {
const value = new MeshValue({ lovelace: 20n });
const target = new MeshValue({ lovelace: 5n, [mockUnit]: 10n });
expect(value.leqUnit(mockUnit, target)).toBe(false);
});

it("should return false if the unit does not exist in this.value", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ somethingElse: 5n });
expect(value.leqUnit("somethingElse", target)).toBe(false);
const target = new MeshValue({ lovelace: 20n });
expect(value.leqUnit(mockUnit, target)).toBe(false);
});
});
describe("isEmpty", () => {
Expand Down
99 changes: 99 additions & 0 deletions packages/mesh-common/test/data/value/convertor.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
Asset,
assocMap,
byteString,
currencySymbol,
dict,
Dict,
Integer,
integer,
MeshValue,
MValue,
mValue,
tokenName,
Value,
value,
} from "@meshsdk/common";
Expand Down Expand Up @@ -170,4 +173,100 @@ describe("MeshValue class", () => {
expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
});
});
describe("toData", () => {
test("Empty Value", () => {
const val: Asset[] = [];
const plutusValue: Value = value(val);
const data = MeshValue.fromValue(plutusValue).toData();
const expected: MValue = mValue(val);
expect(JSON.stringify(expected)).toBe(JSON.stringify(data));
});

test("Multiple Assets with Same Policy", () => {
const val: Asset[] = [
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
quantity: "100",
},
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
quantity: "200",
},
];
const plutusValue: Value = value(val);
const data = MeshValue.fromValue(plutusValue).toData();
const expected: MValue = mValue(val);

expect(JSON.stringify(expected)).toBe(JSON.stringify(data));
});

test("Mixed Assets", () => {
const val: Asset[] = [
{ unit: "lovelace", quantity: "1000000" },
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
quantity: "567",
},
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461",
quantity: "345",
},
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
quantity: "100",
},
];
const plutusValue: Value = value(val);
const data = MeshValue.fromValue(plutusValue).toData();
const expected: MValue = mValue(val);
expect(JSON.stringify(expected)).toBe(JSON.stringify(data));
});

test("Single Asset with Large Quantity", () => {
const val: Asset[] = [{ unit: "lovelace", quantity: "1000000000000" }];
const plutusValue: Value = value(val);
const data = MeshValue.fromValue(plutusValue).toData();
const expected: MValue = mValue(val);
expect(JSON.stringify(expected)).toBe(JSON.stringify(data));
});
});
describe("toJSON", () => {
test("should correctly convert MeshValue to JSON with multiple assets", () => {
const assets: Asset[] = [
{ unit: "lovelace", quantity: "1000000" },
{
unit: "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64",
quantity: "500",
},
];

const expectedValue = assocMap([
[currencySymbol(""), assocMap([[tokenName(""), integer(1000000)]])],
[
currencySymbol(
"c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c",
),
assocMap([[tokenName("000643b04d65736820676f6f64"), integer(500)]]),
],
]);

const meshValue = new MeshValue();
meshValue.toAssets = () => assets;

const jsonValue = meshValue.toJSON();
expect(JSON.stringify(jsonValue)).toEqual(JSON.stringify(expectedValue));
});

test("should correctly convert MeshValue to JSON with no asset", () => {
const assets: Asset[] = [];

const expectedValue = assocMap([]);

const meshValue = new MeshValue();
meshValue.toAssets = () => assets;

const jsonValue = meshValue.toJSON();
expect(JSON.stringify(jsonValue)).toEqual(JSON.stringify(expectedValue));
});
});
});

0 comments on commit d90879c

Please sign in to comment.