Skip to content

Commit c36205e

Browse files
committed
Fixed a bug introduced while implementing issue #44 where unicode encoding was not working.
Created unit test for ActionFile.
1 parent 57a60dc commit c36205e

File tree

4 files changed

+229
-1
lines changed

4 files changed

+229
-1
lines changed

src/ActionFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace shellanything
7171
{
7272
std::wstring textW = ra::unicode::Utf8ToUnicode(text);
7373
text.clear();
74-
text.assign((const char *)textW.data(), text.size());
74+
text.assign((const char *)textW.data(), textW.size()*2);
7575
}
7676
//note: utf-8 does not need conversion
7777

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ add_executable(shellanything_unittest
4646
${CONFIGURATION_TEST_FILES}
4747
${SHELLANYTHING_PRIVATE_FILES}
4848
main.cpp
49+
TestActionFile.cpp
50+
TestActionFile.h
4951
TestBitmapCache.cpp
5052
TestBitmapCache.h
5153
TestConfigManager.cpp

test/TestActionFile.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#include "TestActionFile.h"
26+
#include "shellanything/Context.h"
27+
#include "shellanything/ActionFile.h"
28+
#include "PropertyManager.h"
29+
#include "rapidassist/testing.h"
30+
#include "rapidassist/filesystem_utf8.h"
31+
32+
#include <Windows.h>
33+
34+
namespace shellanything { namespace test
35+
{
36+
37+
//--------------------------------------------------------------------------------------------------
38+
void TestActionFile::SetUp()
39+
{
40+
PropertyManager & pmgr = PropertyManager::GetInstance();
41+
pmgr.Clear();
42+
}
43+
//--------------------------------------------------------------------------------------------------
44+
void TestActionFile::TearDown()
45+
{
46+
}
47+
//--------------------------------------------------------------------------------------------------
48+
TEST_F(TestActionFile, testEncodingUtf8)
49+
{
50+
PropertyManager & pmgr = PropertyManager::GetInstance();
51+
52+
const std::string selected_file_path_utf8 = "C:\\TEMP\\\303\251cole"; //school in french, encoded as utf-8
53+
54+
//Create a valid context
55+
Context c;
56+
Context::ElementList elements;
57+
elements.push_back(selected_file_path_utf8);
58+
c.SetElements(elements);
59+
60+
c.RegisterProperties();
61+
62+
const std::string separator = ra::filesystem::GetPathSeparatorStr();
63+
const std::string filename = ra::testing::GetTestQualifiedName() + ".txt";
64+
const std::string temp_dir = ra::filesystem::GetTemporaryDirectory();
65+
const std::string path = temp_dir + separator + filename;
66+
67+
//cleanup
68+
ra::filesystem::DeleteFileUtf8(path.c_str());
69+
70+
//execute the action
71+
ActionFile af;
72+
af.SetPath(path);
73+
af.SetEncoding("utf-8");
74+
af.SetText("${selection.path}");
75+
76+
bool executed = af.Execute(c);
77+
ASSERT_TRUE( executed );
78+
79+
ASSERT_TRUE( ra::filesystem::FileExistsUtf8(path.c_str()) ) << "File not found: " << path;
80+
81+
const size_t file_size = ra::filesystem::GetFileSize(path.c_str());
82+
const size_t expected_size = selected_file_path_utf8.size();
83+
ASSERT_EQ(expected_size, file_size);
84+
85+
//cleanup
86+
ra::filesystem::DeleteFileUtf8(path.c_str());
87+
}
88+
//--------------------------------------------------------------------------------------------------
89+
TEST_F(TestActionFile, testEncodingAnsi)
90+
{
91+
//validate user's active code page is compatible with U+00E9 character (E with Acute)
92+
UINT acp = GetACP();
93+
bool is_compatible_acp =
94+
(acp == 850) || //https://en.wikipedia.org/wiki/Code_page_850
95+
(acp == 1252); //https://en.wikipedia.org/wiki/Windows-1252
96+
if (!is_compatible_acp)
97+
{
98+
printf("Skipped. Incompatible active code page: %u\n", acp);
99+
return;
100+
}
101+
102+
PropertyManager & pmgr = PropertyManager::GetInstance();
103+
104+
const std::string selected_file_path_utf8 = "C:\\TEMP\\\303\251cole"; //school in french, encoded as utf-8
105+
106+
//Create a valid context
107+
Context c;
108+
Context::ElementList elements;
109+
elements.push_back(selected_file_path_utf8);
110+
c.SetElements(elements);
111+
112+
c.RegisterProperties();
113+
114+
const std::string separator = ra::filesystem::GetPathSeparatorStr();
115+
const std::string filename = ra::testing::GetTestQualifiedName() + ".txt";
116+
const std::string temp_dir = ra::filesystem::GetTemporaryDirectory();
117+
const std::string path = temp_dir + separator + filename;
118+
119+
//cleanup
120+
ra::filesystem::DeleteFileUtf8(path.c_str());
121+
122+
//execute the action
123+
ActionFile af;
124+
af.SetPath(path);
125+
af.SetEncoding("ansi");
126+
af.SetText("${selection.path}");
127+
128+
bool executed = af.Execute(c);
129+
ASSERT_TRUE( executed );
130+
131+
ASSERT_TRUE( ra::filesystem::FileExistsUtf8(path.c_str()) ) << "File not found: " << path;
132+
133+
const size_t file_size = ra::filesystem::GetFileSize(path.c_str());
134+
const size_t expected_size = selected_file_path_utf8.size() - 1; //a character encoded as 2 bytes should be encoded as a single byte.
135+
ASSERT_EQ(expected_size, file_size);
136+
137+
//cleanup
138+
ra::filesystem::DeleteFileUtf8(path.c_str());
139+
}
140+
//--------------------------------------------------------------------------------------------------
141+
TEST_F(TestActionFile, testEncodingUnicode)
142+
{
143+
PropertyManager & pmgr = PropertyManager::GetInstance();
144+
145+
const std::string selected_file_path_utf8 = "C:\\TEMP\\\303\251cole"; //school in french, encoded as utf-8
146+
147+
//Create a valid context
148+
Context c;
149+
Context::ElementList elements;
150+
elements.push_back(selected_file_path_utf8);
151+
c.SetElements(elements);
152+
153+
c.RegisterProperties();
154+
155+
const std::string separator = ra::filesystem::GetPathSeparatorStr();
156+
const std::string filename = ra::testing::GetTestQualifiedName() + ".txt";
157+
const std::string temp_dir = ra::filesystem::GetTemporaryDirectory();
158+
const std::string path = temp_dir + separator + filename;
159+
160+
//cleanup
161+
ra::filesystem::DeleteFileUtf8(path.c_str());
162+
163+
//execute the action
164+
ActionFile af;
165+
af.SetPath(path);
166+
af.SetEncoding("unicode");
167+
af.SetText("${selection.path}");
168+
169+
bool executed = af.Execute(c);
170+
ASSERT_TRUE( executed );
171+
172+
ASSERT_TRUE( ra::filesystem::FileExistsUtf8(path.c_str()) ) << "File not found: " << path;
173+
174+
const size_t file_size = ra::filesystem::GetFileSize(path.c_str());
175+
const size_t expected_size = (selected_file_path_utf8.size() - 1) * 2; //a character encoded as 2 bytes should be encoded as a single byte and then the whole string should be doubled.
176+
ASSERT_EQ(expected_size, file_size);
177+
178+
//cleanup
179+
ra::filesystem::DeleteFileUtf8(path.c_str());
180+
}
181+
//--------------------------------------------------------------------------------------------------
182+
183+
} //namespace test
184+
} //namespace shellanything

test/TestActionFile.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#ifndef TEST_SA_ACTIONFILE_H
26+
#define TEST_SA_ACTIONFILE_H
27+
28+
#include <gtest/gtest.h>
29+
30+
namespace shellanything { namespace test
31+
{
32+
class TestActionFile : public ::testing::Test
33+
{
34+
public:
35+
virtual void SetUp();
36+
virtual void TearDown();
37+
};
38+
39+
} //namespace test
40+
} //namespace shellanything
41+
42+
#endif //TEST_SA_ACTIONFILE_H

0 commit comments

Comments
 (0)