Skip to content

Commit

Permalink
improve unicode tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tfussell committed Apr 13, 2017
1 parent 8bf7d0c commit 46df18c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
8 changes: 8 additions & 0 deletions include/xlnt/utils/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class XLNT_API path
/// </summary>
std::string string() const;

#ifdef _MSC_VER
/// <summary>
/// Create a wstring representing this path separated by the provided
/// separator or the system-default separator if not provided.
/// </summary>
std::wstring wstring() const;
#endif

/// <summary>
/// If this path is relative, append each component of this path
/// to base_path and return the resulting absolute path. Otherwise,
Expand Down
10 changes: 10 additions & 0 deletions source/utils/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file

#include <codecvt>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
Expand Down Expand Up @@ -207,6 +209,14 @@ std::string path::string() const
return internal_;
}

#ifdef _MSC_VER
std::wstring path::wstring() const
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
return convert.from_bytes(string());
}
#endif

std::vector<std::string> path::split() const
{
return split_path(internal_, guess_separator());
Expand Down
8 changes: 4 additions & 4 deletions source/workbook/tests/test_consume_xlsx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ class test_consume_xlsx : public CxxTest::TestSuite
{
#ifdef _MSC_VER
xlnt::workbook wb;
const auto path = LSTRING_LITERAL(XLNT_TEST_DATA_DIR) L"/9_unicode_filename_Λ.xlsx";
const auto path = LSTRING_LITERAL(XLNT_TEST_DATA_DIR) L"/9_unicode_Λ.xlsx";
wb.load(path);
TS_ASSERT_EQUALS(wb.active_sheet().cell("A1").value<std::string>(), "unicode!");
TS_ASSERT_EQUALS(wb.active_sheet().cell("A1").value<std::string>(), u8"unicodê!");
#endif

#ifndef __MINGW32__
xlnt::workbook wb2;
const auto path2 = U8STRING_LITERAL(XLNT_TEST_DATA_DIR) u8"/9_unicode_filename_Λ.xlsx";
const auto path2 = U8STRING_LITERAL(XLNT_TEST_DATA_DIR) u8"/9_unicode_Λ.xlsx";
wb2.load(path2);
TS_ASSERT_EQUALS(wb2.active_sheet().cell("A1").value<std::string>(), "unicode!");
TS_ASSERT_EQUALS(wb2.active_sheet().cell("A1").value<std::string>(), u8"unicodê!");
#endif
}

Expand Down
12 changes: 11 additions & 1 deletion source/workbook/tests/test_round_trip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class test_round_trip : public CxxTest::TestSuite
source_workbook.save(destination);
source_workbook.save("temp.xlsx");

std::ifstream source_stream(source.string(), std::ios::binary);
#ifdef _MSC_VER
std::ifstream source_stream(source.wstring(), std::ios::binary);
#else
std::ifstream source_stream(source.string(), std::ios::binary);
#endif

return xml_helper::xlsx_archives_match(xlnt::detail::to_vector(source_stream), destination);
}
Expand All @@ -39,7 +43,12 @@ class test_round_trip : public CxxTest::TestSuite
std::vector<std::uint8_t> destination;
source_workbook.save(destination);

#ifdef _MSC_VER
std::ifstream source_stream(source.wstring(), std::ios::binary);
#else
std::ifstream source_stream(source.string(), std::ios::binary);
#endif

const auto source_decrypted = xlnt::detail::decrypt_xlsx(
xlnt::detail::to_vector(source_stream), password);

Expand All @@ -53,6 +62,7 @@ class test_round_trip : public CxxTest::TestSuite
"2_minimal",
"3_default",
"4_every_style",
u8"9_unicode_Λ",
"10_comments_hyperlinks_formulae",
"11_print_settings",
"12_advanced_properties"
Expand Down
10 changes: 2 additions & 8 deletions source/workbook/workbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@
namespace {

#ifdef _MSC_VER
std::wstring utf8_to_utf16(const std::string &utf8_string)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
return convert.from_bytes(utf8_string);
}

void open_stream(std::ifstream &stream, const std::wstring &path)
{
stream.open(path, std::ios::binary);
Expand All @@ -86,12 +80,12 @@ void open_stream(std::ofstream &stream, const std::wstring &path)

void open_stream(std::ifstream &stream, const std::string &path)
{
open_stream(stream, utf8_to_utf16(path));
open_stream(stream, xlnt::path(path).wstring());
}

void open_stream(std::ofstream &stream, const std::string &path)
{
open_stream(stream, utf8_to_utf16(path));
open_stream(stream, xlnt::path(path).wstring());
}
#else
void open_stream(std::ifstream &stream, const std::string &path)
Expand Down
Binary file removed tests/data/9_unicode_filename_Λ.xlsx
Binary file not shown.
Binary file added tests/data/9_unicode_Λ.xlsx
Binary file not shown.

0 comments on commit 46df18c

Please sign in to comment.