murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | 'use strict'; |
| 35 | |
| 36 | var Metadata = require('../src/metadata.js'); |
| 37 | |
| 38 | var assert = require('assert'); |
| 39 | |
| 40 | describe('Metadata', function() { |
| 41 | var metadata; |
| 42 | beforeEach(function() { |
| 43 | metadata = new Metadata(); |
| 44 | }); |
| 45 | describe('#set', function() { |
| 46 | it('Only accepts string values for non "-bin" keys', function() { |
| 47 | assert.throws(function() { |
| 48 | metadata.set('key', new Buffer('value')); |
| 49 | }); |
| 50 | assert.doesNotThrow(function() { |
| 51 | metadata.set('key', 'value'); |
| 52 | }); |
| 53 | }); |
| 54 | it('Only accepts Buffer values for "-bin" keys', function() { |
| 55 | assert.throws(function() { |
| 56 | metadata.set('key-bin', 'value'); |
| 57 | }); |
| 58 | assert.doesNotThrow(function() { |
| 59 | metadata.set('key-bin', new Buffer('value')); |
| 60 | }); |
| 61 | }); |
murgatroid99 | cc248a2 | 2015-08-20 14:47:15 -0700 | [diff] [blame] | 62 | it('Rejects invalid keys', function() { |
| 63 | assert.throws(function() { |
| 64 | metadata.set('key$', 'value'); |
| 65 | }); |
| 66 | assert.throws(function() { |
| 67 | metadata.set('', 'value'); |
| 68 | }); |
| 69 | }); |
| 70 | it('Rejects values with non-ASCII characters', function() { |
| 71 | assert.throws(function() { |
| 72 | metadata.set('key', 'résumé'); |
| 73 | }); |
| 74 | }); |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 75 | it('Saves values that can be retrieved', function() { |
| 76 | metadata.set('key', 'value'); |
| 77 | assert.deepEqual(metadata.get('key'), ['value']); |
| 78 | }); |
| 79 | it('Overwrites previous values', function() { |
| 80 | metadata.set('key', 'value1'); |
| 81 | metadata.set('key', 'value2'); |
| 82 | assert.deepEqual(metadata.get('key'), ['value2']); |
| 83 | }); |
| 84 | it('Normalizes keys', function() { |
| 85 | metadata.set('Key', 'value1'); |
| 86 | assert.deepEqual(metadata.get('key'), ['value1']); |
| 87 | metadata.set('KEY', 'value2'); |
| 88 | assert.deepEqual(metadata.get('key'), ['value2']); |
| 89 | }); |
| 90 | }); |
| 91 | describe('#add', function() { |
| 92 | it('Only accepts string values for non "-bin" keys', function() { |
| 93 | assert.throws(function() { |
| 94 | metadata.add('key', new Buffer('value')); |
| 95 | }); |
| 96 | assert.doesNotThrow(function() { |
| 97 | metadata.add('key', 'value'); |
| 98 | }); |
| 99 | }); |
| 100 | it('Only accepts Buffer values for "-bin" keys', function() { |
| 101 | assert.throws(function() { |
| 102 | metadata.add('key-bin', 'value'); |
| 103 | }); |
| 104 | assert.doesNotThrow(function() { |
| 105 | metadata.add('key-bin', new Buffer('value')); |
| 106 | }); |
| 107 | }); |
murgatroid99 | cc248a2 | 2015-08-20 14:47:15 -0700 | [diff] [blame] | 108 | it('Rejects invalid keys', function() { |
| 109 | assert.throws(function() { |
| 110 | metadata.add('key$', 'value'); |
| 111 | }); |
| 112 | assert.throws(function() { |
| 113 | metadata.add('', 'value'); |
| 114 | }); |
| 115 | }); |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 116 | it('Saves values that can be retrieved', function() { |
| 117 | metadata.add('key', 'value'); |
| 118 | assert.deepEqual(metadata.get('key'), ['value']); |
| 119 | }); |
| 120 | it('Combines with previous values', function() { |
| 121 | metadata.add('key', 'value1'); |
| 122 | metadata.add('key', 'value2'); |
| 123 | assert.deepEqual(metadata.get('key'), ['value1', 'value2']); |
| 124 | }); |
| 125 | it('Normalizes keys', function() { |
| 126 | metadata.add('Key', 'value1'); |
| 127 | assert.deepEqual(metadata.get('key'), ['value1']); |
| 128 | metadata.add('KEY', 'value2'); |
| 129 | assert.deepEqual(metadata.get('key'), ['value1', 'value2']); |
| 130 | }); |
| 131 | }); |
| 132 | describe('#remove', function() { |
| 133 | it('clears values from a key', function() { |
| 134 | metadata.add('key', 'value'); |
| 135 | metadata.remove('key'); |
| 136 | assert.deepEqual(metadata.get('key'), []); |
| 137 | }); |
murgatroid99 | 6b8a3a7 | 2015-08-20 14:51:59 -0700 | [diff] [blame] | 138 | it('Normalizes keys', function() { |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 139 | metadata.add('key', 'value'); |
| 140 | metadata.remove('KEY'); |
murgatroid99 | 6b8a3a7 | 2015-08-20 14:51:59 -0700 | [diff] [blame] | 141 | assert.deepEqual(metadata.get('key'), []); |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 142 | }); |
| 143 | }); |
| 144 | describe('#get', function() { |
| 145 | beforeEach(function() { |
| 146 | metadata.add('key', 'value1'); |
| 147 | metadata.add('key', 'value2'); |
| 148 | metadata.add('key-bin', new Buffer('value')); |
| 149 | }); |
| 150 | it('gets all values associated with a key', function() { |
| 151 | assert.deepEqual(metadata.get('key'), ['value1', 'value2']); |
| 152 | }); |
murgatroid99 | 6b8a3a7 | 2015-08-20 14:51:59 -0700 | [diff] [blame] | 153 | it('Normalizes keys', function() { |
| 154 | assert.deepEqual(metadata.get('KEY'), ['value1', 'value2']); |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 155 | }); |
| 156 | it('returns an empty list for non-existent keys', function() { |
| 157 | assert.deepEqual(metadata.get('non-existent-key'), []); |
| 158 | }); |
| 159 | it('returns Buffers for "-bin" keys', function() { |
| 160 | assert(metadata.get('key-bin')[0] instanceof Buffer); |
| 161 | }); |
| 162 | }); |
| 163 | describe('#getMap', function() { |
| 164 | it('gets a map of keys to values', function() { |
| 165 | metadata.add('key1', 'value1'); |
| 166 | metadata.add('Key2', 'value2'); |
| 167 | metadata.add('KEY3', 'value3'); |
| 168 | assert.deepEqual(metadata.getMap(), |
| 169 | {key1: 'value1', |
| 170 | key2: 'value2', |
| 171 | key3: 'value3'}); |
| 172 | }); |
| 173 | }); |
| 174 | describe('#clone', function() { |
| 175 | it('retains values from the original', function() { |
| 176 | metadata.add('key', 'value'); |
| 177 | var copy = metadata.clone(); |
| 178 | assert.deepEqual(copy.get('key'), ['value']); |
| 179 | }); |
| 180 | it('Does not see newly added values', function() { |
| 181 | metadata.add('key', 'value1'); |
| 182 | var copy = metadata.clone(); |
| 183 | metadata.add('key', 'value2'); |
| 184 | assert.deepEqual(copy.get('key'), ['value1']); |
| 185 | }); |
| 186 | it('Does not add new values to the original', function() { |
| 187 | metadata.add('key', 'value1'); |
| 188 | var copy = metadata.clone(); |
| 189 | copy.add('key', 'value2'); |
| 190 | assert.deepEqual(metadata.get('key'), ['value1']); |
| 191 | }); |
| 192 | }); |
| 193 | }); |