Skip to content

Commit

Permalink
Added tests for remaining builtin parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Aug 28, 2012
1 parent c2e3266 commit b99e936
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/routers.coffee
Expand Up @@ -90,14 +90,14 @@ class Compiler
return null
return rv

number: (str, opts) ->
float: (str, opts) ->
str = str.trim()
rv = parseFloat(str)
if !isFinite(rv) || rv.toString() != str
return null
if opts
if (isFinite(opts.min) && rv < min) ||
(isFinite(opts.max) && rv > max)
if (isFinite(opts.min) && rv < opts.min) ||
(isFinite(opts.max) && rv > opts.max)
return null
return rv

Expand Down
50 changes: 50 additions & 0 deletions test/routers.coffee
Expand Up @@ -96,6 +96,56 @@ describe 'Builtin string parser', ->
.expect(404, done)


describe 'Builtin path parser', ->
router = routers()
app = connect()
app.use(router.middleware)

router.get '/pages/<path:page>/edit', (req, res) ->
res.write(req.params.page)
res.end()

it 'should match normal strings', (done) ->
app.request()
.get('/pages/foo/edit')
.end (res) ->
res.body.should.eql('foo')
done()

it 'should match any number of path segments', (done) ->
app.request()
.get('/pages/abc/def/ghi/jkl/edit')
.end (res) ->
res.body.should.eql('abc/def/ghi/jkl')
done()


describe 'Builtin float parser', ->
router = routers()
app = connect()
app.use(router.middleware)

router.post '/credit/<float(max=99.99,min=1):amount>', (req, res) ->
res.write(JSON.stringify(req.params.amount))
res.end()

it 'should match numbers inside range', (done) ->
app.request()
.post('/credit/55.3')
.end (res) ->
JSON.parse(res.body).should.eql(55.3)
done()

it 'should not match numbers outside range', (done) ->
app.request()
.get('/credit/99.999')
.expect 404, ->
app.request()
.get('/credit/0.999')
.expect(404, done)



describe 'Builtin integer parser', ->
router = routers()
app = connect()
Expand Down

0 comments on commit b99e936

Please sign in to comment.