if-statement optional braces when single statement
This commit is contained in:
@@ -4084,6 +4084,104 @@ conditional_unwrap_parser_captures_guard_and_parenthesized_chain :: proc(t: ^tes
|
||||
testing.expect_value(t, module.exprs[statement.guard].kind, ast.Expr_Kind.And)
|
||||
}
|
||||
|
||||
@(test)
|
||||
parser_accepts_braceless_if_bodies :: proc(t: ^testing.T) {
|
||||
text := `ready :: func() bool { return true }
|
||||
main :: func() void {
|
||||
x i32 = 0
|
||||
if (x == 0) x = 1
|
||||
if ready() x = 2
|
||||
if (x == 2) x = 3 else x = 4
|
||||
if (x > 0) { x = 10 } else x = 11
|
||||
v ?i32 = 5
|
||||
if v |u| _ = u
|
||||
}
|
||||
`
|
||||
source_file := source.Source{path="test.bro", text=text}
|
||||
diagnostics := source.init_diagnostics(&source_file)
|
||||
defer source.destroy_diagnostics(&diagnostics)
|
||||
symbols := symbol.init_table()
|
||||
defer symbol.destroy_table(&symbols)
|
||||
stream := lexer.lex(&source_file, &diagnostics, &symbols)
|
||||
defer delete(stream.items)
|
||||
module := parser.parse(&stream, &source_file, &diagnostics)
|
||||
defer ast.destroy_module(&module)
|
||||
|
||||
testing.expect_value(t, len(diagnostics.items), 0)
|
||||
main := module.functions[1]
|
||||
testing.expect_value(t, len(main.body), 7)
|
||||
|
||||
paren_if := module.statements[main.body[1]]
|
||||
testing.expect_value(t, paren_if.kind, ast.Stmt_Kind.If)
|
||||
testing.expect_value(t, len(paren_if.body), 1)
|
||||
testing.expect(t, module.exprs[paren_if.expr].parenthesized)
|
||||
|
||||
call_if := module.statements[main.body[2]]
|
||||
testing.expect_value(t, call_if.kind, ast.Stmt_Kind.If)
|
||||
testing.expect_value(t, len(call_if.body), 1)
|
||||
testing.expect_value(t, module.exprs[call_if.expr].kind, ast.Expr_Kind.Call)
|
||||
|
||||
if_else := module.statements[main.body[3]]
|
||||
testing.expect_value(t, len(if_else.body), 1)
|
||||
testing.expect_value(t, len(if_else.else_body), 1)
|
||||
|
||||
braced_then := module.statements[main.body[4]]
|
||||
testing.expect_value(t, len(braced_then.body), 1)
|
||||
testing.expect_value(t, len(braced_then.else_body), 1)
|
||||
|
||||
unwrap_if := module.statements[main.body[6]]
|
||||
testing.expect_value(t, len(unwrap_if.captures), 1)
|
||||
testing.expect_value(t, len(unwrap_if.body), 1)
|
||||
}
|
||||
|
||||
@(test)
|
||||
parser_diagnoses_braceless_if_without_parens_or_call :: proc(t: ^testing.T) {
|
||||
text := `main :: func() void {
|
||||
x i32 = 0
|
||||
if x == 0 x = 1
|
||||
}
|
||||
`
|
||||
source_file := source.Source{path="test.bro", text=text}
|
||||
diagnostics := source.init_diagnostics(&source_file)
|
||||
defer source.destroy_diagnostics(&diagnostics)
|
||||
symbols := symbol.init_table()
|
||||
defer symbol.destroy_table(&symbols)
|
||||
stream := lexer.lex(&source_file, &diagnostics, &symbols)
|
||||
defer delete(stream.items)
|
||||
module := parser.parse(&stream, &source_file, &diagnostics)
|
||||
defer ast.destroy_module(&module)
|
||||
|
||||
testing.expect_value(t, len(diagnostics.items), 1)
|
||||
testing.expect(t, strings.contains(diagnostics.items[0].message, "parenthesized"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
braceless_if_compiles_and_runs :: proc(t: ^testing.T) {
|
||||
directory := "/tmp/brolang-test-braceless-if"
|
||||
main_path := "/tmp/brolang-test-braceless-if/main.bro"
|
||||
output := "/tmp/brolang-test-braceless-if-output"
|
||||
text := `ready :: func() bool { return true }
|
||||
main :: func() i32 {
|
||||
x i32 = 0
|
||||
if (x == 0) x = 1 else x = 2
|
||||
if ready() x = x + 10
|
||||
y i32 = 5
|
||||
if (y == 0) y = 1 else y = 30
|
||||
x = x + y
|
||||
return x
|
||||
}
|
||||
`
|
||||
_ = os2.remove_all(directory)
|
||||
defer _ = os2.remove_all(directory)
|
||||
defer _ = os.remove(output)
|
||||
testing.expect(t, os.make_directory(directory) == nil)
|
||||
testing.expect(t, os.write_entire_file(main_path, transmute([]byte)text))
|
||||
status := compiler_core.compile_package(directory, output)
|
||||
testing.expect_value(t, status, 0)
|
||||
state := run_executable(output)
|
||||
testing.expect_value(t, state.exit_code, 41)
|
||||
}
|
||||
|
||||
@(test)
|
||||
conditional_unwrap_allows_sink_captures :: proc(t: ^testing.T) {
|
||||
text := `main :: func() void {
|
||||
|
||||
Reference in New Issue
Block a user