|
| 1 | +/** |
| 2 | + * Copyright 2019, OpenCensus Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import {TagMap} from '../src/tags/tag-map'; |
| 19 | + |
| 20 | +describe('TagMap()', () => { |
| 21 | + let tagMap: TagMap; |
| 22 | + const key1 = {name: 'key1'}; |
| 23 | + const key2 = {name: 'key2'}; |
| 24 | + const invalidKey1 = {name: 'a'.repeat(256)}; |
| 25 | + const value1 = {value: 'value1'}; |
| 26 | + const value2 = {value: 'value2'}; |
| 27 | + const invalidValue1 = {value: 'a'.repeat(256)}; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + tagMap = new TagMap(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('set()', () => { |
| 34 | + it('should set tagkey and tagvalue', () => { |
| 35 | + tagMap.set(key1, value1); |
| 36 | + const tags = tagMap.tags; |
| 37 | + assert.equal(tags.size, 1); |
| 38 | + assert.deepStrictEqual(tags.get(key1), value1); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should throw an error when invalid tagKey', () => { |
| 42 | + assert.throws(() => { |
| 43 | + tagMap.set(invalidKey1, value1); |
| 44 | + }, /^Error: Invalid TagKey name:/); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw an error when invalid tagValue', () => { |
| 48 | + assert.throws(() => { |
| 49 | + tagMap.set(key1, invalidValue1); |
| 50 | + }, /^Error: Invalid TagValue:/); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not set duplicate tagkey and tagvalue', () => { |
| 54 | + tagMap.set(key1, value1); |
| 55 | + const tags = tagMap.tags; |
| 56 | + assert.equal(tags.size, 1); |
| 57 | + assert.deepStrictEqual(tags.get(key1), value1); |
| 58 | + tagMap.set(key1, value1); |
| 59 | + assert.equal(tags.size, 1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should update existing tagkey', () => { |
| 63 | + tagMap.set(key1, value1); |
| 64 | + const tags = tagMap.tags; |
| 65 | + assert.equal(tags.size, 1); |
| 66 | + assert.deepStrictEqual(tags.get(key1), value1); |
| 67 | + tagMap.set(key1, value2); |
| 68 | + assert.equal(tags.size, 1); |
| 69 | + assert.deepStrictEqual(tags.get(key1), value2); |
| 70 | + }); |
| 71 | + }); |
| 72 | + describe('delete()', () => { |
| 73 | + it('should delete tagkey', () => { |
| 74 | + tagMap.set(key1, value1); |
| 75 | + const tags = tagMap.tags; |
| 76 | + assert.equal(tags.size, 1); |
| 77 | + tagMap.delete(key1); |
| 78 | + assert.equal(tags.size, 0); |
| 79 | + }); |
| 80 | + it('should delete missing tagkey1', () => { |
| 81 | + tagMap.set(key1, value1); |
| 82 | + const tags = tagMap.tags; |
| 83 | + assert.equal(tags.size, 1); |
| 84 | + tagMap.delete(key2); |
| 85 | + assert.equal(tags.size, 1); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments