Skip to content

Commit

Permalink
Add galvan tests for if expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusnaumann committed Jan 12, 2024
1 parent e9333aa commit 0133de5
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
24 changes: 24 additions & 0 deletions galvan-test/src/control_flow.galvan
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test "If clause" {
if true {
assert(true)
} else {
assert(false)
}
}

test "If expression" {
let some = if 6 == 6 { "Something" }

// TODO: Auto-unwrap optionals in comparisons to make this work
// assert some == "Something"

let nothing = if 6 == 7 { "Something" }

// TODO: Introduce none keyword
// assert nothing == none
}

test "If-Else expression" {
let result = if 6 == 7 { "Correct" } else { "Wrong" }
assert result == "Wrong"
}
23 changes: 23 additions & 0 deletions galvan-test/src/logical.galvan
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test "Logical AND" {
let a = true
let b = false

assert(a and b == false)

let a = true
let b = true

assert(a and b)
}

test "Logical OR" {
let a = true
let b = false

assert(a or b)

let a = false
let b = false

assert(a or b == false)
}
16 changes: 16 additions & 0 deletions galvan-test/src/string.galvan
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn greet(name: String) -> String {
"Hello, {name}!"
}

test "String interpolation" {
assert "Hello, World!" == greet("World")
assert "Hello, Galvan!" == greet("Galvan")
}

// TODO: String interpolation should be able to take any expression
test "Interpolation with Integers" {
let x = 3
let y = 7
let sum = x + y
assert "3 + 7 = 10" == "{x} + {y} = {sum}"
}
35 changes: 35 additions & 0 deletions galvan-test/src/struct.galvan
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Dog {
name: String
age: Int
}

test "Create struct" {
let dog = Dog(name: "Rex", age: 3)
assert dog.name == "Rex"
assert dog.age == 3
}

fn shout_name(self: Dog) -> String {
self.name.to_uppercase()
}

test "Call method" {
let dog = Dog(name: "Rex", age: 3)
assert dog.shout_name() == "REX"
}

fn happy_birthday(mut self: Dog) {
self.age = self.age + 1
}

test "Call method with mutation" {
mut dog = Dog(name: "Rex", age: 3)
dog.happy_birthday()
assert dog.age == 4
}

test "Mutate struct" {
mut dog = Dog(name: "Rex", age: 3)
dog.name = "Bello"
assert dog.name == "Bello"
}
13 changes: 12 additions & 1 deletion galvan-transpiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,18 @@ fn transpile_segmented(
fn transpile_tests(segmented_asts: &SegmentedAsts, ctx: &Context, scope: &mut Scope) -> String {
fn test_name<'a>(desc: &Option<StringLiteral>) -> Cow<'a, str> {
desc.as_ref().map_or("test".into(), |desc| {
let snake = desc.as_str().trim_matches('\"').to_case(Case::Snake);
let snake = desc
.as_str()
.trim_matches('\"')
.to_case(Case::Snake)
.replace(|c: char| !c.is_ascii_alphanumeric(), "_");

let snake = if snake.starts_with(|c: char| c.is_ascii_digit()) {
format!("test_{}", snake)
} else {
snake
};

if snake.ends_with(|c: char| c.is_ascii_digit()) {
format!("{}_", snake).into()
} else {
Expand Down
6 changes: 6 additions & 0 deletions galvan-transpiler/src/transpile_item/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ impl Transpile for Body {
let mut body_scope = Scope::child(scope);
let scope = &mut body_scope;

let last = match self.statements.last() {
Some(Statement::Declaration(_)) | Some(Statement::Assignment(_)) => ";",
_ => "",
};

format!(
"{{\n{}\n}}",
self.statements
.iter()
.map(|stmt| stmt.transpile(ctx, scope))
.join(";\n")
+ last
)
}
}
Expand Down

0 comments on commit 0133de5

Please sign in to comment.