Skip to content

Instantly share code, notes, and snippets.

@xnuk
Created August 23, 2014 16:52
Show Gist options
  • Save xnuk/6a73755d7b7d9321e4cf to your computer and use it in GitHub Desktop.
Save xnuk/6a73755d7b7d9321e4cf to your computer and use it in GitHub Desktop.
"이렇게 이렇게 하는 거야!" "우와 굉장하다 그거 어떻게 돌아가는 거야?" "그건 이제 네가 생각해야지"
require('pretty-error').start().appendStyle({
'pretty-error > trace > item > footer > addr': {'display': 'none'},
'pretty-error > trace > item > header > pointer > line': {'color': 'bright-cyan'},
'pretty-error > trace > item > header > pointer > colon': {'color': 'cyan'},
'pretty-error > trace > item > header > pointer > file': {'color': 'bright-cyan'},
'pretty-error > trace > item': {'marginLeft': 0, 'bullet': '', 'display': 'none'}
})
function parse(a){
// Your code goes here
}
try{describe}catch(e){return}
require('should')
describe('parse', function(){
describe('single', function(){
var a=(/([.*+?^${}()|\[\]\/\\])/+'').replace(/^\/|\/$/g,'')
var alphabet='abcdefghijklmnopqrstuvwxyz'
it('regex with no flags', function(done){
parse(/([.*+?^${}()|\[\]\/\\])/+'').should.eql(/([.*+?^${}()|\[\]\/\\])/)
done()
})
it('regex with one flag', function(done){
var b=new RegExp(a, 'i')
parse(b+'').should.eql(b)
b=new RegExp(a, 'm')
parse(b+'').should.eql(b)
b=new RegExp(a, 'g')
parse(b+'').should.eql(b)
done()
})
it('regex with the flags', function(done){
var b=new RegExp(a, 'im')
parse(b+'').should.eql(b)
b=new RegExp(a, 'ig')
parse(b+'').should.eql(b)
b=new RegExp(a, 'mg')
parse(b+'').should.eql(b)
b=new RegExp(a, 'img')
parse(b+'').should.eql(b)
})
describe('should throw error if a regex', function(){
var a=/([.*+?^${}()|\[\]\/\\])/
it('has a wrong flag', function(done){
alphabet.replace(/i|m|g/g, '').split('').forEach(function(v){parse(a+v).should.Error})
done()
})
it('has 2 flags with wrong flag(s)', function(done){
var b=[Math.random()*26|0, Math.random()*26|0]
while(b.every(function(v){return v===8 || v===12 || v===6}))b=[Math.random()*26|0, Math.random()*26|0]
parse(a+b.map(function(v){return alphabet[v]}).join('')).should.Error
done()
})
it('has 3 flags with wrong flag(s)', function(done){
var b=[Math.random()*26|0, Math.random()*26|0, Math.random()*26|0]
while(b.every(function(v){return v===8 || v===12 || v===6}))b=[Math.random()*26|0, Math.random()*26|0, Math.random()*26|0]
parse(a+b.map(function(v){return alphabet[v]}).join('')).should.Error
done()
})
it('is wrong', function(done){
parse("/\\([.*+?^${}()|\\[\\]\\/\\])/").should.Error
done()
})
})
})
describe('#Complex:', function(){
var smpl=["\"([^\\\\\"]|\\\\.)*\"|'([^\\\\']|\\\\.)*'", "\\/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+\\/",
"(?:\\/\\/|#).*(?=\\r\\n|\\n|$)", "\\r?\\n"]
it('AND command', function(done){
var a=new RegExp(smpl[0],'i'), b=new RegExp(smpl[2],'')
parse(a+' and '+b)[0].should.eql('&')
parse(a+' and '+b).sort().should.eql(['&', a, b].sort())
parse(a+' aNd '+b).sort().should.eql(['&', a, b].sort())
parse(a+' AND '+b).sort().should.eql(['&', a, b].sort())
parse(a+'and'+b).sort().should.eql(['&', a, b].sort())
parse(a+'anD'+b).sort().should.eql(['&', a, b].sort())
parse(a+'AND'+b).sort().should.eql(['&', a, b].sort())
parse(/ and /+' and '+/ or /).sort().should.eql(['&', / and /, / or /].sort())
var c=new RegExp(smpl[3], '')
parse(a+' and '+c+' and '+b+' and '+c).sort().should.eql(['&', a, b, c].sort())
})
it('OR command', function(done){
var a=new RegExp(smpl[1],''), b=new RegExp(smpl[2],'')
parse(a+' and '+b)[0].should.eql('|')
parse(a+' or '+b).sort().should.eql(['|', a, b].sort())
parse(a+' oR '+b).sort().should.eql(['|', a, b].sort())
parse(a+' OR '+b).sort().should.eql(['|', a, b].sort())
parse(a+'or'+b).sort().should.eql(['|', a, b].sort())
parse(a+'oR'+b).sort().should.eql(['|', a, b].sort())
parse(a+'OR'+b).sort().should.eql(['|', a, b].sort())
parse(/ or /+' or '+/ and /).sort().should.eql(['|', / or /, / and /].sort())
var c=new RegExp(smpl[3], '')
parse(a+' and '+c+' and '+b+' and '+c).sort().should.eql(['|', a, b, c].sort())
})
//it('should not have')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment