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/v8/IDBBindingUtilities.h"
30#include "bindings/v8/SerializedScriptValue.h"
31#include "modules/indexeddb/IDBKey.h"
32#include "wtf/Vector.h"
33
34#include <gtest/gtest.h>
35
36using namespace WebCore;
37
38namespace {
39
40void checkKeyPath(const String& keyPath, const Vector<String>& expected, int parserError)
41{
42    IDBKeyPath idbKeyPath(keyPath);
43    ASSERT_EQ(idbKeyPath.type(), IDBKeyPath::StringType);
44    ASSERT_EQ(idbKeyPath.isValid(), (parserError == IDBKeyPathParseErrorNone));
45
46    IDBKeyPathParseError error;
47    Vector<String> keyPathElements;
48    IDBParseKeyPath(keyPath, keyPathElements, error);
49    ASSERT_EQ(parserError, error);
50    if (error != IDBKeyPathParseErrorNone)
51        return;
52    ASSERT_EQ(expected.size(), keyPathElements.size());
53    for (size_t i = 0; i < expected.size(); ++i)
54        ASSERT_TRUE(expected[i] == keyPathElements[i]) << i;
55}
56
57TEST(IDBKeyPathTest, ValidKeyPath0)
58{
59    Vector<String> expected;
60    String keyPath("");
61    checkKeyPath(keyPath, expected, 0);
62}
63
64TEST(IDBKeyPathTest, ValidKeyPath1)
65{
66    Vector<String> expected;
67    String keyPath("foo");
68    expected.append(String("foo"));
69    checkKeyPath(keyPath, expected, 0);
70}
71
72TEST(IDBKeyPathTest, ValidKeyPath2)
73{
74    Vector<String> expected;
75    String keyPath("foo.bar.baz");
76    expected.append(String("foo"));
77    expected.append(String("bar"));
78    expected.append(String("baz"));
79    checkKeyPath(keyPath, expected, 0);
80}
81
82TEST(IDBKeyPathTest, InvalidKeyPath0)
83{
84    Vector<String> expected;
85    String keyPath(" ");
86    checkKeyPath(keyPath, expected, 1);
87}
88
89TEST(IDBKeyPathTest, InvalidKeyPath1)
90{
91    Vector<String> expected;
92    String keyPath("+foo.bar.baz");
93    checkKeyPath(keyPath, expected, 1);
94}
95
96TEST(IDBKeyPathTest, InvalidKeyPath2)
97{
98    Vector<String> expected;
99    String keyPath("foo bar baz");
100    expected.append(String("foo"));
101    checkKeyPath(keyPath, expected, 2);
102}
103
104TEST(IDBKeyPathTest, InvalidKeyPath3)
105{
106    Vector<String> expected;
107    String keyPath("foo .bar .baz");
108    expected.append(String("foo"));
109    checkKeyPath(keyPath, expected, 2);
110}
111
112TEST(IDBKeyPathTest, InvalidKeyPath4)
113{
114    Vector<String> expected;
115    String keyPath("foo. bar. baz");
116    expected.append(String("foo"));
117    checkKeyPath(keyPath, expected, 3);
118}
119
120TEST(IDBKeyPathTest, InvalidKeyPath5)
121{
122    Vector<String> expected;
123    String keyPath("foo..bar..baz");
124    expected.append(String("foo"));
125    checkKeyPath(keyPath, expected, 3);
126}
127
128} // namespace
129