Skip to content

Commit 2585946

Browse files
committed
fix: generate each data type in tests
1 parent 6a63af2 commit 2585946

8 files changed

+1181
-1408
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
.DS_Store
1010
.idea/
1111
js-stellar-base/
12+
13+
test/unit/out/

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@
143143
"webpack-cli": "^5.0.1"
144144
},
145145
"dependencies": {
146+
"@stellar/stellar-base": "10.0.0",
146147
"axios": "^1.6.0",
147148
"bignumber.js": "^9.1.2",
148149
"eventsource": "^2.0.2",
149150
"json-schema-faker": "^0.5.4",
150151
"randombytes": "^2.1.0",
151-
"@stellar/stellar-base": "10.0.0",
152152
"toml": "^3.0.0",
153153
"urijs": "^1.19.1"
154154
}

src/contract_spec.ts

+86-51
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,64 @@ export class ContractSpec {
270270
}
271271
}
272272
if (Array.isArray(val)) {
273-
if (xdr.ScSpecType.scSpecTypeVec().value === value) {
274-
let vec = ty.value() as xdr.ScSpecTypeVec;
275-
let elementType = vec.elementType();
276-
return xdr.ScVal.scvVec(
277-
val.map((v) => this.nativeToScVal(v, elementType))
278-
);
279-
} else if (xdr.ScSpecType.scSpecTypeTuple().value === value) {
280-
let tup = ty.value() as xdr.ScSpecTypeTuple;
281-
let valTypes = tup.valueTypes();
282-
if (val.length !== valTypes.length) {
283-
throw new TypeError(
284-
`Tuple expects ${valTypes.length} values, but ${val.length} were provided`
273+
// if (xdr.ScSpecType.scSpecTypeVec().value === value) {
274+
// let vec = ty.value() as xdr.ScSpecTypeVec;
275+
// let elementType = vec.elementType();
276+
// return xdr.ScVal.scvVec(
277+
// val.map((v) => this.nativeToScVal(v, elementType))
278+
// );
279+
// } else if (xdr.ScSpecType.scSpecTypeTuple().value === value) {
280+
// let tup = ty.value() as xdr.ScSpecTypeTuple;
281+
// let valTypes = tup.valueTypes();
282+
// if (val.length !== valTypes.length) {
283+
// throw new TypeError(
284+
// `Tuple expects ${valTypes.length} values, but ${val.length} were provided`
285+
// );
286+
// }
287+
// return xdr.ScVal.scvVec(
288+
// val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
289+
// );
290+
// } else {
291+
// throw new TypeError(`Type ${ty} was not vec, but value was Array`);
292+
// }
293+
// This is converted to use a switch statement with a new case for the map type
294+
switch (value) {
295+
case xdr.ScSpecType.scSpecTypeVec().value: {
296+
let vec = ty.value() as xdr.ScSpecTypeVec;
297+
let elementType = vec.elementType();
298+
return xdr.ScVal.scvVec(
299+
val.map((v) => this.nativeToScVal(v, elementType))
285300
);
286301
}
287-
return xdr.ScVal.scvVec(
288-
val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
289-
);
290-
} else {
291-
throw new TypeError(`Type ${ty} was not vec, but value was Array`);
302+
case xdr.ScSpecType.scSpecTypeTuple().value: {
303+
let tup = ty.value() as xdr.ScSpecTypeTuple;
304+
let valTypes = tup.valueTypes();
305+
if (val.length !== valTypes.length) {
306+
throw new TypeError(
307+
`Tuple expects ${valTypes.length} values, but ${val.length} were provided`
308+
);
309+
}
310+
return xdr.ScVal.scvVec(
311+
val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
312+
);
313+
}
314+
case xdr.ScSpecType.scSpecTypeMap().value: {
315+
let map = ty.value() as xdr.ScSpecTypeMap;
316+
let keyType = map.keyType();
317+
let valueType = map.valueType();
318+
return xdr.ScVal.scvMap(
319+
val.map((entry) => {
320+
let key = this.nativeToScVal(entry[0], keyType);
321+
let val = this.nativeToScVal(entry[1], valueType);
322+
return new xdr.ScMapEntry({ key, val });
323+
})
324+
);
325+
}
326+
327+
default:
328+
throw new TypeError(
329+
`Type ${ty} was not vec, but value was Array`
330+
);
292331
}
293332
}
294333
if (val.constructor === Map) {
@@ -518,7 +557,7 @@ export class ContractSpec {
518557
case xdr.ScValType.scvI128().value:
519558
case xdr.ScValType.scvU256().value:
520559
case xdr.ScValType.scvI256().value:
521-
return scValToBigInt(scv) as T;
560+
return (scValToBigInt(scv) as T);
522561

523562
case xdr.ScValType.scvVec().value: {
524563
if (value == xdr.ScSpecType.scSpecTypeVec().value) {
@@ -545,12 +584,12 @@ export class ContractSpec {
545584
let type_ = typeDef.value() as xdr.ScSpecTypeMap;
546585
let keyType = type_.keyType();
547586
let valueType = type_.valueType();
548-
return new Map(
549-
map.map((entry) => [
550-
this.scValToNative(entry.key(), keyType),
551-
this.scValToNative(entry.val(), valueType),
552-
])
553-
) as T;
587+
588+
let res = map.map((entry) => [
589+
this.scValToNative(entry.key(), keyType),
590+
this.scValToNative(entry.val(), valueType),
591+
]) as T;
592+
return res;
554593
}
555594
throw new TypeError(
556595
`ScSpecType ${t.name} was not map, but ${JSON.stringify(
@@ -773,20 +812,6 @@ function findCase(name: string) {
773812
}
774813

775814
const PRIMITIVE_DEFINITONS = {
776-
U64: {
777-
type: "string",
778-
pattern: "^[0-9]+$",
779-
format: "bigint",
780-
minLength: 1,
781-
maxLength: 20, // 64-bit max value has 20 digits
782-
},
783-
I64: {
784-
type: "string",
785-
pattern: "^-?^[0-9]+$",
786-
format: "bigint",
787-
minLength: 1,
788-
maxLength: 21, // Includes additional digit for the potential '-'
789-
},
790815
U32: {
791816
type: "integer",
792817
minimum: 0,
@@ -797,31 +822,39 @@ const PRIMITIVE_DEFINITONS = {
797822
minimum: -2147483648,
798823
maximum: 2147483647,
799824
},
825+
U64: {
826+
type: "string",
827+
pattern: "^([1-9][0-9]*|0)$",
828+
minLength: 1,
829+
maxLength: 20, // 64-bit max value has 20 digits
830+
},
831+
I64: {
832+
type: "string",
833+
pattern: "^(-?[1-9][0-9]*|0)$",
834+
minLength: 1,
835+
maxLength: 21, // Includes additional digit for the potential '-'
836+
},
800837
U128: {
801838
type: "string",
802-
pattern: "^[0-9]+$",
803-
format: "bigint",
839+
pattern: "^([1-9][0-9]*|0)$",
804840
minLength: 1,
805841
maxLength: 39, // 128-bit max value has 39 digits
806842
},
807843
I128: {
808844
type: "string",
809-
pattern: "^-?[0-9]+$",
810-
format: "bigint",
845+
pattern: "^(-?[1-9][0-9]*|0)$",
811846
minLength: 1,
812847
maxLength: 40, // Includes additional digit for the potential '-'
813848
},
814849
U256: {
815850
type: "string",
816-
pattern: "^[0-9]+$",
817-
format: "bigint",
851+
pattern: "^([1-9][0-9]*|0)$",
818852
minLength: 1,
819853
maxLength: 78, // 256-bit max value has 78 digits
820854
},
821855
I256: {
822856
type: "string",
823-
pattern: "^-?[0-9]+$",
824-
format: "bigint",
857+
pattern: "^(-?[1-9][0-9]*|0)$",
825858
minLength: 1,
826859
maxLength: 79, // Includes additional digit for the potential '-'
827860
},
@@ -945,13 +978,15 @@ function typeRef(typeDef: xdr.ScSpecTypeDef): object {
945978
}
946979
case xdr.ScSpecType.scSpecTypeMap().value: {
947980
let map = typeDef.value() as xdr.ScSpecTypeMap;
948-
let ref = typeRef(map.valueType());
981+
let items = [typeRef(map.keyType()), typeRef(map.valueType())];
949982
return {
950-
type: "object",
951-
patternProperties: {
952-
"^[a-zA-Z0-9]+$": ref,
983+
type: "array",
984+
items: {
985+
type: "array",
986+
items,
987+
minItems: 2,
988+
maxItems: 2,
953989
},
954-
additionalProperties: false,
955990
};
956991
}
957992
case xdr.ScSpecType.scSpecTypeTuple().value: {

test/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"extends": "@stellar/tsconfig",
33
"compilerOptions": {
44
"declaration": false,
5-
"sourceMap": false,
65
"removeComments": false,
76
"lib": ["es2015"],
87
"moduleResolution": "node",
9-
"rootDir": "..",
10-
// "outDir": "./unit/build",
8+
"rootDir": "./unit/spec",
9+
"outDir": "./unit/out",
1110
"target": "ES2020",
1211
"strict": false,
1312
},
13+
"allowSyntheticDefaultImports": true,
1414
"include": ["**/*.ts" ]
1515
}

0 commit comments

Comments
 (0)