use crate::pretty_printer::{
assert_pretty_prints, assert_round_trips, assert_round_trips_with_types,
};
use luau_common::flags;
#[test]
fn index_name_spaces_around_tokens() {
assert_round_trips_with_types("local _ = a.name");
assert_round_trips_with_types("local _ = a .name");
assert_round_trips_with_types("local _ = a. name");
}
#[test]
fn index_name_ends_with_digit() {
assert_round_trips_with_types("sparkles.Color = Color3.new()");
}
#[test]
fn pretty_print_index_expr() {
assert_round_trips_with_types("local a = {1, 2, 3} local b = a[2]");
}
#[test]
fn index_expr_spaces_around_tokens() {
assert_round_trips_with_types("local _ = a[2]");
assert_round_trips_with_types("local _ = a [2]");
assert_round_trips_with_types("local _ = a[ 2]");
assert_round_trips_with_types("local _ = a[2 ]");
}
#[test]
fn pretty_print_unary() {
assert_round_trips_with_types(
r#"
local a = 1
local b = -1
local c = true
local d = not c
local e = 'hello'
local d = #e
"#,
);
}
#[test]
fn unary_spaces_around_tokens() {
assert_round_trips_with_types(
r#"
local _ = -1
local _ = - 1
local _ = not true
local _ = not true
local _ = #e
local _ = # e
"#,
);
}
#[test]
fn binary_spaces_around_tokens() {
assert_round_trips_with_types(
r#"
local _ = 1+1
local _ = 1 +1
local _ = 1+ 1
"#,
);
}
#[test]
fn pretty_print_break_continue() {
assert_round_trips_with_types(
r#"
local a, b, c
repeat
if a then break end
if b then continue end
until c
"#,
);
}
#[test]
fn pretty_print_compound_assignment() {
assert_round_trips_with_types(
r#"
local a = 1
a += 2
a -= 3
a *= 4
a /= 5
a //= 5
a %= 6
a ^= 7
a ..= ' - result'
"#,
);
}
#[test]
fn compound_assignment_spaces_around_tokens() {
assert_round_trips_with_types(r" a += 1 ");
assert_round_trips_with_types(r" a += 1 ");
}
#[test]
fn pretty_print_assign_multiple() {
assert_round_trips_with_types("a, b, c = 1, 2, 3");
}
#[test]
fn pretty_print_assign_spaces_around_tokens() {
assert_round_trips("a = 1");
assert_round_trips("a = 1");
assert_round_trips("a = 1");
assert_round_trips("a , b = 1, 2");
assert_round_trips("a, b = 1, 2");
assert_round_trips("a, b = 1 , 2");
assert_round_trips("a, b = 1, 2");
}
#[test]
fn pretty_print_generic_function() {
assert_round_trips_with_types(
r#"
local function foo<T,S...>(a: T, ...: S...) return 1 end
local f: <T,S...>(T, S...)->(number) = foo
"#,
);
}
#[test]
fn pretty_print_for_in_multiple() {
assert_round_trips_with_types("for k,v in next,{}do print(k,v) end");
}
#[test]
fn pretty_print_for_in_multiple_types() {
assert_round_trips_with_types("for k:string,v:boolean in next,{}do end");
}
#[test]
fn pretty_print_function_attributes() {
let _export = flags::LuauExportValueSyntax.scoped(true);
assert_round_trips_with_types(
r#"
@native
function foo()
end
"#,
);
assert_round_trips_with_types(
r#"
@native
local function foo()
end
"#,
);
assert_round_trips_with_types(
r#"
@checked local function foo()
end
"#,
);
assert_round_trips_with_types(
r#"
local foo = @native function() end
"#,
);
assert_round_trips_with_types(
r#"
@native
function foo:bar()
end
"#,
);
assert_round_trips_with_types(
r#"
@native @checked
function foo:bar()
end
"#,
);
assert_round_trips_with_types(
r#"
@debugnoinline
local function t() end
"#,
);
assert_round_trips_with_types(
r#"
@[deprecated {
use = "newApi()",
reason = "newApi is faster and supports all value types.",
}]
local function oldApi()
end
"#,
);
assert_round_trips_with_types(
r#"
@[deprecated {use = "newApi()"}, native]
local function oldFastApi()
end
"#,
);
assert_round_trips_with_types(
r#"
@[deprecated({use = "newApi()"})]
local function oldFastApi()
end
"#,
);
assert_round_trips_with_types(
r#"
@[deprecated {
use = "newApi()",
reason = "newApi is faster and supports all value types.",
}, native]
function oldApi()
end
"#,
);
assert_round_trips_with_types(
r#"
@checked
@[ deprecated , native ]
function oldApi()
end
"#,
);
assert_round_trips_with_types(
r#"
@checked
@[ deprecated , native ]
export function oldApi()
end
"#,
);
assert_round_trips_with_types(
r#"
local foo = @checked
@[ deprecated , native ]
function()
end
"#,
);
}
#[test]
fn pretty_print_explicit_type_instantiations() {
let code = "f<<A, B, C...>>() t.f<<A, B, C...>>() t:f<<A, B, C>>()";
assert_round_trips_with_types(code);
assert_pretty_prints(
code,
"f () t.f () t:f ()",
);
assert_round_trips_with_types(
"f < < A , B , C... > >( ) t.f < < A, B, C... > > ( ) t:f< < A, B, C > > ( )",
);
}