|  | 
| 1 | 1 | import path from 'node:path'; | 
| 2 | 2 | import fs from 'fs-extra'; | 
| 3 |  | -import immutable from 'immutable'; | 
| 4 | 3 | import { afterAll, beforeAll, describe, expect, test } from 'vitest'; | 
| 5 |  | -import * as ts from '../../../codegen/typescript.js'; | 
| 6 | 4 | import ABI from '../abi.js'; | 
| 7 | 5 | import AbiCodeGenerator from './abi.js'; | 
| 8 | 6 | 
 | 
| 9 | 7 | let tempdir: string; | 
| 10 | 8 | let abi: ABI; | 
| 11 |  | -let generatedTypes: any[]; | 
|  | 9 | +let generatedTypes: any[] = []; | 
| 12 | 10 | 
 | 
| 13 | 11 | describe.concurrent('ABI code generation', () => { | 
| 14 | 12 |   beforeAll(async () => { | 
| @@ -179,153 +177,10 @@ describe.concurrent('ABI code generation', () => { | 
| 179 | 177 |   }); | 
| 180 | 178 | 
 | 
| 181 | 179 |   describe('Generated types', () => { | 
| 182 |  | -    test('All expected types are generated', () => { | 
| 183 |  | -      expect(generatedTypes.map(type => type.name)).toEqual([ | 
| 184 |  | -        'Contract__getProposalResultValue0Struct', | 
| 185 |  | -        'Contract__getProposalInputParam1Struct', | 
| 186 |  | -        'Contract__getProposalInputParam1BarStruct', | 
| 187 |  | -        'Contract__getProposalsResultValue1Struct', | 
| 188 |  | -        'Contract__getProposalsResult', | 
| 189 |  | -        'Contract', | 
| 190 |  | -      ]); | 
| 191 |  | -    }); | 
| 192 |  | -  }); | 
| 193 |  | - | 
| 194 |  | -  describe('Contract class', () => { | 
| 195 |  | -    test('Exists', () => { | 
| 196 |  | -      expect(generatedTypes.find(type => type.name === 'Contract')).toBeDefined(); | 
| 197 |  | -    }); | 
| 198 |  | - | 
| 199 |  | -    test('Has methods', () => { | 
| 200 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 201 |  | -      expect(contract.methods).toBeInstanceOf(Array); | 
| 202 |  | -    }); | 
| 203 |  | - | 
| 204 |  | -    test('Has `bind` method', () => { | 
| 205 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 206 |  | -      expect(contract.methods.find((method: any) => method.name === 'bind')).toBeDefined(); | 
| 207 |  | -    }); | 
| 208 |  | - | 
| 209 |  | -    test('Has methods for all callable functions', () => { | 
| 210 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 211 |  | -      expect(contract.methods.map((method: any) => method.name)).toContain('getProposal'); | 
| 212 |  | -    }); | 
| 213 |  | -  }); | 
| 214 |  | - | 
| 215 |  | -  describe('Methods for callable functions', () => { | 
| 216 |  | -    test('Have correct parameters', () => { | 
| 217 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 218 |  | -      expect(contract.methods.map((method: any) => [method.name, method.params])).toEqual([ | 
| 219 |  | -        ['bind', immutable.List([ts.param('address', 'Address')])], | 
| 220 |  | -        ['read', immutable.List()], | 
| 221 |  | -        ['try_read', immutable.List()], | 
| 222 |  | -        [ | 
| 223 |  | -          'getProposal', | 
| 224 |  | -          immutable.List([ | 
| 225 |  | -            ts.param('proposalId', 'BigInt'), | 
| 226 |  | -            ts.param('param1', 'Contract__getProposalInputParam1Struct'), | 
| 227 |  | -          ]), | 
| 228 |  | -        ], | 
| 229 |  | -        [ | 
| 230 |  | -          'try_getProposal', | 
| 231 |  | -          immutable.List([ | 
| 232 |  | -            ts.param('proposalId', 'BigInt'), | 
| 233 |  | -            ts.param('param1', 'Contract__getProposalInputParam1Struct'), | 
| 234 |  | -          ]), | 
| 235 |  | -        ], | 
| 236 |  | -        ['getProposals', immutable.List()], | 
| 237 |  | -        ['try_getProposals', immutable.List()], | 
| 238 |  | -        ['overloaded', immutable.List([ts.param('param0', 'string')])], | 
| 239 |  | -        ['try_overloaded', immutable.List([ts.param('param0', 'string')])], | 
| 240 |  | -        ['overloaded1', immutable.List([ts.param('param0', 'BigInt')])], | 
| 241 |  | -        ['try_overloaded1', immutable.List([ts.param('param0', 'BigInt')])], | 
| 242 |  | -        ['overloaded2', immutable.List([ts.param('param0', 'Bytes')])], | 
| 243 |  | -        ['try_overloaded2', immutable.List([ts.param('param0', 'Bytes')])], | 
| 244 |  | -      ]); | 
| 245 |  | -    }); | 
| 246 |  | - | 
| 247 |  | -    test('Have correct return types', () => { | 
| 248 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 249 |  | -      expect(contract.methods.map((method: any) => [method.name, method.returnType])).toEqual([ | 
| 250 |  | -        ['bind', ts.namedType('Contract')], | 
| 251 |  | -        ['read', ts.namedType('Bytes')], | 
| 252 |  | -        ['try_read', 'ethereum.CallResult<Bytes>'], | 
| 253 |  | -        ['getProposal', ts.namedType('Contract__getProposalResultValue0Struct')], | 
| 254 |  | -        ['try_getProposal', 'ethereum.CallResult<Contract__getProposalResultValue0Struct>'], | 
| 255 |  | -        ['getProposals', ts.namedType('Contract__getProposalsResult')], | 
| 256 |  | -        ['try_getProposals', 'ethereum.CallResult<Contract__getProposalsResult>'], | 
| 257 |  | -        ['overloaded', ts.namedType('string')], | 
| 258 |  | -        ['try_overloaded', 'ethereum.CallResult<string>'], | 
| 259 |  | -        ['overloaded1', ts.namedType('string')], | 
| 260 |  | -        ['try_overloaded1', 'ethereum.CallResult<string>'], | 
| 261 |  | -        ['overloaded2', ts.namedType('string')], | 
| 262 |  | -        ['try_overloaded2', 'ethereum.CallResult<string>'], | 
| 263 |  | -      ]); | 
| 264 |  | -    }); | 
| 265 |  | -  }); | 
| 266 |  | - | 
| 267 |  | -  describe('Tuples', () => { | 
| 268 |  | -    test('Tuple types exist for function parameters', () => { | 
| 269 |  | -      let tupleType = generatedTypes.find( | 
| 270 |  | -        type => type.name === 'Contract__getProposalInputParam1Struct', | 
| 271 |  | -      ); | 
| 272 |  | - | 
| 273 |  | -      // Verify that the tuple type has methods | 
| 274 |  | -      expect(tupleType.methods).toBeDefined(); | 
| 275 |  | - | 
| 276 |  | -      // Verify that the tuple type has getters for all tuple fields with | 
| 277 |  | -      // the right return types | 
| 278 |  | -      expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([ | 
| 279 |  | -        ['get foo', 'i32'], | 
| 280 |  | -        ['get bar', 'Contract__getProposalInputParam1BarStruct'], | 
| 281 |  | -      ]); | 
| 282 |  | - | 
| 283 |  | -      // Inner tuple: | 
| 284 |  | -      tupleType = generatedTypes.find( | 
| 285 |  | -        type => type.name === 'Contract__getProposalInputParam1BarStruct', | 
| 286 |  | -      ); | 
| 287 |  | - | 
| 288 |  | -      // Verify that the tuple type has methods | 
| 289 |  | -      expect(tupleType.methods).toBeDefined(); | 
| 290 |  | - | 
| 291 |  | -      // Verify that the tuple type has getters for all tuple fields with | 
| 292 |  | -      // the right return types | 
| 293 |  | -      expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([ | 
| 294 |  | -        ['get baz', 'Address'], | 
| 295 |  | -      ]); | 
| 296 |  | -    }); | 
| 297 |  | - | 
| 298 |  | -    test('Tuple types exist for function return values', () => { | 
| 299 |  | -      const tupleType = generatedTypes.find( | 
| 300 |  | -        type => type.name === 'Contract__getProposalResultValue0Struct', | 
| 301 |  | -      ); | 
| 302 |  | - | 
| 303 |  | -      // Verify that the tuple type has methods | 
| 304 |  | -      expect(tupleType.methods).toBeDefined(); | 
| 305 |  | - | 
| 306 |  | -      // Verify that the tuple type has getters for all tuple fields with | 
| 307 |  | -      // the right return types | 
| 308 |  | -      expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([ | 
| 309 |  | -        ['get result', 'i32'], | 
| 310 |  | -        ['get target', 'Address'], | 
| 311 |  | -        ['get data', 'Bytes'], | 
| 312 |  | -        ['get proposer', 'Address'], | 
| 313 |  | -        ['get feeRecipient', 'Address'], | 
| 314 |  | -        ['get fee', 'BigInt'], | 
| 315 |  | -        ['get startTime', 'BigInt'], | 
| 316 |  | -        ['get yesCount', 'BigInt'], | 
| 317 |  | -        ['get noCount', 'BigInt'], | 
| 318 |  | -      ]); | 
| 319 |  | -    }); | 
| 320 |  | - | 
| 321 |  | -    test('Function bodies are generated correctly for tuple arrays', () => { | 
| 322 |  | -      const contract = generatedTypes.find(type => type.name === 'Contract'); | 
| 323 |  | -      const getter = contract.methods.find((method: any) => method.name === 'getProposals'); | 
| 324 |  | - | 
| 325 |  | -      expect(getter.body).not.toContain('toTupleArray<undefined>'); | 
| 326 |  | -      expect(getter.body).toContain( | 
| 327 |  | -        'result[1].toTupleArray<Contract__getProposalsResultValue1Struct>()', | 
| 328 |  | -      ); | 
|  | 180 | +    test(`Type test`, () => { | 
|  | 181 | +      for (const generatedType of generatedTypes) { | 
|  | 182 | +        expect(generatedType).toMatchSnapshot(); | 
|  | 183 | +      } | 
| 329 | 184 |     }); | 
| 330 | 185 |   }); | 
| 331 | 186 | }); | 
0 commit comments