deno.land / std@0.224.0 / cli / _tools / compare_with_rust.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// Run this test with `deno test --unstable-ffi -A compare_with_rust.ts`
import { unicodeWidth } from "../unicode_width.ts";import { fromFileUrl } from "../../path/mod.ts";import fc from "https://esm.sh/fast-check@3.8.0";
// Note: This test is optional. It requires the Rust code to be compiled locallyDeno.test("fast-check equality with unicode_width Rust crate", async (t) => { const libName = ({ darwin: "libunicode_width_crate.dylib", linux: "libunicode_width_crate.so", windows: "libunicode_width_crate.dll", // deno-lint-ignore no-explicit-any } as any)[Deno.build.os]; const libPath = fromFileUrl( import.meta.resolve( `../testdata/unicode_width_crate/target/debug/${libName}`, ), );
const toCString = (str: string) => new TextEncoder().encode(str + "\0");
// @ts-ignore type-check errors if unavailable due to lack of --unstable-ffi flag let dylib: Deno.DynamicLibrary<{ unicode_width: { parameters: ["buffer"]; result: "usize" }; }>;
try { dylib = Deno.dlopen(libPath, { unicode_width: { parameters: ["buffer"], result: "usize" }, });
for ( const arbitrary of [ "string", "unicodeString", "fullUnicodeString", ] as const ) { await t.step({ name: `fc.${arbitrary}()`, fn() { // To avoid esm.sh statically analyzed fc.assert( fc.property( fc[arbitrary](), // JSON stringify to allow "\0" chars to cross FFI boundary in a null-terminated string // deno-lint-ignore no-explicit-any (str: any) => unicodeWidth(str) === dylib.symbols.unicode_width(toCString(JSON.stringify(str))), ), ); }, }); } } finally { // deno-lint-ignore no-extra-non-null-assertion dylib!?.close(); }});
Version Info