Skip to content

Commit

Permalink
feat: Add each block to engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Olian04 committed Jun 12, 2024
1 parent f85a064 commit b71a18f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/engine.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ fn eval_if(
}
}

fn eval_each(
ctx_list: List(dynamic.Dynamic),
children: List(compiler.AST),
) -> String {
ctx_list
|> list.fold(string_builder.new(), fn(acc, ctx) {
string_builder.append(acc, run(children, ctx))
})
|> string_builder.to_string
}

pub fn run(ast: List(compiler.AST), ctx: dynamic.Dynamic) -> String {
{
use acc, it <- list.fold(ast, string_builder.new())
Expand All @@ -134,7 +145,12 @@ pub fn run(ast: List(compiler.AST), ctx: dynamic.Dynamic) -> String {
|> bool.negate
|> eval_if(children, ctx),
)
"each" -> todo
"each" ->
string_builder.append(
acc,
get_as_list(ctx, path)
|> eval_each(children),
)
_ -> panic as "Unknown block"
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/engine_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import compiler
import engine
import gleam/dict
import gleam/dynamic
import gleam/list
import gleeunit/should

pub fn engine_should_return_correct_when_running_hello_world_test() {
Expand Down Expand Up @@ -100,3 +101,32 @@ pub fn engine_should_return_correct_when_using_falsy_unless_test() {
)
|> should.equal("42")
}

pub fn engine_should_return_correct_when_using_empty_each_test() {
engine.run(
[
compiler.Block("each", ["list"], [
compiler.Property(["name"]),
compiler.Constant(", "),
]),
],
dict.new()
|> dict.insert(
"list",
list.new()
|> list.append([
dict.new()
|> dict.insert("name", "Knatte")
|> dynamic.from,
dict.new()
|> dict.insert("name", "Fnatte")
|> dynamic.from,
dict.new()
|> dict.insert("name", "Tjatte")
|> dynamic.from,
]),
)
|> dynamic.from,
)
|> should.equal("Knatte, Fnatte, Tjatte, ")
}

0 comments on commit b71a18f

Please sign in to comment.