const mocha = require('mocha')
const chai = require('chai')
const expect = chai.expect

const Martinetto = require('../src/routeParser')
const parseRoute = Martinetto.parseRoute

context('Route parser', () => {

  describe('Route with named parameters', () => {
    const route = parseRoute('/users/:username/lists/:title')

    it('should match valid routes and parse query params', () => {
      const path = '/users/janne/lists/2015?filter=name&criteria=Aphex%20Twin'
      const result = route(path)
      expect(result).to.exist
      expect(result.path).to.equal('/users/janne/lists/2015')
      expect(result.pathParams.username).to.equal('janne')
      expect(result.pathParams.title).to.equal('2015')
      expect(result.queryParams).to.exist
      expect(result.queryParams.filter).to.equal('name')
      expect(result.queryParams.criteria).to.equal('Aphex Twin')
    })

    it('should not match invalid named parameter route', () => {
      const path = '/users/janne/artists/Aphex%20Twin'
      const result = route(path)
      expect(result).to.be.null
    })

    it('should consider route to be the same with or without trailing slash', () => {
      const pathWithSlash = '/users/joe/lists/rock-anthems'
      const result = route(pathWithSlash)
      expect(result).to.exist
      expect(result.path).to.equal(pathWithSlash)
      expect(result.pathParams.username).to.equal('joe')
      expect(result.pathParams.title).to.equal('rock-anthems')
    })

    it('should URI decode parameters', () => {
      const path = '/users/John%20Doe/lists/%C3%84%C3%A4ni%C3%A4'
      const result = route(path)
      expect(result).to.exist
      expect(result.path).to.equal(path)
      expect(result.pathParams.username).to.equal('John Doe')
      expect(result.pathParams.title).to.equal('Ääniä')
    })

  })

  describe('Route with wildcards', () => {

    it('should provide wildcard path param as the first array element', () => {
      const route = parseRoute('/rest/*')
      const path = '/rest/artists/Deerhunter'
      const result = route(path)
      expect(result).to.exist
      expect(Object.keys(result.pathParams).length).to.equal(0)
      expect(result.wildcards.length).to.equal(1)
      expect(result.wildcards[0]).to.equal('artists/Deerhunter')
    })

    it('should consider route to be the same with or without single trailing slash', () => {
      const route = parseRoute('/rest/*/')
      const path = '/rest/artists/Deerhunter'
      const result = route(path)
      expect(result).to.exist
      expect(Object.keys(result.pathParams).length).to.equal(0)
      expect(result.wildcards.length).to.equal(1)
      expect(result.wildcards[0]).to.equal('artists/Deerhunter')  })

      it('should support multiple wildcard params', () => {
        const route = parseRoute('/files/*/filter/*')
        const path = '/files/photos/2015/spain/filter/Barcelona/Monday'
        const result = route(path)
        expect(result).to.exist
        expect(result.wildcards.length).to.equal(2)
        expect(result.wildcards[0]).to.equal('photos/2015/spain')
        expect(result.wildcards[1]).to.equal('Barcelona/Monday')
      })

      it('should support url encoded wildcard params', () => {
        const route = parseRoute('/rest/*')
        const path = '/rest/artist/Sigur%20R%C3%B3s'
        const result = route(path)
        expect(result).to.exist
        expect(result.wildcards.length).to.equal(1)
        expect(result.wildcards[0]).to.equal('artist/Sigur Rós')
      })


    })

  })

@@ -97,16 +97,22 @@
 rtinetto
+Parser
  = requi
@@ -165,16 +165,22 @@
 rtinetto
+Parser
 .parseRo
@@ -184,16 +184,106 @@
 eRoute%0A%0A
+const MartinettoRouter = require('../src/main')%0Aconst routing = MartinettoRouter.routing%0A%0A
 context(
@@ -3608,20 +3608,651 @@
   %7D)%0A%0A%0A    %7D)%0A%0A  %7D)%0A
+%0A  context('Routing', () =%3E %7B%0A    describe('Routing with descending priority route list', () =%3E %7B%0A      const routes = %5B%0A        %7B route: '/artists/:name/album/:albumName', fn: () =%3E 'Album page' %7D,%0A        %7B route: '/artists/:name/*', fn: () =%3E 'Artist page' %7D%0A      %5D%0A%0A      const router = routing(routes)%0A%0A      it('should match routes in order', () =%3E %7B%0A        const routingResult1 = router('/artists/Aphex%2520Twin/album/Syro')%0A        expect(routingResult1).to.equal('Album page')%0A%0A        const routingResult2 = router('/artists/Aphex%2520Twin/gigs')%0A        expect(routingResult2).to.equal('Artist page')%0A      %7D)%0A    %7D)%0A  %7D)%0A

Add first test for route matching