提交 079774ea 编写于 作者: Y yylgit

interface-parser unit test

上级 84b1201e
......@@ -753,5 +753,23 @@ describe('index.js', function () {
expect(result).to.be.equal('/user/cml/name.web_3123123123123sd.cml');
})
it(`resolveSync relativePath`, function () {
let filePath = path.join(__dirname, 'index.test.js');
let relativePath = './testlib/index.cml';
let result = _.resolveSync(filePath, relativePath);
expect(result).to.be.equal(path.join(__dirname, './testlib/index.cml'));
})
it(`resolveSync npmPath`, function () {
let filePath = path.join(__dirname, 'index.test.js');
let relativePath = 'glob';
let result = _.resolveSync(filePath, relativePath);
console.log(result)
expect(!!~result.indexOf('glob')).to.be.equal(true);
})
})
......@@ -3,14 +3,9 @@ const cmlUtils = require('chameleon-tool-utils');
const {getCode} = require('./lib/check.js');
const getInterfaceCode = require('./lib/getInterfaceCode.js');
const getMethodCode = require('./lib/getMethodCode.js');
const path = require('path');
const defaultResolve = function(filePath, relativePath) {
return path.resolve(path.dirname(filePath), relativePath)
}
// resolve 用于处理interface中include文件中的引用
module.exports = function({cmlType, media, source, filePath, check, resolve = defaultResolve }) {
module.exports = function({cmlType, media, source, filePath, check, resolve = cmlUtils.resolveSync }) {
let interfaceResut = getInterfaceCode({interfacePath: filePath, content: source})
let methodResult = getMethodCode({interfacePath: filePath, content: source, cmlType, resolve})
......
......@@ -43,6 +43,7 @@ const handlExport = function (ast) {
* @param {Object} obj 需要处理的对象
* @return {Object} 对象
*/
/* istanbul ignore next */
const wrapper = function (obj) {
const className = obj.constructor.name;
/* eslint-disable no-undef */
......
......@@ -3,7 +3,8 @@
"version": "0.4.0-mvvm.6",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"cover": "istanbul cover --report lcov _mocha -- -R spec --recursive",
"test": "mocha --recursive --reporter spec"
},
"author": "Chameleon-Team",
"license": "Apache",
......@@ -15,5 +16,11 @@
"@babel/types": "^7.3.4",
"chameleon-tool-utils": "0.4.0-mvvm.6",
"runtime-check": "0.4.0-mvvm.6"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^5.9.0",
"istanbul": "^0.4.5",
"mocha": "^5.2.0"
}
}
\ No newline at end of file
const {getCode} = require('../lib/check.js');
const expect = require('chai').expect;
let code = `
interface Interface1Interface {
getMsg(msg: String): String;
}
class Method implements Interface1Interface {
getMsg(msg) {
return 'web:' + msg;
}
}
export default new Method();
`
let result = getCode(code,{
cmlType: 'wx',
filePath: '/user/name.cml',
enableTypes: []
})
describe('check.js wx', function() {
it('getCode', function() {
expect(!!~result.indexOf('export default __OBJECT__WRAPPER__')).to.be.equal(true);
})
})
let result2 = getCode(code,{
cmlType: 'weex',
filePath: '/user/name.cml',
enableTypes: []
})
describe('check.js weex', function() {
it('getCode', function() {
expect(!!~result2.indexOf('export default __OBJECT__WRAPPER__')).to.be.equal(true);
})
})
const getInterfaceCode = require('../lib/getInterfaceCode.js');
const path = require('path');
const fs = require('fs');
const interfacePath = path.join(__dirname, './lib/components/second/second.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getInterfaceCode({interfacePath, content});
const expect = require('chai').expect;
const getInterfaceCode = require('../lib/getInterfaceCode.js');
const path = require('path');
const fs = require('fs');
describe('mvvm-interface-parser/getInterfaceCode', function() {
it('getInterfaceCode', function() {
const interfacePath = path.join(__dirname, './lib/components/second/second.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getInterfaceCode({interfacePath, content});
let firstInterfacePath = path.join(__dirname, './lib/components/first/first.interface')
expect(result1.content).to.be.equal('\ninterface FirstInterface {\n getMsg(msg: String): String;\n}\n\n')
expect(result1.contentFilePath).to.be.equal(firstInterfacePath)
expect(!!~result1.devDeps.indexOf(firstInterfacePath)).to.be.equal(true)
})
it('getInterfaceCode src', function() {
const interfacePath = path.join(__dirname, './lib/components/third.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getInterfaceCode({interfacePath, content});
let contentPath = path.join(__dirname, './lib/components/thirdinterface.js')
expect(result1.contentFilePath).to.be.equal(contentPath)
expect(!!~result1.devDeps.indexOf(contentPath)).to.be.equal(true)
console.log(result1)
})
})
const getMethodCode = require('../lib/getMethodCode.js');
const path = require('path');
const fs = require('fs');
const interfacePath = path.join(__dirname, './lib/components/second/second.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getMethodCode({interfacePath, content, cmlType: 'web', resolve: function(filePath, relativePath) {
return path.resolve(path.dirname(filePath), relativePath)
}});
console.log(result1.devDeps)
const expect = require('chai').expect;
const getMethodCode = require('../lib/getMethodCode.js');
const path = require('path');
const fs = require('fs');
const cmlUtils = require('chameleon-tool-utils')
describe('mvvm-interface-parser/getMethodCode', function() {
it('getMethodCode', function() {
const interfacePath = path.join(__dirname, './lib/components/second/second.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getMethodCode({interfacePath, content, cmlType: 'web', resolve: cmlUtils.resolveSync});
let firstInterfacePath = path.join(__dirname, './lib/components/first/first.interface');
expect(result1.contentFilePath).to.be.equal(firstInterfacePath)
expect(!!~result1.devDeps.indexOf(firstInterfacePath)).to.be.equal(true)
expect(!!~result1.content.indexOf('FirstInterface')).to.be.equal(true);
})
it('getMethodCode src', function() {
const interfacePath = path.join(__dirname, './lib/components/third.interface');
const content = fs.readFileSync(interfacePath, {encoding: 'utf-8'})
let result1 = getMethodCode({interfacePath, content, cmlType: 'weex', resolve: cmlUtils.resolveSync});
let contentPath = path.join(__dirname, './lib/components/thirdmethod.js');
expect(result1.contentFilePath).to.be.equal(contentPath)
expect(!!~result1.devDeps.indexOf(contentPath)).to.be.equal(true)
expect(!!~result1.content.indexOf('thirdmethods')).to.be.equal(true);
})
})
<script cml-type="interface" src="./thirdinterface.js"></script>
<script cml-type="web">
import test1 from './test1.js';
const requireModule = require('./test1.js');
import cml from 'chameleon-api';
class Method implements FirstInterface {
getMsg(msg) {
return 'first web:' + msg;
}
}
export default new Method();
</script>
<script cml-type="weex" src="./thirdmethod.js">
</script>
`interface FirstInterface {
getMsg(msg: String): String;
}`
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册