Skip to content

Commit

Permalink
Refactored placeholder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n4kz committed Oct 5, 2016
1 parent 01fc30d commit 8857eb1
Showing 1 changed file with 132 additions and 142 deletions.
274 changes: 132 additions & 142 deletions t/15-placeholders.coffee
Expand Up @@ -5,154 +5,144 @@ Crixalis = require '../lib'
port = +process.env.CRIXALIS_PORT + 15

Crixalis
.start 'http', port
.start('http', port)
.unref()

# TODO: Indirect access
Crixalis.view = 'json'

request = (options) ->
options.host ?= 'localhost'
options.port ?= port
options.path ?= '/'
options.method ?= 'GET'

return ->
fetch options, @callback
return

dummy = ->
matches = (expected) ->
return (error, response) ->
data = JSON.parse(response.body)

parts = ['alpha', 'bravo4_03', '_-_5690_i-', '+', '55']
assert.equal expected.code, response.statusCode
assert.deepEqual expected.data, JSON.parse(response.body)

# TODO: Indirect access
defineRoute = (route) ->
Crixalis.route route, ->
@params.route = String(route)
@stash.json = @params

@render()

defineRoute route for route in [
'/',
'/static',
'/static/:action',
'/static/*',
'/:item',
'/:item/:action',
'/:item/*',
'*'
]

(require 'vows')
.describe('shortcuts')
.describe('placeholders')
.addBatch
placeholders:
topic: 'placeholders'

one: ->
route = Crixalis
.route('/:item/list', dummy)
._routes.pop()[0]

for item in parts
context =
params: {}
url: "/#{item}/list"

assert route.match context
assert.equal context.params.item, item

two: ->
route = Crixalis
.route('/:item/:action', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context =
params: {}
url: "/#{item}/#{action}"

assert route.match context
assert.equal context.params.item, item
assert.equal context.params.action, action

escape: ->
route = Crixalis
.route('/([^\/]+)/:action', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context = url: "/#{item}/#{action}"

assert not route.match context

invalid: ->
route = Crixalis
.route('/([^\\//:action', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context =
url: "/#{item}/#{action}"

assert not route.match context

negative: ->
route = Crixalis
.route('/test/:item', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context =
url : "/#{item}/#{action}"
params : {}

assert not route.match context

assert not route.match url: '/test/file.jpg', params: {}
assert route.match url: '/test/file', params: {}

'asterisk#middle': ->
route = Crixalis
.route('/*/alpha', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context = url: "/#{item}/#{action}"

if action is 'alpha'
assert route.match context
else
assert not route.match context

'asterisk#end': ->
route = Crixalis
.route('/alpha/*', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context = url: "/#{item}/#{action}"

if item is 'alpha'
assert route.match context
else
assert not route.match context

'asterisk#double': ->
route = Crixalis
.route('/*/test/*', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context = url: "/#{item}/test/#{action}"

assert route.match context

assert not route.match url: '/notesthere'
assert not route.match url: '/abc/test/'
assert not route.match url: '//test/abc'

everything: ->
route = Crixalis
.route('*', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context = url: "/#{item}/#{action}"

assert route.match context

tail: ->
route = Crixalis
.route('*/:tail', dummy)
._routes.pop()[0]

for item in parts
for action in parts
context =
params : {}
url : "/#{item}/#{action}"

assert route.match context
assert.equal context.params.tail, action
root:
topic: request
path: '/'

result: matches
code: 200
data:
route: '/'

static:
topic: request
path: '/static'

result: matches
code: 200
data:
route: '/static'

action:
topic: request
path: '/static/index'

result: matches
code: 200
data:
route: '/static/:action'
action: 'index'

extension:
topic: request
path: '/static/index.txt'

result: matches
code: 200
data:
route: '/static/*'

wildcard:
topic: request
path: '/static/files/index.txt'

result: matches
code: 200
data:
route: '/static/*'

placeholder:
topic: request
path: '/index'

result: matches
code: 200
data:
route: '/:item'
item: 'index'

placeholder:
topic: request
path: '/index/files'

result: matches
code: 200
data:
route: '/:item/:action'
item: 'index'
action: 'files'

extension:
topic: request
path: '/index/files.txt'

result: matches
code: 200
data:
route: '/:item/*'
item: 'index'

wildcard:
topic: request
path: '/index/files/list.txt'

result: matches
code: 200
data:
route: '/:item/*'
item: 'index'

extension:
topic: request
path: '/index.txt'

result: matches
code: 200
data:
route: '*'

.export(module)

0 comments on commit 8857eb1

Please sign in to comment.