import { expect, describe, test } from 'bun:test' import '../shrimp.grammar' // Importing this so changes cause it to retest! describe('if/elseif/else', () => { test('parses single line if', () => { expect(`if y == 1: 'cool' end`).toMatchTree(` IfExpr keyword if ConditionalOp Identifier y EqEq == Number 1 colon : SingleLineThenBlock String StringFragment cool keyword end `) expect('a = if x: 2 end').toMatchTree(` Assign AssignableIdentifier a Eq = IfExpr keyword if Identifier x colon : SingleLineThenBlock Number 2 keyword end `) }) test('parses multiline if', () => { expect(` if x < 9: yes end`).toMatchTree(` IfExpr keyword if ConditionalOp Identifier x Lt < Number 9 colon : ThenBlock FunctionCallOrIdentifier Identifier yes keyword end `) }) test('parses multiline if with else', () => { expect(`if with-else: x else: y end`).toMatchTree(` IfExpr keyword if Identifier with-else colon : ThenBlock FunctionCallOrIdentifier Identifier x ElseExpr keyword else colon : ThenBlock FunctionCallOrIdentifier Identifier y keyword end `) }) test('parses multiline if with elseif', () => { expect(`if with-elseif: x elseif another-condition: y end`).toMatchTree(` IfExpr keyword if Identifier with-elseif colon : ThenBlock FunctionCallOrIdentifier Identifier x ElseIfExpr keyword elseif Identifier another-condition colon : ThenBlock FunctionCallOrIdentifier Identifier y keyword end `) }) test('parses multiline if with multiple elseif and else', () => { expect(`if with-elseif-else: x elseif another-condition: y elseif yet-another-condition: z else: oh-no end`).toMatchTree(` IfExpr keyword if Identifier with-elseif-else colon : ThenBlock FunctionCallOrIdentifier Identifier x ElseIfExpr keyword elseif Identifier another-condition colon : ThenBlock FunctionCallOrIdentifier Identifier y ElseIfExpr keyword elseif Identifier yet-another-condition colon : ThenBlock FunctionCallOrIdentifier Identifier z ElseExpr keyword else colon : ThenBlock FunctionCallOrIdentifier Identifier oh-no keyword end `) }) test('does not parse identifiers that start with if', () => { expect('iffy = if true: 2 end').toMatchTree(` Assign AssignableIdentifier iffy Eq = IfExpr keyword if Boolean true colon : SingleLineThenBlock Number 2 keyword end `) }) })