Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Dedicated Test block and `amber test` command

Open zlfn opened this issue 3 weeks ago • 1 comments

Problem

Amber does not have a clear testing syntax. It is possible to create a seperate entry point for testing, but this makes the test functions too far removed from the functions under test, which is hard to read.

Suggestion

Having a dedicated test scope, similar to main, would be helpful for testing.

Running

amber test code.ab

would execute the test block instead of the main block.

code.ab will be like

main(args) {
   ... do something on main logic
}

test {
    test_something()
    test_function()
    assert_eq(something)
    ...
}

It would also be great if amber test could run the tests blocks of all Amber codes files under the current directory.

> amber test
[1/20] Executing lib/text.ab Test... Successed
[2/20] Executing lib/env.ab Test... Failed
"<Some stdout/stderr outputs generated by lib/env.ab test>"

This approach might allow Amber to achieve a level of test convenience comparable to other programming languages, without requiring major changes to the compiler’s architecture.

To enable more effective testing, it would also be helpful for std/test to provide built-in functions such as assert or assert_eq

zlfn avatar Nov 28 '25 09:11 zlfn

I love this

Ph0enixKM avatar Nov 29 '25 10:11 Ph0enixKM

It would be good Amber allows multiple test functions and runs them parallelly, so my suggestion is

main(args) {
   ... do something on main logic
}

#[test] 
test_something() {
    const something = some_funtion()
    assert(something)
}

#[test] 
test_another() {
    const another = another_funtion()
    assert(another)
}

lens0021 avatar Dec 03 '25 00:12 lens0021

I've already implemented something similar... a test block, that can be named and executed. Testing all already works in paralel.

test validate_something {
    const something = some_funtion()
    assert(something)
}

test validate_another {
    const another = another_funtion()
    assert(another)
}

Though I've been thinking if tests should be named with Text to allow for better description:

test "Validate that some_function works properly" {
    const something = some_funtion()
    assert(something)
}

test "Validate that another_function works properly" {
    const another = another_funtion()
    assert(another)
}

What do you all think @lens0021 @zlfn @Mte90 @KrosFire

Ph0enixKM avatar Dec 04 '25 10:12 Ph0enixKM

I like that version, in this way we don't need to generate the text from the function like usually other test frameworks do

Mte90 avatar Dec 04 '25 10:12 Mte90

@Mte90 You are talking about this version, right?

test "Validate that another_function works properly" { const another = another_funtion() assert(another) }

The one with text instead of identifier.

Ph0enixKM avatar Dec 04 '25 10:12 Ph0enixKM

yep that one

Mte90 avatar Dec 04 '25 10:12 Mte90

test function named with text seems to be limited for me. I have a preferences how testing framework should be, and others maybe have their one. So the Rustic way only can be middle ground.

lens0021 avatar Dec 04 '25 10:12 lens0021

Yeah it makes sense where main exists. It's a similar concept. Functionality wise in #865 test works the same way as main (but has no args). The difference is that test blocks are being run in parallel when testing. This is beneficial because it makes sense syntactically in my opinion.

@lens0021 And how do you view this syntax alternative?

test validate_something {
    const something = some_funtion()
    assert(something)
}

Ph0enixKM avatar Dec 04 '25 11:12 Ph0enixKM

@Ph0enixKM Let me walk back what I said earlier. test "validate ..." {} looks better. I was confused, it looks like HashiCorp configuration language

lens0021 avatar Dec 04 '25 11:12 lens0021

Ah I see... @lens0021 . For me this syntax looks like Jest.js library which is the testing standard in web dev.

Ph0enixKM avatar Dec 04 '25 21:12 Ph0enixKM

Looks good. Just have to think of stripping it from compiled bash and running just tests with an option to specify a test in cli

KrosFire avatar Dec 04 '25 21:12 KrosFire

@KrosFire I've already implemented the stripping the code in main block and other non-relevent test blocks in #865 I think that I should think about how to implement the running a concrete test in cli when we support string descriptions of tests... I'll check how jest does it

Ph0enixKM avatar Dec 04 '25 22:12 Ph0enixKM

From what I see Jest accepts running a subset of tests by providing a substring that matches test's description. We can implement this approach.

Ph0enixKM avatar Dec 04 '25 22:12 Ph0enixKM