Skip to content

Commit

Permalink
chore(core): Unicode version test
Browse files Browse the repository at this point in the history
- support wasm: copy package.json, nodeversions.json and Blocks.txt into the keyboard area so that they can be mounted under wasm
- path changes to make the non-wasm part work as well

Fixes: #10183
  • Loading branch information
srl295 committed May 16, 2024
1 parent fc8b734 commit 4fc2e39
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 37 deletions.
43 changes: 31 additions & 12 deletions core/tests/unit/ldml/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,42 @@ endif

# Build ldml test executable

keyboard_build_path = join_paths(meson.current_build_dir(),'keyboards')

if cpp_compiler.get_id() == 'emscripten'
tests_flags = ['--embed-file', join_paths(meson.current_build_dir(),'keyboards','@')]
tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'invalid-keyboards','@')]
tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'nodeversions.json') + '@nodeversions.json']
tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'package.json') + '@package.json']
tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'Blocks.txt') + '@Blocks.txt']
test_path = '/'
test_unicode_path = '/'
invalid_test_path = '/'
tests_flags += ['-lnodefs.js',
'-sNO_DISABLE_EXCEPTION_CATCHING', # for test exceptions
'-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']']
else
tests_flags = []
test_path = join_paths(meson.current_build_dir(),'keyboards')
test_unicode_path = join_paths(meson.current_build_dir())
invalid_test_path = join_paths(meson.current_build_dir(),'invalid-keyboards')
endif

if cpp_compiler.get_id() == 'emscripten'
# TODO: we will move the tests to ES6 in the future
# '-sMODULARIZE', '-sEXPORT_ES6',
tests_flags += ['-lnodefs.js', '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']']
endif
# copy package.json into build dir for test use
configure_file(
copy: true,
input: '../../../../package.json',
output: 'package.json',
)
configure_file(
copy: true,
input: '../../../../resources/standards-data/unicode-character-database/Blocks.txt',
output: 'Blocks.txt',
)

# collect some data from node.js
node_versions = custom_target(
'nodeversions.json',
command: [node, '-p', 'JSON.stringify(process.versions)'],
capture: true, # collect stdout from nodejs
output: ['nodeversions.json'],
configure_file(
command: [node, join_paths(meson.current_source_dir(), 'write_node_versions.js'),'@OUTPUT@'],
output: 'nodeversions.json',
)

ldml = executable('ldml',
Expand Down Expand Up @@ -124,8 +137,14 @@ u = executable('test_unicode', 'test_unicode.cpp',
objects: lib.extract_all_objects(recursive: false),

)
test('test_unicode', u, suite: 'ldml', args: [node_versions])

test('test_unicode', u, suite: 'ldml',
args: [
join_paths(test_unicode_path, 'nodeversions.json'),
join_paths(test_unicode_path, 'package.json'),
join_paths(test_unicode_path, 'Blocks.txt'),
],
)

# Run tests on all keyboards (`tests` defined in keyboards/meson.build)

Expand Down
80 changes: 55 additions & 25 deletions core/tests/unit/ldml/test_unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ nlohmann::json load_json(const km::core::path &jsonpath) {
std::cout << "== " << __FUNCTION__ << " loading " << jsonpath << std::endl;
std::ifstream json_file(jsonpath.native());
if (!json_file) {
return -1; // no file
std::cerr << "ERROR Could not load: " << jsonpath << std::endl;
assert (json_file);
}
nlohmann::json data = nlohmann::json::parse(json_file);
return data;
Expand All @@ -70,10 +71,12 @@ std::string get_major(const std::string& ver) {
return ver.substr(start, end - start);
}

std::string get_block_unicode_ver() {
/** @return the Unicode version from a Blocks.txt file */
std::string get_block_unicode_ver(const char *blocks_path) {
std::cout << "= " << __FUNCTION__ << " load " << blocks_path << std::endl;
// fetch Blocks.txt
std::ifstream blocks_file(
km::core::path("../../../../resources/standards-data/unicode-character-database/Blocks.txt").native());
km::core::path(blocks_path).native());
assert(blocks_file.good());
std::string block_line;
assert(std::getline(blocks_file, block_line)); // first line
Expand All @@ -82,16 +85,37 @@ std::string get_block_unicode_ver() {
return block_line.substr(prefix.length());
}

void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json &package) {
std::cout << "== " << __FUNCTION__ << std::endl;
void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json &package,
const std::string &block_unicode_ver) {
std::cout << "== test: " << __FUNCTION__ << std::endl;

const std::string cxx_icu(U_ICU_VERSION),
cxx_icu_unicode(U_UNICODE_VERSION),
node_icu_unicode(versions["/unicode"_json_pointer].template get<std::string>()),
node(versions["/node"_json_pointer].template get<std::string>()),
node_icu(versions["/icu"_json_pointer].template get<std::string>()),
node_engine(package["/engines/node"_json_pointer].template get<std::string>());
const auto block_unicode_ver = get_block_unicode_ver();
#define SHOW_VAR(x) (std::cout << #x << "\t" << (x) << std::endl)

const std::string cxx_icu(U_ICU_VERSION);
SHOW_VAR(cxx_icu);

const std::string cxx_icu_unicode(U_UNICODE_VERSION);
SHOW_VAR(cxx_icu_unicode);

SHOW_VAR(versions);

const std::string node_icu_unicode(versions["unicode"].template get<std::string>());
SHOW_VAR(node_icu_unicode);
SHOW_VAR(versions["node"]);

const std::string node(versions["node"].template get<std::string>());
SHOW_VAR(node);

const std::string node_icu(versions["icu"].template get<std::string>());
SHOW_VAR(node_icu);

const std::string node_engine(package["engines"]["node"].template get<std::string>());
SHOW_VAR(node_engine);

std::cout << "=== Loaded from JSON" << std::endl;

SHOW_VAR(block_unicode_ver);
std::cout << "=== calculating major versions" << std::endl;

// calculations
auto block_ver_major = get_major(block_unicode_ver);
Expand All @@ -101,14 +125,7 @@ void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json
auto node_icu_major = get_major(node_icu);
auto cxx_icu_unicode_major = get_major(cxx_icu_unicode);
auto node_icu_unicode_major = get_major(node_icu_unicode);
#define SHOW_VAR(x) (std::cout << #x << "\t" << (x) << std::endl)
SHOW_VAR(cxx_icu);
SHOW_VAR(cxx_icu_unicode);
SHOW_VAR(node_icu_unicode);
SHOW_VAR(node);
SHOW_VAR(node_icu);
SHOW_VAR(node_engine);
SHOW_VAR(block_unicode_ver);

#undef SHOW_VAR

// allow the Node.js version to be >= required
Expand All @@ -129,17 +146,20 @@ void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json
std::cout << std::endl;
}

int test_all(const char *jsonpath) {
int test_all(const char *jsonpath, const char *packagepath, const char *blockspath) {
std::cout << "= " << __FUNCTION__ << std::endl;

// load the dump of node's process.versions which the meson.build file generated
auto versions = load_json(km::core::path(jsonpath));
assert(!versions.empty());

// load our top level package.json
auto package = load_json(km::core::path("../../../../package.json"));
auto package = load_json(km::core::path(packagepath));
assert(!package.empty());

const auto block_unicode_ver = get_block_unicode_ver(blockspath);

test_unicode_versions(versions, package);
test_unicode_versions(versions, package, block_unicode_ver);

return EXIT_SUCCESS;
}
Expand All @@ -149,7 +169,7 @@ int test_all(const char *jsonpath) {
//-------------------------------------------------------------------------------------

constexpr const auto help_str = "\
test_unicode [--color] nodeversions.json\n\
test_unicode [--color] nodeversions.json package.json Blocks.txt\n\
\n\
--color Force color output\n";

Expand Down Expand Up @@ -183,7 +203,17 @@ int main(int argc, char *argv []) {
}
auto jsonpath = argv[first_arg++];

int rc = test_all(jsonpath);
if (argc <= first_arg) {
return error_args();
}
auto packagepath = argv[first_arg++];

if (argc <= first_arg) {
return error_args();
}
auto blockspath = argv[first_arg++];

int rc = test_all(jsonpath, packagepath, blockspath);

return rc;
}
8 changes: 8 additions & 0 deletions core/tests/unit/ldml/write_node_versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Simple utility to dump process.versions to argument 1

const { argv, versions } = require('process');
const { writeFileSync } = require('fs');

const [ f ] = argv.slice(2);
writeFileSync(f, JSON.stringify(versions, null, ' '));
console.log('Wrote:', f);

0 comments on commit 4fc2e39

Please sign in to comment.