Skip to content

Commit

Permalink
Allow functions to be defined in suite. (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign authored Sep 19, 2023
1 parent e4af7b9 commit ba914ba
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
10 changes: 10 additions & 0 deletions core/tests/tests/Test.mint
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ suite "Test (Async)" {
await Test.Context.of(true)
}
}

suite "Test (Function)" {
fun test : String {
""
}

test "function" {
test() == ""
}
}
16 changes: 13 additions & 3 deletions spec/examples/suite
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ suite "X" {
}
-------------------------------------------------------------------------------
suite "Suite" {
const ONE_PLUS_ONE = 1 + 1
const TWO = 2

test "Test" {
1 + 1 == TWO
}

const ONE_PLUS_ONE = 1 + 1
const TWO = 2

test "Test 2" {
ONE_PLUS_ONE == TWO
}
}
-------------------------------------------------------------------------------
suite "Suite" {
fun test : String {
""
}

test "Test" {
"" == test()
}
}
3 changes: 2 additions & 1 deletion src/ast/suite.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Mint
class Ast
class Suite < Node
getter tests, name, comments, constants
getter tests, name, comments, constants, functions

def initialize(@constants : Array(Constant),
@functions : Array(Function),
@comments : Array(Comment),
@name : StringLiteral,
@tests : Array(Test),
Expand Down
19 changes: 16 additions & 3 deletions src/compilers/suite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ module Mint
node.location.to_json

tests =
compile node.tests, ","
compile node.tests

constants =
compile_constants node.constants
compile_constants(node.constants).map do |key, value|
"#{key}: #{value}"
end

"{ name: #{name}, location: #{location}, tests: [#{tests}], constants: #{js.object(constants)} }"
functions =
compile node.functions

context =
"{ #{(constants + functions).join(",")} }"

js.object({
"tests" => js.array(tests),
"location" => location,
"context" => context,
"name" => name,
})
end
end
end
18 changes: 10 additions & 8 deletions src/compilers/test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Mint
left =
compile operation.left

<<-JS
((constants) => {
<<-COMPILED
(() => {
const context = new TestContext(#{left})
const right = #{right}
Expand All @@ -24,8 +24,8 @@ module Mint
return true
})
return context
})(constants)
JS
})()
COMPILED
end

def _compile(node : Ast::Test) : String
Expand All @@ -42,11 +42,13 @@ module Mint
case raw_expression
when Ast::Operation
_compile_operation_test(raw_expression)
end
end || compile(raw_expression)

expression ||= compile(raw_expression)

"{ name: #{name}, location: #{location}, proc: async (constants) => { return #{expression} } }"
js.object({
"proc" => "async function () { return #{expression} }",
"location" => location,
"name" => name,
})
end
end
end
2 changes: 1 addition & 1 deletion src/compilers/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module Mint
case parent
when Ast::Suite
# The variable is a constant in a test suite
"constants.#{name}()"
"this.#{name}()"
else
"this.#{name}"
end
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/suite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Mint
class Formatter
def format(node : Ast::Suite) : String
body =
list(node.constants + node.tests + node.comments)
list(node.constants + node.tests + node.comments + node.functions)

name =
format node.name
Expand Down
6 changes: 5 additions & 1 deletion src/parsers/suite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ module Mint
snippet self
end if items.none?(Ast::Test | Ast::Constant)
}
) { many { test || constant || comment } }
) { many { function || test || constant || comment } }

next unless body

constants = [] of Ast::Constant
functions = [] of Ast::Function
comments = [] of Ast::Comment
tests = [] of Ast::Test

body.each do |item|
case item
when Ast::Function
functions << item
when Ast::Constant
constants << item
when Ast::Comment
Expand All @@ -49,6 +52,7 @@ module Mint
Ast::Suite.new(
from: start_position,
constants: constants,
functions: functions,
comments: comments,
tests: tests,
to: position,
Expand Down
1 change: 1 addition & 0 deletions src/scope.cr
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ module Mint
build(node.constants, node)
build(node.functions, node)
when Ast::Suite
build(node.functions, node)
build(node.constants, node)
build(node.tests, node)
when Ast::Locale
Expand Down
2 changes: 1 addition & 1 deletion src/test_runner.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
const test = this.suite.tests.shift()
try {
const result = await test.proc(this.suite.constants)
const result = await test.proc.call(this.suite.context)
// Go back to the beginning
if (window.history.length - currentHistory) {
Expand Down

0 comments on commit ba914ba

Please sign in to comment.