use blade_syntax::{debug::sexp, parse};
fn tree(t: &str) -> String {
let p = parse(t);
assert_eq!(p.syntax().to_string(), t, "parse must be lossless");
sexp(&p.syntax())
}
fn errors(t: &str) -> Vec<String> {
parse(t)
.errors()
.iter()
.map(|e| e.message.clone())
.collect()
}
#[test]
fn pairs_if_else() {
let t = "@if($a) x @else y @endif";
assert_eq!(
tree(t),
r#"(ROOT (BLOCK (DIRECTIVE "@if" "($a)") (BODY " x ") (DIRECTIVE "@else") (BODY " y ") (DIRECTIVE "@endif")))"#
);
assert!(errors(t).is_empty());
}
#[test]
fn pairs_nested_foreach_in_if() {
assert_eq!(
tree("@if(1)@foreach($a as $b){{ $b }}@endforeach @endif"),
r#"(ROOT (BLOCK (DIRECTIVE "@if" "(1)") (BODY (BLOCK (DIRECTIVE "@foreach" "($a as $b)") (BODY (ECHO "{{" " $b " "}}")) (DIRECTIVE "@endforeach")) " ") (DIRECTIVE "@endif")))"#
);
}
#[test]
fn forelse_empty_is_middle_but_empty_with_args_opens() {
let t = "@forelse($a as $b) x @empty y @endforelse";
assert_eq!(
tree(t),
r#"(ROOT (BLOCK (DIRECTIVE "@forelse" "($a as $b)") (BODY " x ") (DIRECTIVE "@empty") (BODY " y ") (DIRECTIVE "@endforelse")))"#
);
assert!(errors(t).is_empty());
assert_eq!(
tree("@empty($a) x @endempty"),
r#"(ROOT (BLOCK (DIRECTIVE "@empty" "($a)") (BODY " x ") (DIRECTIVE "@endempty")))"#
);
}
#[test]
fn section_with_two_args_is_inline() {
assert_eq!(
tree("@section('t', 'Home')"),
r#"(ROOT (DIRECTIVE "@section" "('t', 'Home')"))"#
);
assert!(errors("@section('t', 'Home')").is_empty());
assert_eq!(
tree("@section('c') x @show"),
r#"(ROOT (BLOCK (DIRECTIVE "@section" "('c')") (BODY " x ") (DIRECTIVE "@show")))"#
);
}
#[test]
fn names_are_case_insensitive() {
let t = "@pushOnce('s') x @endPushOnce";
assert_eq!(
tree(t),
r#"(ROOT (BLOCK (DIRECTIVE "@pushOnce" "('s')") (BODY " x ") (DIRECTIVE "@endPushOnce")))"#
);
assert!(errors(t).is_empty());
}
#[test]
fn alpine_shorthand_is_a_plain_directive() {
let t = r#"<button @click="open = !open" @keyup.enter="go()">"#;
assert!(!tree(t).contains("BLOCK"), "{}", tree(t));
assert!(errors(t).is_empty());
}
#[test]
fn custom_conditional_pairs() {
let t = "@disk('local') a @elsedisk('s3') b @enddisk";
assert_eq!(
tree(t),
r#"(ROOT (BLOCK (DIRECTIVE "@disk" "('local')") (BODY " a ") (DIRECTIVE "@elsedisk" "('s3')") (BODY " b ") (DIRECTIVE "@enddisk")))"#
);
assert!(errors(t).is_empty());
}
#[test]
fn closer_for_outer_block_closes_inner_with_error() {
let t = "@foreach($a as $b)@if($b) x @endforeach";
assert_eq!(
tree(t),
r#"(ROOT (BLOCK (DIRECTIVE "@foreach" "($a as $b)") (BODY (BLOCK (DIRECTIVE "@if" "($b)") (BODY " x "))) (DIRECTIVE "@endforeach")))"#
);
assert_eq!(errors(t), ["unclosed @if: expected @endif"]);
}
#[test]
fn stray_closer_is_an_error() {
assert_eq!(errors("x @endif"), ["@endif without a matching opener"]);
}
#[test]
fn at_glued_to_a_word_is_not_a_directive() {
assert!(errors("x@endif").is_empty());
assert!(!tree("@if($a) x@endif").contains("\"@endif\""));
}
#[test]
fn unclosed_block_at_eof() {
assert_eq!(errors("@if($a) x"), ["unclosed @if: expected @endif"]);
}
#[test]
fn unclosed_args_on_an_opener_still_pair_with_the_closer() {
let t = "@if($a <p>x</p> @endif";
assert_eq!(errors(t), ["unclosed ( in directive arguments"]);
assert!(tree(t).starts_with("(ROOT (BLOCK"), "{}", tree(t));
}
#[test]
fn push_and_prepend_with_two_args_are_inline() {
for t in ["@push('body-class', 'tri-layout ')", "@prepend('s', 'x')"] {
assert!(!tree(t).contains("BLOCK"), "{}", tree(t));
assert!(errors(t).is_empty(), "{t}: {:?}", errors(t));
}
assert!(tree("@push('scripts') x @endpush").contains("BLOCK"));
}
#[test]
fn unknown_at_word_followed_by_parens_is_not_an_error() {
for t in [
"<p>Follow @calebporzio (Livewire's creator)</p>",
"Hi @john (he's here)",
"@john (aside",
] {
assert!(errors(t).is_empty(), "{t}: {:?}", errors(t));
}
assert!(
tree("@john ({{ $x }})").contains("(ECHO"),
"{}",
tree("@john ({{ $x }})")
);
assert!(tree("@john (a\n{{ $x }} )").contains("(ECHO"));
}
#[test]
fn text_after_raw_blocks_is_not_checked_for_args() {
for t in [
"@verbatim (function(){ return {{ x }} })() @endverbatim",
"@php $a = 1; @endphp (note",
] {
assert!(errors(t).is_empty(), "{t}: {:?}", errors(t));
}
}
#[test]
fn lang_opens_a_block_only_without_args_or_with_an_array() {
let t = "@lang('messages.welcome') @lang(['n' => 1]) text @endlang";
assert!(errors(t).is_empty(), "{:?}", errors(t));
assert_eq!(tree(t).matches("(BLOCK").count(), 1, "{}", tree(t));
assert!(!tree("@lang('a')").contains("BLOCK"));
}
#[test]
fn section_and_slot_with_null_content_open_blocks() {
for t in [
"@section('a', null) x @endsection",
"@slot('title', null, ['class' => 'x']) T @endslot",
] {
assert!(errors(t).is_empty(), "{t}: {:?}", errors(t));
assert!(tree(t).contains("(BLOCK"), "{}", tree(t));
}
assert!(!tree("@slot('t', null)").contains("BLOCK"));
}