-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManifestHandler.spec.ts
More file actions
154 lines (128 loc) · 5.75 KB
/
ManifestHandler.spec.ts
File metadata and controls
154 lines (128 loc) · 5.75 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { expect } from 'chai'
import ManifestHandler from '../src/lib/ManifestHandler'
import { SEM_VER_REGEX } from '../src/validators'
import invalidSemVers from './fixtures/sem-vers/invalid-sem-vers.json'
import validSemVers from './fixtures/sem-vers/valid-sem-vers.json'
describe('ManifestHandler', () => {
const manifest = {
version: '1.0.0',
name: 'sample function',
description: 'some description',
inputs: [{ firstStaticNumber: 'uint8' }, { secondStaticNumber: 'uint8' }],
abis: [{ ERC20: './abis/ERC20.json' }],
}
describe('validate', () => {
context('when the manifest is valid', () => {
context('when everything is present', () => {
it('returns the parsed manifest', () => {
for (const version of validSemVers) {
const parsedManifest = ManifestHandler.validate({ ...manifest, version })
expect(parsedManifest).to.not.be.undefined
expect(Array.isArray(parsedManifest.inputs)).to.be.false
expect(Array.isArray(parsedManifest.abis)).to.be.false
}
})
})
context('when dealing with inputs', () => {
context('when inputs have descriptions', () => {
it('returns the parsed manifest with described inputs', () => {
const manifestWithDescriptions = {
...manifest,
inputs: [
{ staticNumber: 'uint32' },
{ describedNumber: { type: 'uint32', description: 'A number with description' } },
],
}
const parsedManifest = ManifestHandler.validate(manifestWithDescriptions)
expect(parsedManifest).to.not.be.undefined
expect(parsedManifest.inputs.staticNumber).to.equal('uint32')
expect(parsedManifest.inputs.describedNumber).to.deep.equal({
type: 'uint32',
description: 'A number with description',
})
})
})
context('when inputs do not have descriptions', () => {
it('returns the parsed manifest with simple type inputs', () => {
const parsedManifest = ManifestHandler.validate(manifest)
expect(parsedManifest).to.not.be.undefined
expect(parsedManifest.inputs.firstStaticNumber).to.equal('uint8')
expect(parsedManifest.inputs.secondStaticNumber).to.equal('uint8')
})
})
context('when inputs is missing', () => {
it('returns the parsed manifest', () => {
const parsedManifest = ManifestHandler.validate({ ...manifest, inputs: undefined })
expect(parsedManifest).to.not.be.undefined
expect(Array.isArray(parsedManifest.inputs)).to.be.false
expect(Array.isArray(parsedManifest.abis)).to.be.false
})
})
})
context('when dealing with runner target version', () => {
context('when the runner target version is not present', () => {
it('adds the runner target version to the manifest', () => {
const parsedManifest = ManifestHandler.validate(manifest)
expect(parsedManifest.metadata.runnerTarget).to.match(SEM_VER_REGEX)
})
})
context('when the lib version is present', () => {
it('overrides the lib version', () => {
const libVersion = '999.9.9'
const manifestWithLibVersion = { ...manifest, metadata: { libVersion } }
const parsedManifest = ManifestHandler.validate(manifestWithLibVersion)
expect(parsedManifest.metadata.libVersion).to.not.equal(libVersion)
})
})
})
context('when abis is missing', () => {
it('returns the parsed manifest', () => {
const parsedManifest = ManifestHandler.validate({ ...manifest, abis: undefined })
expect(parsedManifest).to.not.be.undefined
expect(Array.isArray(parsedManifest.inputs)).to.be.false
expect(Array.isArray(parsedManifest.abis)).to.be.false
})
})
})
context('when the manifest is not valid', () => {
const itReturnsAnError = (m, ...errors) => {
it('returns an error', () => {
for (const error of errors) expect(() => ManifestHandler.validate(m)).to.throw(error)
})
}
context('when the inputs are not unique', () => {
itReturnsAnError({ ...manifest, inputs: [{ first: 1 }, { first: 2 }, { second: 3 }] }, 'Duplicate Entry')
})
context('when the abis are not unique', () => {
itReturnsAnError(
{ ...manifest, abis: [{ first: 'first' }, { first: 'something' }, { second: 'second' }] },
'Duplicate Entry'
)
})
context('when there is more than one entry on inputs', () => {
itReturnsAnError({ ...manifest, inputs: [{ first: 2, second: 4 }] }, 'More than one entry')
})
context('when there is more than one entry on abis', () => {
itReturnsAnError({ ...manifest, abis: [{ first: 'first', second: 'second' }] }, 'More than one entry')
})
context('when the version is invalid', () => {
it('returns an error', () => {
for (const version of invalidSemVers)
expect(() => ManifestHandler.validate({ ...manifest, version })).to.throw('Must be a valid semver')
})
})
context('when an input is invalid', () => {
itReturnsAnError(
{ ...manifest, inputs: [...manifest.inputs, { wrong: 'u8' }] },
'Must be a valid solidity type'
)
})
context('when the name is empty', () => {
itReturnsAnError({ ...manifest, name: '' }, 'String must contain at least 1 character(s)')
})
context('when the name is missing', () => {
itReturnsAnError({ ...manifest, name: undefined }, 'Required', 'name')
})
})
})
})