|
1
|
|
|
import { expect } from 'chai'; |
|
2
|
|
|
import { |
|
3
|
|
|
compileModulesFromSource, |
|
4
|
|
|
evaluateModules, |
|
5
|
|
|
inspect |
|
6
|
|
|
} from '../src/module'; |
|
7
|
|
|
import { createVM } from '../src/vm'; |
|
8
|
|
|
|
|
9
|
|
|
describe('module.js', () => { |
|
10
|
|
|
describe('Module compilation', () => { |
|
11
|
|
|
it('should compile and evaluate modules', () => { |
|
12
|
|
|
const modules = [ |
|
13
|
|
|
['drag-queen.json', JSON.stringify({ |
|
14
|
|
|
alaska: 1, |
|
15
|
|
|
courtney: 2 |
|
16
|
|
|
})], |
|
17
|
|
|
['workaround.js', ` |
|
18
|
|
|
module.exports = 42; |
|
19
|
|
|
`] |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
const vm = createVM({}); |
|
23
|
|
|
const result = evaluateModules(vm, modules); |
|
24
|
|
|
expect(result).to.be.an('object'); |
|
25
|
|
|
expect(result).to.have.property('drag-queen.json'); |
|
26
|
|
|
expect(result).to.have.property('workaround.js'); |
|
27
|
|
|
expect(result).property('workaround.js').to.equals(42); |
|
28
|
|
|
expect(result).property('drag-queen.json').to.have.property('alaska'); |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
it('should get module files from AST', () => { |
|
32
|
|
|
const { code, modules } = inspect(` |
|
33
|
|
|
import jquery from 'jquery'; |
|
34
|
|
|
import isThirteen from 'is-thirteen'; |
|
35
|
|
|
import workarounds from './gambiarras'; |
|
36
|
|
|
`); |
|
37
|
|
|
|
|
38
|
|
|
expect(code).to.be.a('string'); |
|
39
|
|
|
expect(modules).to.be.an('array'); |
|
40
|
|
|
expect(modules).to.have.lengthOf(3); |
|
41
|
|
|
expect(modules).to.contain('jquery'); |
|
42
|
|
|
expect(modules).to.contain('is-thirteen'); |
|
43
|
|
|
expect(modules).to.contain('./gambiarras'); |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
it('should reject when required file has no loader', () => { |
|
47
|
|
|
const source = 'import config from "./_config.yml"'; |
|
48
|
|
|
return compileModulesFromSource(source) |
|
49
|
|
|
.then((x) => { |
|
50
|
|
|
throw new Error('Should never fall here'); |
|
51
|
|
|
}) |
|
52
|
|
|
.catch(({ message }) => { |
|
53
|
|
|
expect(message).to.match(/Unknown module loader for file/); |
|
54
|
|
|
}); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('should reject compilation of unknown file', () => { |
|
58
|
|
|
const modules = [ |
|
59
|
|
|
['Main.hs', ` |
|
60
|
|
|
module Main where |
|
61
|
|
|
|
|
62
|
|
|
main :: IO () |
|
63
|
|
|
main = putStrLn "Hello from Haskell!" |
|
64
|
|
|
`] |
|
65
|
|
|
]; |
|
66
|
|
|
const vm = createVM({}); |
|
67
|
|
|
expect(() => evaluateModules(vm, modules)) |
|
68
|
|
|
.to.throw('Unknown file type for Main.hs'); |
|
69
|
|
|
}); |
|
70
|
|
|
}); |
|
71
|
|
|
}); |