1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "modules/indexeddb/IDBKeyPath.h" 28 29 #include "bindings/core/v8/SerializedScriptValue.h" 30 #include "bindings/modules/v8/IDBBindingUtilities.h" 31 #include "modules/indexeddb/IDBKey.h" 32 #include "wtf/Vector.h" 33 34 #include <gtest/gtest.h> 35 36 namespace blink { 37 namespace { 38 39 void checkKeyPath(const String& keyPath, const Vector<String>& expected, int parserError) 40 { 41 IDBKeyPath idbKeyPath(keyPath); 42 ASSERT_EQ(idbKeyPath.type(), IDBKeyPath::StringType); 43 ASSERT_EQ(idbKeyPath.isValid(), (parserError == IDBKeyPathParseErrorNone)); 44 45 IDBKeyPathParseError error; 46 Vector<String> keyPathElements; 47 IDBParseKeyPath(keyPath, keyPathElements, error); 48 ASSERT_EQ(parserError, error); 49 if (error != IDBKeyPathParseErrorNone) 50 return; 51 ASSERT_EQ(expected.size(), keyPathElements.size()); 52 for (size_t i = 0; i < expected.size(); ++i) 53 ASSERT_TRUE(expected[i] == keyPathElements[i]) << i; 54 } 55 56 TEST(IDBKeyPathTest, ValidKeyPath0) 57 { 58 Vector<String> expected; 59 String keyPath(""); 60 checkKeyPath(keyPath, expected, 0); 61 } 62 63 TEST(IDBKeyPathTest, ValidKeyPath1) 64 { 65 Vector<String> expected; 66 String keyPath("foo"); 67 expected.append(String("foo")); 68 checkKeyPath(keyPath, expected, 0); 69 } 70 71 TEST(IDBKeyPathTest, ValidKeyPath2) 72 { 73 Vector<String> expected; 74 String keyPath("foo.bar.baz"); 75 expected.append(String("foo")); 76 expected.append(String("bar")); 77 expected.append(String("baz")); 78 checkKeyPath(keyPath, expected, 0); 79 } 80 81 TEST(IDBKeyPathTest, InvalidKeyPath0) 82 { 83 Vector<String> expected; 84 String keyPath(" "); 85 checkKeyPath(keyPath, expected, 1); 86 } 87 88 TEST(IDBKeyPathTest, InvalidKeyPath1) 89 { 90 Vector<String> expected; 91 String keyPath("+foo.bar.baz"); 92 checkKeyPath(keyPath, expected, 1); 93 } 94 95 TEST(IDBKeyPathTest, InvalidKeyPath2) 96 { 97 Vector<String> expected; 98 String keyPath("foo bar baz"); 99 expected.append(String("foo")); 100 checkKeyPath(keyPath, expected, 2); 101 } 102 103 TEST(IDBKeyPathTest, InvalidKeyPath3) 104 { 105 Vector<String> expected; 106 String keyPath("foo .bar .baz"); 107 expected.append(String("foo")); 108 checkKeyPath(keyPath, expected, 2); 109 } 110 111 TEST(IDBKeyPathTest, InvalidKeyPath4) 112 { 113 Vector<String> expected; 114 String keyPath("foo. bar. baz"); 115 expected.append(String("foo")); 116 checkKeyPath(keyPath, expected, 3); 117 } 118 119 TEST(IDBKeyPathTest, InvalidKeyPath5) 120 { 121 Vector<String> expected; 122 String keyPath("foo..bar..baz"); 123 expected.append(String("foo")); 124 checkKeyPath(keyPath, expected, 3); 125 } 126 127 } // namespace 128 } // namespace blink 129