MCPcopy Create free account
hub / github.com/d1ll0n/stack-packer / generateHardhatTestForFunction

Function generateHardhatTestForFunction

src/code-gen/test/hardhat.ts:118–206  ·  view source on GitHub ↗
(
  struct: ProcessedStruct,
  fn: CodeGenFunction,
  libraryName: string
)

Source from the content-addressed store, hash-verified

116]);
117
118export function generateHardhatTestForFunction(
119 struct: ProcessedStruct,
120 fn: CodeGenFunction,
121 libraryName: string
122) {
123 const contractName = `external${libraryName}`;
124 const testCode: ArrayJoinInput<string>[] = [];
125 const generateCall = (args: string[]) => _generateCall(contractName, fn, args)
126 const getOverflowTest = (args: string[], field: ProcessedField) => _getOverflowTest(contractName, fn, args, field)
127 const getUnderflowTest = (args: string[], field: ProcessedField) => _getUnderflowTest(contractName, fn, args, field)
128
129 const fields = fn.internalType === 'getter' ? fn.outputFields : fn.inputFields;
130 const {minValues, maxValues} = getMinMaxValues(fields)
131
132 const minMax = minValues.map((f, i) => i % 2 ? minValues[i] : maxValues[i]);
133 const maxMin = minValues.map((f, i) => i % 2 ? maxValues[i] : minValues[i]);
134 if (fn.internalType === 'setter') {
135 for (let i = 0; i < fields.length; i++) {
136 const inputs = [...minValues];
137 const field = fields[i];
138 if (shouldCheckForOverflow(field)) {
139 inputs[i] = getFieldValue(field, 'overflow');
140 testCode.push(getOverflowTest(inputs, field), '')
141 if (field.type.meta === 'elementary' && field.type.type === 'int') {
142 inputs[i] = getFieldValue(field, 'underflow');
143 testCode.push(getUnderflowTest(inputs, field), '')
144 }
145 }
146 }
147 const getGetter = _env.findGetterOrSetterForFields(struct, fields, fn, contractName, 'getter');
148 if (!getGetter) {
149 console.log(`Could not find way to get all fields updated by ${fn.name}`)
150 return undefined;
151 }
152 const getterCode = getGetter();
153 const testSetMinMax = getSetterTest(
154 `Should be able to set ${fields.length === 1 ? 'min value' : 'min/max values'}`,
155 fields,
156 [generateCall(minMax)],
157 getterCode,
158 minMax
159 )
160 testCode.push(testSetMinMax, '')
161 const testSetMaxMin = getSetterTest(
162 `Should be able to set ${fields.length === 1 ? 'max value' : 'max/min values'}`,
163 fields,
164 [generateCall(maxMin)],
165 getterCode,
166 maxMin
167 )
168 testCode.push(testSetMaxMin, '')
169 } else {
170 const destructuredDecodeParams = fn.outputFields.length > 1
171 ? `{ ${fn.outputFields.map(f => f.name).join(", ")} }`
172 : `${fn.outputFields[0].name}`
173 const getterCode = [`const ${destructuredDecodeParams} = await ${contractName}.${fn.name}()`]
174
175 const getSetter = _env.findGetterOrSetterForFields(struct, fields, fn, contractName, 'setter');

Callers 1

generateHardhatTestFunction · 0.85

Calls 9

shouldCheckForOverflowFunction · 0.90
removeTrailingNewLinesFunction · 0.90
getMinMaxValuesFunction · 0.70
getFieldValueFunction · 0.70
getOverflowTestFunction · 0.70
getUnderflowTestFunction · 0.70
getSetterTestFunction · 0.70
generateCallFunction · 0.70

Tested by

no test coverage detected