-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestGen.js
More file actions
executable file
·116 lines (95 loc) · 2.86 KB
/
testGen.js
File metadata and controls
executable file
·116 lines (95 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
/**
* Audits.dev - Truffle Test Generator
*/
const fs = require('fs')
const getConstructor = (contract) => {
for (let i = 0; i < contract.abi.length; i++) {
if(contract.abi[i].type == 'constructor') {
return contract.abi[i]
}
}
}
const genConstructorParams = (constructor) => {
let lines = []
if(constructor != undefined) {
for (let i = 0; i < constructor.inputs.length; i++) {
lines.push(constructor.inputs[i].name)
}
}
return lines
}
const genConstructorLine = (constructor) => {
let line = ''
if(constructor != undefined) {
for (let i = 0; i < constructor.inputs.length; i++) {
line += constructor.inputs[i].name + ', '
}
}
line += '{from: maintainer}'
return line
}
const genTestContent = (contract, constructorParams, constructorLine) => {
let testContent = `
/**
* This file was created by truffle-test-generator.
* For every test, a new contract will be created in the
* top beforeEach block. This line uses the arguments for
* your contract's constructor with the same variable names.
* Each public, non-constant (view) method has a describe
* block generated for it.
*/
const ${contract.contractName} = artifacts.require('${contract.contractName}')
contract('${contract.contractName}', (accounts) => {
const maintainer = accounts[0]
const user1 = accounts[1]
const user2 = accounts[2]
const stranger = accounts[3]
let ${contract.contractName.toLowerCase()}`
if(constructorParams.length > 0) {
testContent += `
// Be sure to update these constructor values`
for (let i = 0; i < constructorParams.length; i++) {
testContent += `
let ${constructorParams[i]} = 0`
}
}
testContent += `
beforeEach(async () => {
${contract.contractName.toLowerCase()} = await ${contract.contractName}.new(${constructorLine})
})
`
for (let i = 0; i < contract.abi.length; i++) {
if(contract.abi[i].type == 'function') {
testContent += `
describe('${contract.abi[i].name}', () => {
})
`
}
}
testContent +=
`})
`
return testContent
}
const cwd = process.cwd()
const jsonFile = cwd + `/build/contracts/${process.argv[2]}.json`
let testFile = cwd + `/test/${process.argv[2]}_test.js`
let contract
try {
contract = JSON.parse(fs.readFileSync(jsonFile, 'utf8'))
} catch (err) {
console.error('Please provide a contract name and be in the top-level directory of your Truffle project\n', err)
return err
}
const constructor = getConstructor(contract)
const constructorParams = genConstructorParams(constructor)
const constructorLine = genConstructorLine(constructor)
const testContent = genTestContent(contract, constructorParams, constructorLine)
if(fs.existsSync(testFile)) {
testFile = testFile.replace('.js', '-new.js')
}
fs.writeFile(testFile, testContent, (error) => {
if(error) return console.error(error)
})
console.log('Wrote to:', testFile)