Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/DictionaryTest.cpp

Issue 10897028: Create a shared dictionary class for plugin and engine (Closed)
Patch Set: Created June 7, 2013, 12:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« src/shared/Dictionary.cpp ('K') | « src/shared/Utils.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <fstream>
19 #include <Windows.h>
20 #include <gtest/gtest.h>
21
22 #include "../src/shared/Dictionary.h"
23 #include "../src/shared/Utils.h"
24
25 namespace
26 {
27 void WriteLocale(const std::wstring& basePath, const std::wstring& locale, con st std::string& data)
28 {
29 std::wstring filePath = basePath + locale + L".ini";
30 std::ofstream stream(filePath);
31 if (stream.fail())
32 throw std::runtime_error("Failed creating locale file " + ToUtf8String(fil ePath));
33
34 stream << data;
35 if (stream.fail())
36 throw std::runtime_error("Failed writing to file " + ToUtf8String(filePath ));
37 }
38
39 void RemoveLocale(const std::wstring& basePath, const std::wstring& locale)
40 {
41 std::wstring filePath = basePath + locale + L".ini";
42 ::DeleteFileW(filePath.c_str());
43 }
44 }
45
46 class DictionaryTest : public ::testing::Test
47 {
48 protected:
49 std::wstring basePath;
50
51 void SetUp()
52 {
53 basePath = GetDllDir() + L"locales\\";
54 ::CreateDirectoryW(basePath.c_str(), NULL);
55
56 WriteLocale(basePath, L"en", "[general]\nfoo=bar\n[x]\nx=y\n");
57 WriteLocale(basePath, L"es-ES", "[general]\n#comment=nada\nfoo=esbar");
58 WriteLocale(basePath, L"ru", "[general]\ntrash\nfoo=\xD1\x82\xD0\xB5\xD1\x81 \xD1\x82\n");
59 }
60 void TearDown()
61 {
62 if (Dictionary::instance)
63 {
64 delete Dictionary::instance;
65 Dictionary::instance = 0;
66 }
67
68 RemoveLocale(basePath, L"en");
69 RemoveLocale(basePath, L"es-ES");
70 RemoveLocale(basePath, L"ru");
71 ::RemoveDirectoryW(basePath.c_str());
72 }
73 };
74
75 TEST_F(DictionaryTest, DefaultLocale)
76 {
77 Dictionary::Create(L"en");
78 Dictionary* dict = Dictionary::GetInstance();
79
80 ASSERT_TRUE(dict);
81 ASSERT_EQ(L"bar", dict->Lookup("general", "foo"));
82 ASSERT_EQ(L"y", dict->Lookup("x", "x"));
83 ASSERT_NE(L"bar", dict->Lookup("", "foo"));
84 ASSERT_NE(L"bar", dict->Lookup("x", "foo"));
85 ASSERT_NE(L"bar", dict->Lookup("generalsomething", "foo"));
86 ASSERT_FALSE(dict->Lookup("foo", "bar").empty());
87 }
88
89 TEST_F(DictionaryTest, LocaleFallback)
90 {
91 Dictionary::Create(L"es-ES");
92 Dictionary* dict = Dictionary::GetInstance();
93
94 ASSERT_TRUE(dict);
95 ASSERT_EQ(L"esbar", dict->Lookup("general", "foo"));
96 ASSERT_EQ(L"y", dict->Lookup("x", "x"));
97 }
98
99 TEST_F(DictionaryTest, LocaleFallbackShort)
100 {
101 Dictionary::Create(L"ru-RU");
102 Dictionary* dict = Dictionary::GetInstance();
103
104 ASSERT_TRUE(dict);
105 ASSERT_EQ(L"\u0442\u0435\u0441\u0442", dict->Lookup("general", "foo"));
106 ASSERT_EQ(L"y", dict->Lookup("x", "x"));
107 }
OLDNEW
« src/shared/Dictionary.cpp ('K') | « src/shared/Utils.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld