use super::common::*;
#[test]
fn test_p3_mixed_decl_expr_basic() {
let js = r#"
export function mixDeclExpr(x, y) {
const z = x + y;
return z;
}
"#;
let zig = transpile_and_check(js, "test_p3_mixed_decl_expr_basic");
println!("=== Mix decl+expr (basic) ===\n{}", zig);
assert!(
zig.contains("z = (x + y)"),
"Expected 'z = (x + y)':\n{}",
zig
);
}
#[test]
fn test_p3_mixed_decl_expr_unused_var() {
let js = r#"
export function mixUnusedVar(x) {
const z = x * 2;
return x + 1;
}
"#;
let zig = transpile_and_assert(js, "test_p3_mixed_decl_expr_unused_var");
println!("=== Mix decl+expr (unused var) ===\n{}", zig);
}
#[test]
fn test_p3_mixed_decl_expr_call() {
let js = r#"
export function mixDeclCall(x) {
const a = x + 1;
const b = x + 2;
return a + b;
}
"#;
let zig = transpile_and_check(js, "test_p3_mixed_decl_expr_call");
println!("=== Mix decl+expr (call) ===\n{}", zig);
assert!(
zig.contains("a = (x + 1)"),
"Expected 'a = (x + 1)':\n{}",
zig
);
assert!(
zig.contains("b = (x + 2)"),
"Expected 'b = (x + 2)':\n{}",
zig
);
}
#[test]
fn test_p3_mixed_decl_expr_return_unused() {
let js = r#"
export function mixStandaloneExpr(x) {
const z = x + 1;
return z;
}
"#;
let zig = transpile_and_check(js, "test_p3_mixed_decl_expr_return_unused");
println!("=== Mix decl+expr (standalone) ===\n{}", zig);
}
#[test]
fn test_native_proto_private_field_basic() {
let js = r#"
class Counter {
#count = 10;
increment() {
return this.#count + 1;
}
getCount() {
return this.#count;
}
}
export function testCounter() {
const c = new Counter();
return c.getCount();
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_private_field_basic");
println!("=== Private field Zig code ===\n{}", zig);
assert!(
zig.contains("const Counter = struct {"),
"Expected Counter struct"
);
assert!(zig.contains("count:"), "Expected count field in struct");
assert!(
zig.contains(".count = 10") || zig.contains(".count=10"),
"Expected default value 10. Got:\n{}",
zig
);
assert!(
zig.contains("self.count"),
"Expected self.count access. Got:\n{}",
zig
);
assert!(zig.contains("increment"), "Expected increment method");
assert!(zig.contains("getCount"), "Expected getCount method");
}
#[test]
fn test_native_proto_private_field_no_default() {
let js = r#"
class Widget {
#id;
getId() {
return this.#id;
}
}
export function testWidget() {
const w = new Widget();
return w.getId();
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_private_field_no_default");
println!("=== Private field (no default) Zig code ===\n{}", zig);
assert!(
zig.contains("const Widget = struct {"),
"Expected Widget struct"
);
assert!(zig.contains("id:"), "Expected id field");
assert!(
zig.contains(".id = undefined"),
"Expected default=undefined. Got:\n{}",
zig
);
}
#[test]
fn test_native_proto_private_field_string_default() {
let js = r#"
class Logger {
#name = "default";
getName() {
return this.#name;
}
}
export function testLogger() {
const log = new Logger();
return log.getName();
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_private_field_string_default");
println!("=== Private field (string) Zig code ===\n{}", zig);
assert!(
zig.contains(".name = \"default\""),
"Expected string default. Got:\n{}",
zig
);
}
#[test]
fn test_native_proto_private_field_multiple() {
let js = r#"
class Config {
#port = 8080;
#host = "localhost";
#secure = true;
getHost() {
return this.#host;
}
}
export function testConfig() {
const cfg = new Config();
return cfg.getHost();
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_private_field_multiple");
println!("=== Private fields (multiple) Zig code ===\n{}", zig);
assert!(
zig.contains(".port = 8080") || zig.contains(".port=8080"),
"Expected port=8080. Got:\n{}",
zig
);
assert!(
zig.contains(".host = \"localhost\""),
"Expected host default. Got:\n{}",
zig
);
assert!(
zig.contains(".secure = true") || zig.contains(".secure=true"),
"Expected secure=true. Got:\n{}",
zig
);
}
#[test]
fn test_native_proto_private_field_mixed_with_public() {
let js = r#"
class Person {
name = "anonymous";
#age = 0;
constructor(nameVal, ageVal) {
this.name = nameVal;
this.#age = ageVal;
}
getAge() {
return this.#age;
}
describe() {
return this.name;
}
}
export function testPerson() {
const p = new Person("Alice", 30);
return p.describe();
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_private_field_mixed");
println!("=== Private+public fields Zig code ===\n{}", zig);
assert!(
zig.contains("const Person = struct {"),
"Expected Person struct"
);
assert!(zig.contains("name:"), "Expected name field");
assert!(zig.contains("age:"), "Expected age field (stripped #)");
assert!(
zig.contains("pub fn init("),
"Expected init() constructor. Got:\n{}",
zig
);
assert!(zig.contains("Person.init("), "Expected Person.init routing");
assert!(zig.contains("self.age"), "Expected self.age. Got:\n{}", zig);
}
#[test]
fn test_bigint_add() {
let zig = transpile_and_assert(
r#"
function test() {
return 2n + 3n;
}
"#,
"test_bigint_add",
);
assert!(
zig.contains("JsBigInt"),
"BigInt expr should generate JsBigInt code"
);
}
#[test]
fn test_not_implemented_tagged_template() {
assert_not_implemented(
r#"
function tag(parts, ...args) { return parts[0]; }
const result = tag`hello ${1}`;
"#,
"Tagged template literal",
);
}
#[test]
fn test_implemented_instanceof_runtime() {
let js = r#"
function check(arr) {
return arr instanceof Array;
}
"#;
let zig = transpile_and_assert(js, "test_implemented_instanceof_runtime");
assert!(
zig.contains("instanceOf"),
"Expected instanceOf call in:\n{}",
zig
);
assert!(
zig.contains("\"Array\""),
"Expected 'Array' string literal in:\n{}",
zig
);
}
#[test]
fn test_class_expression() {
let js = r#"
/**
* @returns {i64}
*/
export function testClassExpr() {
const X = class { constructor(val) { this.x = val; } };
const obj = X.init(42);
return obj.x;
}
"#;
let zig = transpile_and_check(js, "test_class_expression");
assert!(
zig.contains("_AnonClass_0 = struct") || zig.contains("= struct"),
"Expected class struct in:\n{}",
zig
);
assert!(
zig.contains("init("),
"Expected init constructor in:\n{}",
zig
);
}
#[test]
fn test_class_expression_named() {
let js = r#"
/**
* @returns {i64}
*/
export function testNamedClassExpr() {
const X = class MyClass { constructor(val) { this.x = val; } };
const obj = X.init(99);
return obj.x;
}
"#;
let zig = transpile_and_check(js, "test_class_expression_named");
assert!(
zig.contains("MyClass = struct"),
"Expected named class struct in:\n{}",
zig
);
assert!(
zig.contains("const X = MyClass"),
"Expected X assigned to MyClass in:\n{}",
zig
);
}
#[test]
fn test_class_extends_compile_error() {
let js = r#"
class Animal {
constructor(name) { this.name = name; }
speak() { return this.name; }
}
class Dog extends Animal {
constructor(name) { super(name); }
bark() { return this.name; }
}
"#;
let result = parse_and_transpile(js, None).unwrap();
assert!(
result
.errors
.iter()
.any(|e| e.contains("class extends is not supported")),
"Expected 'class extends is not supported' in errors: {:?}",
result.errors
);
assert!(
result
.zig_code
.contains("@compileError(\"class extends is not supported"),
"Expected @compileError in zig code:\n{}",
result.zig_code
);
}
#[test]
fn test_class_extends_expression_compile_error() {
let js = r#"
/** @returns {i64} */
export function fn() {
const Base = class { constructor(v) { this.x = v; } };
const Child = class extends Base { constructor(v) { super(v); } };
return 0;
}
"#;
let result = parse_and_transpile(js, None).unwrap();
assert!(
result
.errors
.iter()
.any(|e| e.contains("class extends is not supported")),
"Expected 'class extends is not supported' in errors: {:?}",
result.errors
);
assert!(
result
.zig_code
.contains("@compileError(\"class extends is not supported"),
"Expected @compileError in zig code:\n{}",
result.zig_code
);
}
#[test]
fn test_not_implemented_generator_function() {
assert_not_implemented(
r#"
function* gen() { yield 1; yield 2; }
"#,
"Generator function (function*)",
);
}
#[test]
fn test_not_implemented_yield_expression() {
assert_not_implemented(
r#"
function* gen() { yield 1; }
const g = gen();
const val = g.next().value;
"#,
"Yield expression (inside generator)",
);
}
#[test]
fn test_not_implemented_async_generator() {
assert_not_implemented(
r#"
async function* gen() { yield 1; }
"#,
"Async generator (async function*)",
);
}
#[test]
fn test_not_implemented_dynamic_import() {
assert_not_implemented(
r#"
const mod = import("some_module");
"#,
"Dynamic import()",
);
}
#[test]
fn test_not_implemented_new_target() {
assert_not_implemented(
r#"
function Foo() {
if (new.target) { return 1; }
return 0;
}
"#,
"new.target meta property",
);
}
#[test]
fn test_not_implemented_for_await_of() {
assert_not_implemented(
r#"
async function process(items) {
for await (const item of items) { }
}
"#,
"for await...of (async iteration)",
);
}
#[test]
fn test_import_meta_is_compile_error() {
let result = parse_and_transpile(
r#"
function getMeta() {
return import.meta.url;
}
"#,
None,
)
.unwrap();
let has_not_supported = result.errors.iter().any(|e| e.contains("not supported"));
assert!(
has_not_supported,
"import.meta should produce 'not supported' error, got: {:?}",
result.errors
);
assert!(
result.zig_code.contains("@compileError"),
"import.meta should generate @compileError: {}",
result.zig_code
);
}
#[test]
fn test_not_implemented_with_statement() {
assert_not_implemented(
r#"
const obj = { x: 1 };
with (obj) { console.log(x); }
"#,
"with statement",
);
}
#[test]
fn test_not_implemented_debugger_statement() {
assert_not_implemented(
r#"
function buggy() {
debugger;
}
"#,
"debugger statement",
);
}
#[test]
fn test_arguments_object() {
let js = r#"
function sum(a, b) {
let total = 0;
for (let i = 0; i < arguments.length; i++) { total += arguments[i]; }
return total;
}
"#;
let zig = transpile_and_check(js, "test_arguments_object");
assert!(
zig.contains("__arguments"),
"Expected __arguments variable in:\n{}",
zig
);
}
#[test]
fn test_static_block_no_error() {
let js = r#"
class Foo {
static x = 1;
}
"#;
let zig = transpile_and_check(js, "test_static_block");
assert!(
!zig.contains("static {} blocks are not supported"),
"static block should not produce error about unsupported, got:\n{}",
zig
);
assert!(
!zig.contains("@compileError"),
"should not have compileError, got:\n{}",
zig
);
}
#[test]
fn test_static_field_read() {
let js = r#"
class Foo {
static x = 1;
}
/**
* @returns {number}
*/
export function readStatic() {
return Foo.x;
}
"#;
let zig = transpile_and_check(js, "test_static_field_read");
assert!(
zig.contains("__Foo_x"),
"Expected __Foo_x in output:\n{}",
zig
);
}
#[test]
fn test_static_field_assign() {
let js = r#"
class Foo {
static x = 1;
}
/**
* @param {number} v
* @returns {number}
*/
export function writeStatic(v) {
Foo.x = v;
return Foo.x;
}
"#;
let zig = transpile_and_check(js, "test_static_field_assign");
assert!(
zig.contains("__Foo_x = v"),
"Expected '__Foo_x = v' in output:\n{}",
zig
);
assert!(
zig.contains("return __Foo_x"),
"Expected 'return __Foo_x' in output:\n{}",
zig
);
}
#[test]
fn test_static_block_this_read() {
let js = r#"
class Foo {
static x = 1;
static {
this.x = this.x + 1;
}
}
"#;
let zig = transpile_and_check(js, "test_static_block_this_read");
assert!(
!zig.contains("@compileError"),
"Should not produce compileError, got:\n{}",
zig
);
assert!(
zig.contains("__Foo_x"),
"Expected __Foo_x for this.x access:\n{}",
zig
);
}
#[test]
fn test_static_block_this_write() {
let js = r#"
class Foo {
static x = 1;
static {
this.x = 42;
}
}
"#;
let zig = transpile_and_check(js, "test_static_block_this_write");
assert!(
!zig.contains("@compileError"),
"Should not produce compileError, got:\n{}",
zig
);
assert!(
zig.contains("__Foo_x = 42"),
"Expected '__Foo_x = 42' for this.x = 42:\n{}",
zig
);
}
#[test]
fn test_array_with() {
let js = r#"
/**
* @returns {number}
*/
export function testWith() {
const arr = [1, 2, 3];
const arr2 = arr.with(1, 99);
return arr2[1];
}
"#;
let zig = transpile_and_check(js, "test_array_with");
assert!(
zig.contains("__with"),
"Expected __with variable in:\n{}",
zig
);
assert!(
zig.contains("appendSlice"),
"Expected clone via appendSlice in:\n{}",
zig
);
}
#[test]
fn test_array_to_reversed() {
let js = r#"
/**
* @returns {number}
*/
export function testToReversed() {
const arr = [1, 2, 3];
const arr2 = arr.toReversed();
return arr2[0];
}
"#;
let zig = transpile_and_check(js, "test_array_to_reversed");
assert!(
zig.contains("__rev"),
"Expected __rev variable in:\n{}",
zig
);
assert!(
zig.contains("append"),
"Expected append in reversed loop in:\n{}",
zig
);
}
#[test]
fn test_not_implemented_string_raw() {
assert_not_implemented(
r#"
function test() {
return String.raw`hello\nworld`;
}
"#,
"String.raw (tagged template static method)",
);
}
#[test]
fn test_not_implemented_map_group_by() {
assert_not_implemented(
r#"
function groupByAge(people) {
return Map.groupBy(people, (p) => p.age > 18 ? "adult" : "child");
}
"#,
"Map.groupBy() (ES2024)",
);
}
#[test]
fn test_not_implemented_set_operations() {
assert_not_implemented(
r#"
function test() {
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
return a.difference(b);
}
"#,
"Set.prototype.difference() (ES2025 Set operation)",
);
}
#[test]
fn test_not_implemented_set_union() {
assert_not_implemented(
r#"
function test() {
const a = new Set([1, 2]);
const b = new Set([2, 3]);
return a.union(b);
}
"#,
"Set.prototype.union() (ES2025 Set operation)",
);
}
#[test]
fn test_not_implemented_set_is_subset_of() {
assert_not_implemented(
r#"
function test() {
const a = new Set([1, 2]);
const b = new Set([1, 2, 3]);
return a.isSubsetOf(b);
}
"#,
"Set.prototype.isSubsetOf() (ES2025 Set operation)",
);
}
#[test]
fn test_not_implemented_object_get_own_property_symbols() {
assert_not_implemented(
r#"
function test(obj) {
return Object.getOwnPropertySymbols(obj);
}
"#,
"Object.getOwnPropertySymbols()",
);
}
#[test]
fn test_object_group_by() {
let js = r#"
/**
* @returns {JsAny}
*/
export function test() {
const items = [1, 2, 3];
return Object.groupBy(items, (item) => item > 1 ? "big" : "small");
}
"#;
let zig = transpile_and_check(js, "test_object_group_by");
assert!(
zig.contains("_grp_map"),
"Expected _grp_map in groupBy emit:\n{}",
zig
);
}
#[test]
fn test_date_set_time() {
let js = r#"
function test() {
const d = new Date();
return d.setTime(0);
}
"#;
let zig = transpile_and_check(js, "test_date_set_time");
assert!(zig.contains("setTime"), "Expected setTime in:\n{}", zig);
}
#[test]
fn test_date_to_utc_string() {
let js = r#"
function test() {
const d = new Date();
return d.toUTCString();
}
"#;
let zig = transpile_and_check(js, "test_date_to_utc_string");
assert!(
zig.contains("toUTCString"),
"Expected toUTCString in:\n{}",
zig
);
}
#[test]
fn test_not_implemented_eval() {
assert_not_implemented(
r#"
function test() {
return eval("1 + 2");
}
"#,
"eval() (security risk)",
);
}
#[test]
fn test_regexp_source() {
let js = r#"
function test() {
const re = /abc/g;
return re.source;
}
"#;
let zig = transpile_and_check(js, "test_regexp_source");
assert!(
zig.contains("pattern"),
"Expected pattern field access in:\n{}",
zig
);
}
#[test]
fn test_not_implemented_promise() {
assert_not_implemented(
r#"
function test() {
const p = new Promise((resolve, reject) => { resolve(1); });
return p;
}
"#,
"new Promise() (use async/await instead)",
);
}
#[test]
fn test_not_implemented_promise_static_methods() {
assert_not_implemented(
r#"
function test() {
return Promise.all([1, 2, 3]);
}
"#,
"Promise.all() (use async/await + host functions instead)",
);
}
#[test]
fn test_not_implemented_intl_static_methods() {
assert_not_implemented(
r#"
function test() {
const fmt = Intl.NumberFormat("en-US");
return fmt;
}
"#,
"Intl.NumberFormat() (use Zig/C library for internationalization)",
);
}
#[test]
fn test_not_implemented_weakmap() {
assert_not_implemented(
r#"
function test() {
const wm = new WeakMap();
return wm;
}
"#,
"WeakMap (Zig memory model different)",
);
}
#[test]
fn test_not_implemented_weakset() {
assert_not_implemented(
r#"
function test() {
const ws = new WeakSet();
return ws;
}
"#,
"WeakSet (Zig memory model different)",
);
}
#[test]
fn test_not_implemented_reflect() {
assert_not_implemented(
r#"
function test(obj) {
return Reflect.has(obj, "x");
}
"#,
"Reflect API (not needed in Zig)",
);
}
#[test]
fn test_not_implemented_intl() {
assert_not_implemented(
r#"
function test() {
const fmt = new Intl.NumberFormat("en-US");
return fmt.format(1234.5);
}
"#,
"Intl (use Zig/C library instead)",
);
}
#[test]
fn test_bigint_constructor() {
let zig = transpile_and_assert(
r#"
function test() {
return BigInt(123);
}
"#,
"test_bigint_constructor",
);
assert!(
zig.contains("fromValue"),
"BigInt(123) should generate fromValue code, got:\n{}",
zig
);
}
#[test]
fn test_not_implemented_atomics() {
assert_not_implemented(
r#"
function test(arr) {
return Atomics.load(arr, 0);
}
"#,
"Atomics (niche scenario)",
);
}
#[test]
fn test_for_loop_nonzero_init() {
let js = r#"
/**
* @returns {i64}
*/
export function sumFrom1() {
let sum = 0;
for (let i = 1; i <= 5; i = i + 1) {
sum = sum + i;
}
return sum;
}
"#;
let zig = transpile_and_assert(js, "test_for_loop_nonzero_init");
assert!(
zig.contains("var i: i64 = 1"),
"Expected 'var i: i64 = 1' (not 0) in generated code:\n{}",
zig
);
assert!(
!zig.contains("var i: i64 = 0"),
"Should not contain 'var i: i64 = 0' when init is 1:\n{}",
zig
);
}
#[test]
fn test_array_to_sorted() {
let js = r#"
/**
* @returns {number}
*/
export function testToSorted() {
const arr = [3, 1, 2];
const arr2 = arr.toSorted();
return arr2[0];
}
"#;
let zig = transpile_and_check(js, "test_array_to_sorted");
assert!(
zig.contains("__sorted"),
"Expected __sorted variable in:\n{}",
zig
);
assert!(zig.contains("sort"), "Expected sort call in:\n{}", zig);
}
#[test]
fn test_array_to_spliced() {
let js = r#"
/**
* @returns {number}
*/
export function testToSpliced() {
const arr = [1, 2, 3, 4];
const arr2 = arr.toSpliced(1, 2);
return arr2[1];
}
"#;
let zig = transpile_and_check(js, "test_array_to_spliced");
assert!(zig.contains("__sp"), "Expected __sp variable in:\n{}", zig);
assert!(
zig.contains("orderedRemove"),
"Expected orderedRemove in:\n{}",
zig
);
}
#[test]
fn test_regexp_flags() {
let js = r#"
function test() {
const re = /abc/gi;
return re.flags;
}
"#;
let zig = transpile_and_check(js, "test_regexp_flags");
assert!(
zig.contains("flags"),
"Expected flags field access in:\n{}",
zig
);
}
#[test]
fn test_regexp_global() {
let js = r#"
function test() {
const re = /abc/g;
return re.global;
}
"#;
let zig = transpile_and_check(js, "test_regexp_global");
assert!(
zig.contains("global"),
"Expected global field access in:\n{}",
zig
);
}
#[test]
fn test_implemented_instanceof_with_annotation() {
let js = r#"
/**
* @returns {bool}
*/
function check(arr) {
return arr instanceof Array;
}
"#;
let zig = transpile_and_check(js, "test_implemented_instanceof_with_annotation");
assert!(
!zig.contains("@compileError"),
"Should not contain @compileError:\n{}",
zig
);
assert!(
zig.contains("instanceOf") || zig.contains("true") || zig.contains("false"),
"Expected instanceof resolution in:\n{}",
zig
);
}
#[test]
fn test_not_implemented_eval_with_annotation() {
assert_not_implemented(
r#"
/**
* @returns {i64}
*/
function test() {
return eval("1 + 2");
}
"#,
"eval() with @returns annotation",
);
}
#[test]
fn test_native_proto_ternary_concat_parens() {
let js = r#"
export function format(x) {
return "value: " + (x > 5 ? "big" : "small");
}
"#;
let zig = transpile_and_assert(js, "test_ternary_concat_parens");
println!("=== Ternary concat with parens ===\n{}", zig);
assert!(
zig.contains("{s}"),
"Expected {{s}} format specifier for string ternary, got:\n{}",
zig
);
assert!(
!zig.contains("{}"),
"Should NOT use {{}} for string ternary, but got:\n{}",
zig
);
assert!(
zig.contains("std.fmt.allocPrint"),
"Expected allocPrint for concat, got:\n{}",
zig
);
}
#[test]
fn test_native_proto_ternary_concat_no_parens() {
let js = r#"
export function format(x) {
return "value: " + x > 5 ? "big" : "small";
}
"#;
let zig = transpile_and_assert(js, "test_ternary_concat_no_parens");
println!("=== Ternary concat without parens ===\n{}", zig);
assert!(
zig.contains("std.fmt.allocPrint"),
"Expected allocPrint for concat, got:\n{}",
zig
);
}
#[test]
fn test_native_proto_comparison_strict_eq_bool() {
let js = r#"
export function isEqual(a, b) {
return a === b;
}
"#;
let zig = transpile_and_assert(js, "test_comparison_strict_eq_bool");
println!("=== Comparison strict eq ===\n{}", zig);
assert!(
zig.contains("pub fn isEqual(") && zig.contains(") bool"),
"Expected return type bool for === on indeterminate operands:\n{}",
zig
);
}
#[test]
fn test_native_proto_comparison_neq_bool() {
let js = r#"
export function isNotEqual(a, b) {
return a !== b;
}
"#;
let zig = transpile_and_assert(js, "test_comparison_neq_bool");
println!("=== Comparison neq ===\n{}", zig);
assert!(
zig.contains(") bool"),
"Expected return type bool for !== on indeterminate operands:\n{}",
zig
);
}
#[test]
fn test_native_proto_comparison_lt_bool() {
let js = r#"
export function isLess(a, b) {
return a < b;
}
"#;
let zig = transpile_and_assert(js, "test_comparison_lt_bool");
println!("=== Comparison less than ===\n{}", zig);
assert!(
zig.contains(") bool"),
"Expected return type bool for < on indeterminate operands:\n{}",
zig
);
}
#[test]
fn test_native_proto_comparison_complex_bool() {
let js = r#"
export function valueIsNaN(x) {
return x !== x ? "NaN" : "ok";
}
"#;
let zig = transpile_and_assert(js, "test_comparison_complex_bool");
println!("=== Comparison complex ===\n{}", zig);
assert!(
zig.contains("pub fn valueIsNaN("),
"Expected valueIsNaN to transpile successfully:\n{}",
zig
);
}
#[test]
fn test_bigint_for_loop_compound_assign() {
let js = r#"
/**
* @param {bigint} sum
* @returns {bigint}
*/
export function test(sum) {
for (var i = 0; i < 3; i++) {
sum += BigInt(i);
}
return sum;
}
"#;
let zig = transpile_and_assert(js, "test_bigint_for_loop_compound");
println!("=== BigInt for-loop compound assign ===\n{}", zig);
assert!(
!zig.contains("sum +="),
"BigInt compound assignment should not use Zig +=:\n{}",
zig
);
}
#[test]
fn test_cross_type_str_vs_number_strict_eq() {
let js = r#"
/**
* @param {string} s
* @param {number} n
*/
export function strEqNum(s, n) {
return s === n;
}
"#;
let zig = transpile_and_assert(js, "test_cross_type_str_vs_num_strict_eq");
println!("=== Cross-type str === num ===\n{}", zig);
assert!(
zig.contains("JsAny.from("),
"Expected JsAny.from() wrapping for cross-type comparison:\n{}",
zig
);
}
#[test]
fn test_cross_type_bool_vs_number_eq() {
let js = r#"
/**
* @param {boolean} b
* @param {number} n
*/
export function boolEqNum(b, n) {
return b == n;
}
"#;
let zig = transpile_and_assert(js, "test_cross_type_bool_vs_num_eq");
println!("=== Cross-type bool == num ===\n{}", zig);
assert!(
zig.contains("JsAny.from("),
"Expected JsAny.from() wrapping for bool vs number comparison:\n{}",
zig
);
}