/
path.js
119 lines (100 loc) · 3.85 KB
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import assert from 'power-assert';
import {FieldPath, ResourcePath} from '../src/path';
const PROJECT_ID = 'test-project';
const DATABASE_ROOT = `projects/${PROJECT_ID}/databases/(default)`;
describe('ResourcePath', function() {
it('has id property', function() {
assert.equal(new ResourcePath(PROJECT_ID, '(default)', 'foo').id, 'foo');
assert.equal(new ResourcePath(PROJECT_ID, '(default)').id, null);
});
it('has append() method', function() {
let path = new ResourcePath(PROJECT_ID, '(default)');
assert.equal(path.formattedName, DATABASE_ROOT);
path = path.append('foo');
assert.equal(path.formattedName, `${DATABASE_ROOT}/documents/foo`);
});
it('has parent() method', function() {
let path = new ResourcePath(PROJECT_ID, '(default)', 'foo');
assert.equal(path.formattedName, `${DATABASE_ROOT}/documents/foo`);
path = path.parent();
assert.equal(path.formattedName, DATABASE_ROOT);
assert.equal(path.parent(), null);
});
it('parses strings', function() {
let path = ResourcePath.fromSlashSeparatedString(DATABASE_ROOT);
assert.equal(path.formattedName, DATABASE_ROOT);
path =
ResourcePath.fromSlashSeparatedString(`${DATABASE_ROOT}/documents/foo`);
assert.equal(path.formattedName, `${DATABASE_ROOT}/documents/foo`);
assert.throws(() => {
path =
ResourcePath.fromSlashSeparatedString('projects/project/databases');
}, /Resource name 'projects\/project\/databases' is not valid\./);
});
it('accepts newlines', function() {
const path = ResourcePath.fromSlashSeparatedString(
`${DATABASE_ROOT}/documents/foo\nbar`);
assert.equal(path.formattedName, `${DATABASE_ROOT}/documents/foo\nbar`);
});
});
describe('FieldPath', function() {
it('encodes field names', function() {
let components = [['foo'], ['foo', 'bar'], ['.', '`'], ['\\']];
let results = ['foo', 'foo.bar', '`.`.`\\``', '`\\\\`'];
for (let i = 0; i < components.length; ++i) {
assert.equal(new FieldPath(components[i]).toString(), results[i]);
}
});
it('doesn\'t accept empty path', function() {
assert.throws(() => {
new FieldPath();
}, /Function 'FieldPath\(\)' requires at least 1 argument\./);
});
it('only accepts strings', function() {
assert.throws(() => {
new FieldPath('foo', 'bar', 0);
}, /Argument at index 2 is not a valid string\./);
});
it('has append() method', function() {
let path = new FieldPath('foo');
path = path.append('bar');
assert.equal(path.formattedName, 'foo.bar');
});
it('has parent() method', function() {
let path = new FieldPath('foo', 'bar');
path = path.parent();
assert.equal(path.formattedName, 'foo');
});
it('escapes special characters', function() {
let path = new FieldPath('f.o.o');
assert.equal(path.formattedName, '`f.o.o`');
});
it('doesn\'t allow empty components', function() {
assert.throws(() => {
new FieldPath('foo', '');
}, /Argument at index 1 should not be empty./);
});
it('has isEqual() method', function() {
let path = new FieldPath('a');
let equals = new FieldPath('a');
let notEquals = new FieldPath('a', 'b', 'a');
assert.ok(path.isEqual(equals));
assert.ok(!path.isEqual(notEquals));
});
});