|
| 1 | +'use strict' |
| 2 | + |
| 3 | +constfs=require('fs') |
| 4 | +constpath=require('path') |
| 5 | +constassert=require('assert') |
| 6 | +constSRL=require('../') |
| 7 | + |
| 8 | +functiontestRules(){ |
| 9 | +construlesDir=path.resolve(__dirname,'./rules') |
| 10 | +constfiles=fs.readdirSync(rulesDir) |
| 11 | + |
| 12 | +files.forEach((file)=>{ |
| 13 | +// Ignore |
| 14 | +if(path.extname(file)!=='.rule'){ |
| 15 | +return |
| 16 | +} |
| 17 | + |
| 18 | +constlines=fs.readFileSync(path.join(rulesDir,file),{encoding:'utf-8'}).split('\n') |
| 19 | +runAssertions(buildData(lines)) |
| 20 | +}) |
| 21 | +} |
| 22 | + |
| 23 | +functionapplySpecialChars(target){ |
| 24 | +returntarget.replace('\\n','\n').replace('\\t','\t') |
| 25 | +} |
| 26 | + |
| 27 | +functiongetExpression(srl,query){ |
| 28 | +return`\n\nSupplied SRL Query:${srl}\nGenerated Expression:${query.getRawRegex()}\n\n` |
| 29 | +} |
| 30 | + |
| 31 | +functionbuildData(lines){ |
| 32 | +constdata={ |
| 33 | +srl:null, |
| 34 | +matches:[], |
| 35 | +no_matches:[], |
| 36 | +captures:{} |
| 37 | +} |
| 38 | +letinCapture=false |
| 39 | +letcaptures=null// Remember captures' name and index. |
| 40 | + |
| 41 | +lines.forEach((line)=>{ |
| 42 | +if(line===''||line.startsWith('#')){ |
| 43 | +return |
| 44 | +} |
| 45 | + |
| 46 | +if(inCapture&&!line.startsWith('-')){ |
| 47 | +inCapture=false |
| 48 | +} |
| 49 | + |
| 50 | +if(line.startsWith('srl: ')){ |
| 51 | +captures=[] |
| 52 | + |
| 53 | +data.srl=line.substr(5).replace(/as\s+"([^"]+)"/g,(s,c)=>{ |
| 54 | +captures.push(c) |
| 55 | +return'' |
| 56 | +}) |
| 57 | +}elseif(line.startsWith('match: "')){ |
| 58 | +data.matches.push(applySpecialChars(line.slice(8,-1))) |
| 59 | +}elseif(line.startsWith('no match: "')){ |
| 60 | +data.no_matches.push(applySpecialChars(line.slice(11,-1))) |
| 61 | +}elseif( |
| 62 | +line.startsWith('capture for "')&& |
| 63 | +line.substr(-2,2)==='":' |
| 64 | +){ |
| 65 | +inCapture=line.slice(13,-2) |
| 66 | +data['captures'][inCapture]=[] |
| 67 | +}elseif( |
| 68 | +inCapture&& |
| 69 | +line.startsWith('-') |
| 70 | +){ |
| 71 | +constsplit=line.substr(1).split(': ') |
| 72 | +constindex=captures.indexOf(split[1].trim()) |
| 73 | +lettarget=data['captures'][inCapture][Number(split[0])] |
| 74 | + |
| 75 | +if(!target){ |
| 76 | +target=data['captures'][inCapture][Number(split[0])]=[] |
| 77 | +} |
| 78 | + |
| 79 | +if(index!==-1){ |
| 80 | +target[index]=applySpecialChars(split[2].slice(1,-1)) |
| 81 | +}else{ |
| 82 | +target.push(applySpecialChars(split[2].slice(1,-1))) |
| 83 | +} |
| 84 | +} |
| 85 | +}) |
| 86 | + |
| 87 | +returndata |
| 88 | +} |
| 89 | + |
| 90 | +functionrunAssertions(data){ |
| 91 | +assert(data.srl,'SRL for rule is empty. Invalid rule.') |
| 92 | + |
| 93 | +letquery,assertionMade=false |
| 94 | + |
| 95 | +try{ |
| 96 | +query=newSRL(data.srl) |
| 97 | +}catch(e){ |
| 98 | +assert(false,`Parser error:${e.message}\n\nSupplied SRL Query:${data.srl}\n\n`) |
| 99 | +} |
| 100 | + |
| 101 | +data.matches.forEach((match)=>{ |
| 102 | +assert( |
| 103 | +query.test(match), |
| 104 | +`Failed asserting that this query matches '${match}'.${getExpression(data.srl,query)}` |
| 105 | +) |
| 106 | +assertionMade=true |
| 107 | +}) |
| 108 | + |
| 109 | +data.no_matches.forEach((noMatch)=>{ |
| 110 | +assert( |
| 111 | +!query.test(noMatch), |
| 112 | +`Failed asserting that this query does not match '${noMatch}'.${getExpression(data.srl,query)}` |
| 113 | +) |
| 114 | +assertionMade=true |
| 115 | +}) |
| 116 | + |
| 117 | +Object.keys(data.captures).forEach((test)=>{ |
| 118 | +constexpected=data.captures[test] |
| 119 | +constmatches=[] |
| 120 | +constregex=query.all() |
| 121 | + |
| 122 | +try{ |
| 123 | +letresult=null |
| 124 | +while(result=regex.exec(test)){ |
| 125 | +matches.push(result.map((item)=>item===undefined ?'' :item).slice(1)) |
| 126 | + |
| 127 | +if(regex.lastIndex===test.length-1){ |
| 128 | +break |
| 129 | +} |
| 130 | +} |
| 131 | +}catch(e){ |
| 132 | +assert(false,`Parser error:${e.message}${getExpression(data.srl,query)}`) |
| 133 | +} |
| 134 | + |
| 135 | +assert.equal( |
| 136 | +expected.length, |
| 137 | +matches.length, |
| 138 | +`Invalid match count for test${test}.${getExpression(data.srl,query)}` |
| 139 | +) |
| 140 | + |
| 141 | +matches.forEach((capture,index)=>{ |
| 142 | +assert.deepEqual( |
| 143 | +expected[index], |
| 144 | +capture, |
| 145 | +`The capture group did not return the expected results for test${test}.${getExpression(data.srl,query)}` |
| 146 | +) |
| 147 | +}) |
| 148 | + |
| 149 | +assertionMade=true |
| 150 | +}) |
| 151 | + |
| 152 | +assert(assertionMade,`No assertion. Invalid rule.${getExpression(data.srl,query)}`) |
| 153 | +} |
| 154 | + |
| 155 | +describe('Rules',()=>{ |
| 156 | +testRules() |
| 157 | +}) |