Skip to content

Commit

Permalink
tests: fix ForEachTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Dec 26, 2023
1 parent ad226d6 commit 75c3827
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
32 changes: 20 additions & 12 deletions src/test/java/team/unnamed/mocha/runtime/ForEachTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,31 @@

import org.junit.jupiter.api.Test;
import team.unnamed.mocha.MochaEngine;

import java.io.InputStreamReader;
import java.io.Reader;
import team.unnamed.mocha.runtime.value.ArrayValue;
import team.unnamed.mocha.runtime.value.Function;
import team.unnamed.mocha.runtime.value.NumberValue;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ForEachTest {
@Test
void test() throws Exception {
final MochaEngine<?> engine = MochaEngine.create();
engine.bindDefaults();
//engine.bindQueryFunction("list_people", (ctx, args) -> Arrays.asList("Andre", "John", "Ian", "Salva"));
void test() {
final String code = String.join("\n",
"v.sum = 0;",
"for_each(v.age, query.list_ages(), {",
" v.sum = v.sum + v.age;",
"});",
"return v.sum;");

final MochaEngine<?> engine = MochaEngine.createStandard();
engine.scope().query().set("list_ages", (Function<?>) (ctx, args) -> ArrayValue.of(
NumberValue.of(18),
NumberValue.of(16),
NumberValue.of(40),
NumberValue.of(24)
));

Object result;
try (Reader reader = new InputStreamReader(ForEachTest.class.getClassLoader().getResourceAsStream("for_each.molang"))) {
result = engine.eval(reader);
}
assertEquals("Andre, John, Ian, Salva", result);
final double result = engine.eval(code);
assertEquals(98, result);
}
}
10 changes: 0 additions & 10 deletions src/test/resources/for_each.molang

This file was deleted.

0 comments on commit 75c3827

Please sign in to comment.