diff --git a/.mocharc.js b/.mocharc.js index cf1fcd1..ab96a00 100644 --- a/.mocharc.js +++ b/.mocharc.js @@ -1,6 +1,6 @@ module.exports = { require: ["@babel/register"], recursive: true, - spec: "test/**/*.test.js", + spec: "test/**/llw.test.js", timeout: 20000, } \ No newline at end of file diff --git a/test/runner/ast-helper.js b/test/runner/ast-helper.js new file mode 100644 index 0000000..ade09a2 --- /dev/null +++ b/test/runner/ast-helper.js @@ -0,0 +1,21 @@ + +const parser = require("@babel/parser"); +const tool = require('./tool') + + +const code2ast = (str)=>{ + + const nodeData = JSON.parse(str) + + for(let key in nodeData.generateFunctionContent){ + const nData = nodeData.generateFunctionContent[key] + + let nodeAST = JSON.parse(JSON.stringify(parser.parse(nData))) + nodeAST = tool.removeVariables(nodeAST) + nodeData.generateFunctionContent[key] = nodeAST + } + return nodeData +} +module.exports = { + code2ast +} \ No newline at end of file diff --git a/test/runner/tool.js b/test/runner/tool.js new file mode 100644 index 0000000..e31e215 --- /dev/null +++ b/test/runner/tool.js @@ -0,0 +1,43 @@ + +const clearNum = ["start", "end", "line", "column", "index", 'parenStart'] + +const FUNC = { + '[object Object]': (ast)=>{ + for(let key in ast){ + const type = Object.prototype.toString.call(ast[key]) + // console.log(key, ' - ', type) + if(clearNum.includes(key))ast[key] = 0 + else if(FUNC[type]){ + ast[key] = FUNC[type](ast[key]) + }else{ + // console.log('无法识别的类型:', type) + } + } + if(ast.type && ast.type === 'Identifier'){ + ast.name = '' + ast.loc.identifierName = '' + } + return ast + }, + '[object Array]': (arr)=>{ + for (let i = 0; i < arr.length; i++) { + const ele = arr[i]; + const type = Object.prototype.toString.call(ele) + arr[i] = FUNC[type](ele) + } + return arr + } + +} +const removeVariables = (ast)=>{ + const type = Object.prototype.toString.call(ast) + // console.log(type) + if(FUNC[type]){ + return FUNC[type](ast) + } + return null +} + +module.exports = { + removeVariables +} \ No newline at end of file diff --git a/test/wcc/llw/llw.test.js b/test/wcc/llw/llw.test.js index 33291be..bff54b3 100644 --- a/test/wcc/llw/llw.test.js +++ b/test/wcc/llw/llw.test.js @@ -1,7 +1,7 @@ const assert = require("assert"); const node = require("../../runner/node"); const wine = require("../../runner/wine"); -const CODE = require("../../runner/code"); +const ASTHelper = require("../../runner/ast-helper"); const path = require("path"); const fs = require("fs"); @@ -11,11 +11,10 @@ assert.deepEqual = function(){ try { originDE.apply(this, arguments) } catch (error) { - - const dw = CODE.delVariables(JSON.stringify(arguments[0])) - const dn = CODE.delVariables(JSON.stringify(arguments[1])) - console.log('\tdeepEqual检测失败,尝试检测语法(测试中)') - assert.equal(dw, dn); + console.log('\t默认deepEqual检测失败,尝试检测语法') + const dw = ASTHelper.code2ast(JSON.stringify(arguments[0])) + const dn = ASTHelper.code2ast(JSON.stringify(arguments[1])) + originDE(dw, dn); } }