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

Side by Side Diff: test/Prefs.cpp

Issue 29481704: Noissue - Use buffer for FileSystem IO (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created July 6, 2017, 9:19 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/DefaultFileSystem.cpp ('K') | « test/FileSystemJsObject.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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 11 matching lines...) Expand all
22 22
23 using namespace AdblockPlus; 23 using namespace AdblockPlus;
24 24
25 namespace 25 namespace
26 { 26 {
27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
28 28
29 class TestFileSystem : public LazyFileSystem 29 class TestFileSystem : public LazyFileSystem
30 { 30 {
31 public: 31 public:
32 std::string prefsContents; 32 IOBuffer prefsContents;
33 33
34 std::shared_ptr<std::istream> Read(const std::string& path) const 34 IOBuffer Read(const std::string& path) const
35 { 35 {
36 if (path == "prefs.json" && !prefsContents.empty()) 36 if (path == "prefs.json" && !prefsContents.empty())
37 return std::shared_ptr<std::istream>(new std::istringstream(prefsContent s)); 37 return prefsContents;
38 38
39 return LazyFileSystem::Read(path); 39 return LazyFileSystem::Read(path);
40 } 40 }
41 41
42 void Write(const std::string& path, std::istream& content) 42 void Write(const std::string& path, const IOBuffer& content)
43 { 43 {
44 if (path == "prefs.json") 44 if (path == "prefs.json")
45 { 45 prefsContents = content;
46 std::stringstream ss;
47 ss << content.rdbuf();
48 prefsContents = ss.str();
49 }
50 else 46 else
51 LazyFileSystem::Write(path, content); 47 LazyFileSystem::Write(path, content);
52 } 48 }
53 49
54 StatResult Stat(const std::string& path) const 50 StatResult Stat(const std::string& path) const
55 { 51 {
56 if (path == "prefs.json") 52 if (path == "prefs.json")
57 { 53 {
58 StatResult result; 54 StatResult result;
59 result.exists = result.isFile = !prefsContents.empty(); 55 result.exists = result.isFile = !prefsContents.empty();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 ResetJsEngine(); 133 ResetJsEngine();
138 auto filterEngine = CreateFilterEngine(); 134 auto filterEngine = CreateFilterEngine();
139 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); 135 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString());
140 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); 136 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt());
141 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); 137 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool());
142 } 138 }
143 } 139 }
144 140
145 TEST_F(PrefsTest, UnknownPrefs) 141 TEST_F(PrefsTest, UnknownPrefs)
146 { 142 {
147 fileSystem->prefsContents = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; 143 using IOBuffer = AdblockPlus::FileSystem::IOBuffer;
144 std::string content = "{\"foobar\":2, \"patternsbackupinterval\": 12}";
145 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend());
148 auto filterEngine = CreateFilterEngine(); 146 auto filterEngine = CreateFilterEngine();
149 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined()); 147 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined());
150 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval").AsInt()); 148 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval").AsInt());
151 } 149 }
152 150
153 TEST_F(PrefsTest, SyntaxFailure) 151 TEST_F(PrefsTest, SyntaxFailure)
154 { 152 {
155 fileSystem->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; 153 using IOBuffer = AdblockPlus::FileSystem::IOBuffer;
154 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}";
155 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend());
156 auto filterEngine = CreateFilterEngine(); 156 auto filterEngine = CreateFilterEngine();
157 157
158 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); 158 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt());
159 } 159 }
160 160
161 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) 161 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured)
162 { 162 {
163 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 163 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
164 preconfiguredPrefs.emplace("disable_auto_updates", jsEngine->NewValue(false)); 164 preconfiguredPrefs.emplace("disable_auto_updates", jsEngine->NewValue(false));
165 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(true) ); 165 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(true) );
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 { 209 {
210 ResetJsEngine(); 210 ResetJsEngine();
211 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 211 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
212 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru e)); 212 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru e));
213 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 213 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
214 214
215 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); 215 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool());
216 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); 216 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool());
217 } 217 }
218 } 218 }
OLDNEW
« src/DefaultFileSystem.cpp ('K') | « test/FileSystemJsObject.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld