Skip to content

Commit

Permalink
fix: generate each data type in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Dec 11, 2023
1 parent 54ff342 commit f292283
Show file tree
Hide file tree
Showing 9 changed files with 1,181 additions and 1,431 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
.DS_Store
.idea/
js-stellar-base/

test/unit/out/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@
"webpack-cli": "^5.0.1"
},
"dependencies": {
"@stellar/stellar-base": "10.0.0",
"axios": "^1.6.0",
"bignumber.js": "^9.1.2",
"eventsource": "^2.0.2",
"json-schema-faker": "^0.5.4",
"randombytes": "^2.1.0",
"@stellar/stellar-base": "10.0.0",
"toml": "^3.0.0",
"urijs": "^1.19.1"
}
Expand Down
137 changes: 86 additions & 51 deletions src/contract_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,64 @@ export class ContractSpec {
}
}
if (Array.isArray(val)) {
if (xdr.ScSpecType.scSpecTypeVec().value === value) {
let vec = ty.value() as xdr.ScSpecTypeVec;
let elementType = vec.elementType();
return xdr.ScVal.scvVec(
val.map((v) => this.nativeToScVal(v, elementType))
);
} else if (xdr.ScSpecType.scSpecTypeTuple().value === value) {
let tup = ty.value() as xdr.ScSpecTypeTuple;
let valTypes = tup.valueTypes();
if (val.length !== valTypes.length) {
throw new TypeError(
`Tuple expects ${valTypes.length} values, but ${val.length} were provided`
// if (xdr.ScSpecType.scSpecTypeVec().value === value) {
// let vec = ty.value() as xdr.ScSpecTypeVec;
// let elementType = vec.elementType();
// return xdr.ScVal.scvVec(
// val.map((v) => this.nativeToScVal(v, elementType))
// );
// } else if (xdr.ScSpecType.scSpecTypeTuple().value === value) {
// let tup = ty.value() as xdr.ScSpecTypeTuple;
// let valTypes = tup.valueTypes();
// if (val.length !== valTypes.length) {
// throw new TypeError(
// `Tuple expects ${valTypes.length} values, but ${val.length} were provided`
// );
// }
// return xdr.ScVal.scvVec(
// val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
// );
// } else {
// throw new TypeError(`Type ${ty} was not vec, but value was Array`);
// }
// This is converted to use a switch statement with a new case for the map type
switch (value) {
case xdr.ScSpecType.scSpecTypeVec().value: {
let vec = ty.value() as xdr.ScSpecTypeVec;
let elementType = vec.elementType();
return xdr.ScVal.scvVec(
val.map((v) => this.nativeToScVal(v, elementType))
);
}
return xdr.ScVal.scvVec(
val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
);
} else {
throw new TypeError(`Type ${ty} was not vec, but value was Array`);
case xdr.ScSpecType.scSpecTypeTuple().value: {
let tup = ty.value() as xdr.ScSpecTypeTuple;
let valTypes = tup.valueTypes();
if (val.length !== valTypes.length) {
throw new TypeError(
`Tuple expects ${valTypes.length} values, but ${val.length} were provided`
);
}
return xdr.ScVal.scvVec(
val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
);
}
case xdr.ScSpecType.scSpecTypeMap().value: {
let map = ty.value() as xdr.ScSpecTypeMap;
let keyType = map.keyType();
let valueType = map.valueType();
return xdr.ScVal.scvMap(
val.map((entry) => {
let key = this.nativeToScVal(entry[0], keyType);
let val = this.nativeToScVal(entry[1], valueType);
return new xdr.ScMapEntry({ key, val });
})
);
}

default:
throw new TypeError(
`Type ${ty} was not vec, but value was Array`
);
}
}
if (val.constructor === Map) {
Expand Down Expand Up @@ -518,7 +557,7 @@ export class ContractSpec {
case xdr.ScValType.scvI128().value:
case xdr.ScValType.scvU256().value:
case xdr.ScValType.scvI256().value:
return scValToBigInt(scv) as T;
return (scValToBigInt(scv) as T);

case xdr.ScValType.scvVec().value: {
if (value == xdr.ScSpecType.scSpecTypeVec().value) {
Expand All @@ -545,12 +584,12 @@ export class ContractSpec {
let type_ = typeDef.value() as xdr.ScSpecTypeMap;
let keyType = type_.keyType();
let valueType = type_.valueType();
return new Map(
map.map((entry) => [
this.scValToNative(entry.key(), keyType),
this.scValToNative(entry.val(), valueType),
])
) as T;

let res = map.map((entry) => [
this.scValToNative(entry.key(), keyType),
this.scValToNative(entry.val(), valueType),
]) as T;
return res;
}
throw new TypeError(
`ScSpecType ${t.name} was not map, but ${JSON.stringify(
Expand Down Expand Up @@ -773,20 +812,6 @@ function findCase(name: string) {
}

const PRIMITIVE_DEFINITONS = {
U64: {
type: "string",
pattern: "^[0-9]+$",
format: "bigint",
minLength: 1,
maxLength: 20, // 64-bit max value has 20 digits
},
I64: {
type: "string",
pattern: "^-?^[0-9]+$",
format: "bigint",
minLength: 1,
maxLength: 21, // Includes additional digit for the potential '-'
},
U32: {
type: "integer",
minimum: 0,
Expand All @@ -797,31 +822,39 @@ const PRIMITIVE_DEFINITONS = {
minimum: -2147483648,
maximum: 2147483647,
},
U64: {
type: "string",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 20, // 64-bit max value has 20 digits
},
I64: {
type: "string",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 21, // Includes additional digit for the potential '-'
},
U128: {
type: "string",
pattern: "^[0-9]+$",
format: "bigint",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 39, // 128-bit max value has 39 digits
},
I128: {
type: "string",
pattern: "^-?[0-9]+$",
format: "bigint",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 40, // Includes additional digit for the potential '-'
},
U256: {
type: "string",
pattern: "^[0-9]+$",
format: "bigint",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 78, // 256-bit max value has 78 digits
},
I256: {
type: "string",
pattern: "^-?[0-9]+$",
format: "bigint",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 79, // Includes additional digit for the potential '-'
},
Expand Down Expand Up @@ -945,13 +978,15 @@ function typeRef(typeDef: xdr.ScSpecTypeDef): object {
}
case xdr.ScSpecType.scSpecTypeMap().value: {
let map = typeDef.value() as xdr.ScSpecTypeMap;
let ref = typeRef(map.valueType());
let items = [typeRef(map.keyType()), typeRef(map.valueType())];
return {
type: "object",
patternProperties: {
"^[a-zA-Z0-9]+$": ref,
type: "array",
items: {
type: "array",
items,
minItems: 2,
maxItems: 2,
},
additionalProperties: false,
};
}
case xdr.ScSpecType.scSpecTypeTuple().value: {
Expand Down
6 changes: 3 additions & 3 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"extends": "@stellar/tsconfig",
"compilerOptions": {
"declaration": false,
"sourceMap": false,
"removeComments": false,
"lib": ["es2015"],
"moduleResolution": "node",
"rootDir": "..",
// "outDir": "./unit/build",
"rootDir": "./unit/spec",
"outDir": "./unit/out",
"target": "ES2020",
"strict": false,
},
"allowSyntheticDefaultImports": true,
"include": ["**/*.ts" ]
}
Loading

0 comments on commit f292283

Please sign in to comment.