deno.land / std@0.224.0 / cli / _tools / compare_with_rust.ts

compare_with_rust.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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(); }});
std

Version Info

Tagged at
4 months ago