Skip to content

Commit

Permalink
sandbox, feat: add test cases for internal typescript. (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016 authored and xicilion committed Jun 5, 2018
1 parent 50555be commit 29e3901
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
15 changes: 15 additions & 0 deletions test/ts_files/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { add, hello } from './basic'

export default class Foo {
static bar = 1;

static bar2 = 'bar';

foo1(...args) {
return add(...args)
}

foo2() {
return hello()
}
}
5 changes: 5 additions & 0 deletions test/ts_files/wrong_syntax1.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// #!/usr/bin/fibjs env

exp ort function add(a: number, b: number) {
return a + b
}
5 changes: 5 additions & 0 deletions test/ts_files/wrong_syntax1.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// #!/usr/bin/fibjs env

export fun ction add(a: number, b: number) {
return a + b
}
5 changes: 5 additions & 0 deletions test/ts_files/wrong_syntax1.3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// #!/usr/bin/fibjs env

export function ad d(a: number, b: number) {
return a + b
}
9 changes: 9 additions & 0 deletions test/ts_files/wrong_syntax1.4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #!/usr/bin/fibjs env

export function add(a: number, b: number) {
return a + b
}

export function ht tp() {
return 'http'
}
9 changes: 9 additions & 0 deletions test/ts_files/wrong_syntax2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #!/usr/bin/fibjs env

export function add(a: number, b: number) {
return a + b
}

export function add() {
return 'psudo add'
}
40 changes: 38 additions & 2 deletions test/ts_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ describe('typescript', () => {
export function hello () {
return 'hello, world'
}
`, _getOptions())
`, _getOptions({
inlineSourceMap: true,
alwaysStrict: true
}))
assert.isString(compiledJScript)

const sbox = new vm.SandBox({})
Expand All @@ -62,7 +65,7 @@ describe('typescript', () => {
assertBasicModule(basic)
})

it('sandbox increasing test', () => {
it('increasing/parallel sandbox', () => {
const iterbase = Array(5).fill(undefined);

function testBody() {
Expand All @@ -76,6 +79,39 @@ describe('typescript', () => {
iterbase.forEach(() => testBody());
coroutine.parallel(iterbase, () => testBody());
})

it('wrong syntax', () => {
assert.throws(() => {
const wrongSynTaxt = require('./ts_files/wrong_syntax1.1');
})
assert.throws(() => {
const wrongSynTaxt = require('./ts_files/wrong_syntax1.2');
})
assert.throws(() => {
const wrongSynTaxt = require('./ts_files/wrong_syntax1.3');
})
assert.throws(() => {
const wrongSynTaxt = require('./ts_files/wrong_syntax1.4');
})
const wrongSynTaxt2 = require('./ts_files/wrong_syntax2');
assert.isFunction(wrongSynTaxt2.add)
assert.equal(Object.keys(wrongSynTaxt2).length, 1)
assert.equal(wrongSynTaxt2.add(), 'psudo add')
})

it('class', () => {
const _class = require('./ts_files/class').default
const basic = require('./ts_files/basic')

assert.exist(_class.bar);
assert.exist(_class.bar2);

const ins = new _class()
assert.exist(ins);

assert.equal(ins.foo1(1, 2), basic.add(1, 2))
assert.equal(ins.foo2(), basic.hello())
})
});

require.main === module && test.run(console.DEBUG);
20 changes: 20 additions & 0 deletions test/ts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// / <reference path="fib-types/declare/_test_env.d.ts" />

import * as test from "test";
import _class from './ts_files/class';
test.setup();

describe('typescript', () => {
Expand All @@ -22,6 +23,25 @@ describe('typescript', () => {
const basic = require('./ts_files/basic')
assertBasicModule(basic)
})

it('class', () => {
const basic = require('./ts_files/basic')
assert.throws(() => {
// not exist
const _classModule = require('./ts_files/_class')
})
const _classModule = require('./ts_files/class')
assert.exist(_class, _classModule.default)

assert.exist(_class.bar);
assert.exist(_class.bar2);

const ins = new _class()
assert.exist(ins);

assert.equal(ins.foo1(1, 2), basic.add(1, 2))
assert.equal(ins.foo2(), basic.hello())
})
});

require.main === module && test.run(console.DEBUG);
Expand Down

0 comments on commit 29e3901

Please sign in to comment.