|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import test from 'ava' |
| 21 | +import { insert, Node } from './extract-routes' |
| 22 | + |
| 23 | +test('insertions', t => { |
| 24 | + class Args { |
| 25 | + name: string |
| 26 | + routes: Array<{name: string, path: string}> |
| 27 | + expect: Node |
| 28 | + |
| 29 | + constructor (name: string, routes: Array<{name: string, path: string}>, expect: Node) { |
| 30 | + this.name = name |
| 31 | + this.routes = routes |
| 32 | + this.expect = expect |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const cases: Args[] = [ |
| 37 | + new Args('simple', [{ name: 'search', path: '/_search' }], new Node('/_search', 'search')), |
| 38 | + |
| 39 | + new Args('with subpath', [ |
| 40 | + { name: 'search', path: '/_search' }, |
| 41 | + { name: 'test', path: '/_search/test' } |
| 42 | + ], new Node('/_search', 'search', [new Node('/test', 'test')])), |
| 43 | + |
| 44 | + new Args('with split subpath', [ |
| 45 | + { name: 'search', path: '/_search' }, |
| 46 | + { name: 'test', path: '/_search/test' }, |
| 47 | + { name: 'travel', path: '/_search/travel' } |
| 48 | + ], new Node('/_search', 'search', [ |
| 49 | + new Node('/t', '', [ |
| 50 | + new Node('est', 'test'), |
| 51 | + new Node('ravel', 'travel') |
| 52 | + ]) |
| 53 | + ])), |
| 54 | + |
| 55 | + new Args('with split subpath and children', [ |
| 56 | + { name: 'search', path: '/_search' }, |
| 57 | + { name: 'test', path: '/_search/test' }, |
| 58 | + { name: 'tool', path: '/_search/test/tool' }, |
| 59 | + { name: 'travel', path: '/_search/travel' } |
| 60 | + ], new Node('/_search', 'search', [ |
| 61 | + new Node('/t', '', [ |
| 62 | + new Node('est', 'test', [ |
| 63 | + new Node('/tool', 'tool') |
| 64 | + ]), |
| 65 | + |
| 66 | + new Node('ravel', 'travel') |
| 67 | + ]) |
| 68 | + ]) |
| 69 | + ), |
| 70 | + |
| 71 | + new Args('with split subpath, 2 direct children', [ |
| 72 | + { name: 'search', path: '/_search' }, |
| 73 | + { name: 'tool', path: '/_search/test/tool' }, |
| 74 | + { name: 'travel', path: '/_search/travel/test' } |
| 75 | + ], new Node('/_search', 'search', [ |
| 76 | + new Node('/t', '', [ |
| 77 | + new Node('est/tool', 'tool'), |
| 78 | + new Node('ravel/test', 'travel') |
| 79 | + ]) |
| 80 | + ]) |
| 81 | + ), |
| 82 | + |
| 83 | + new Args('with split subpath and 3 children, 2 indirect', [ |
| 84 | + { name: 'search', path: '/_search' }, |
| 85 | + { name: 'test', path: '/_search/test' }, |
| 86 | + { name: 'tool', path: '/_search/test/tool' }, |
| 87 | + { name: 'travel', path: '/_search/travel/test' } |
| 88 | + ], new Node('/_search', 'search', [ |
| 89 | + new Node('/t', '', [ |
| 90 | + new Node('est', 'test', [ |
| 91 | + new Node('/tool', 'tool') |
| 92 | + ]), |
| 93 | + |
| 94 | + new Node('ravel/test', 'travel') |
| 95 | + ]) |
| 96 | + ]) |
| 97 | + ), |
| 98 | + |
| 99 | + new Args('with root variable', [ |
| 100 | + { name: 'search', path: '/_search' }, |
| 101 | + { name: 'doc', path: '/{index}/_doc' } |
| 102 | + ], new Node('/', '', [ |
| 103 | + new Node('_search', 'search'), |
| 104 | + new Node('{index}', '', [ |
| 105 | + new Node('/_doc', 'doc') |
| 106 | + ], true) |
| 107 | + ]) |
| 108 | + ), |
| 109 | + |
| 110 | + new Args('with two consecutive variables from root', [ |
| 111 | + { name: 'search', path: '/_search' }, |
| 112 | + { name: 'doc', path: '/{a}/{b}/_doc' } |
| 113 | + ], new Node('/', '', [ |
| 114 | + new Node('_search', 'search'), |
| 115 | + new Node('{a}', '', [ |
| 116 | + new Node('/', 'doc', [ |
| 117 | + new Node('{b}', '', [ |
| 118 | + new Node('/_doc', 'doc') |
| 119 | + ], true) |
| 120 | + ]) |
| 121 | + ], true) |
| 122 | + ]) |
| 123 | + ), |
| 124 | + |
| 125 | + new Args('with variable and an ending variable', [ |
| 126 | + { name: 'search', path: '/_search' }, |
| 127 | + { name: 'doc', path: '/{a}/_doc/{c}' } |
| 128 | + ], new Node('/', '', [ |
| 129 | + new Node('_search', 'search'), |
| 130 | + new Node('{a}', '', [ |
| 131 | + new Node('/_doc/', 'doc', [ |
| 132 | + new Node('{c}', 'doc', [], true) |
| 133 | + ]) |
| 134 | + ], true) |
| 135 | + ]) |
| 136 | + ), |
| 137 | + |
| 138 | + new Args('with variable and an ending variable', [ |
| 139 | + { name: 'search', path: '/_search' }, |
| 140 | + { name: 'doc', path: '/{a}/_doc/{c}' } |
| 141 | + ], new Node('/', '', [ |
| 142 | + new Node('_search', 'search'), |
| 143 | + new Node('{a}', '', [ |
| 144 | + new Node('/_doc/', 'doc', [ |
| 145 | + new Node('{c}', 'doc', [], true) |
| 146 | + ]) |
| 147 | + ], true) |
| 148 | + ]) |
| 149 | + ) |
| 150 | + |
| 151 | + ] |
| 152 | + |
| 153 | + for (const args of cases) { |
| 154 | + t.log(args.name) |
| 155 | + const root = new Node() |
| 156 | + for (const route of args.routes) { |
| 157 | + insert(root, route.path, route.name) |
| 158 | + } |
| 159 | + t.deepEqual(root, args.expect) |
| 160 | + } |
| 161 | +}) |
0 commit comments