Standard Library
Testing Framework

Test Module

The test module provides built-in testing utilities for writing and running tests.

Basic Assertion

Use assert to verify conditions:

// Basic assertion
assert(5 > 3, "Five should be greater than three");

Test Functions

Use test to define test cases:

test("Addition test", func() {
    let result = 2 + 2;
    assert(result == 4, "2 + 2 should equal 4");
});
 
test("String operations", func() {
    let str = "hello";
    assert(len(str) == 5, "String length should be 5");
    assert(upper(str) == "HELLO", "Uppercase conversion failed");
});

Complete Test Suite Example

// Test math functions
test("Math operations", func() {
    assert(add(2, 3) == 5, "Addition failed");
    assert(multiply(4, 5) == 20, "Multiplication failed");
    assert(power(2, 3) == 8, "Power operation failed");
});
 
// Test string functions
test("String functions", func() {
    let str = "hello world";
    assert(len(str) == 11, "Length calculation failed");
    assert(upper(str) == "HELLO WORLD", "Uppercase failed");
    assert(contains(str, "world"), "Contains check failed");
});
 
// Test array operations
test("Array operations", func() {
    let arr = [1, 2, 3];
    arr = push(arr, 4);
    assert(len(arr) == 4, "Push failed");
    assert(arr[3] == 4, "Element access failed");
});
 
// Test hash operations
test("Hash operations", func() {
    let user = {"name": "Alice", "age": 30};
    assert("name" in user, "Key check failed");
    assert(user["age"] == 30, "Value access failed");
});

Testing Functions

func add(a, b) {
    return a + b;
}
 
func multiply(x, y) {
    return x * y;
}
 
test("add function", func() {
    assert(add(1, 2) == 3, "add(1, 2) should equal 3");
    assert(add(-1, 1) == 0, "add(-1, 1) should equal 0");
});
 
test("multiply function", func() {
    assert(multiply(3, 4) == 12, "multiply(3, 4) should equal 12");
    assert(multiply(0, 5) == 0, "multiply(0, 5) should equal 0");
});

Testing with Error Handling

func divide(a, b) {
    if (b == 0) {
        throw "Division by zero";
    }
    return a / b;
}
 
test("divide function", func() {
    assert(divide(10, 2) == 5, "Normal division failed");
 
    try {
        divide(10, 0);
        assert(false, "Should have thrown error");
    } catch (e) {
        assert(contains(e, "zero"), "Wrong error message");
    }
});

Testing Modules

// Test JSON module
test("JSON operations", func() {
    let obj = {"name": "Alice", "age": 30};
    let json_str = json.stringify(obj);
    let parsed = json.parse(json_str);
    assert(parsed["name"] == "Alice", "JSON roundtrip failed");
});
 
// Test file system
test("File operations", func() {
    fs.writeFile("test.txt", "Hello");
    let content = fs.readFile("test.txt");
    assert(content == "Hello", "File read/write failed");
    fs.writeFile("test.txt", "");  // Cleanup
});

Test Functions Reference

FunctionPurposeExample
test(description, function)Run a test case with descriptiontest("My test", func() { ... })
assert(condition, message)Assert that a condition is true, throws error if falseassert(x > 0, "x must be positive")

Best Practices

1. Write Clear Test Descriptions

// Good
test("User registration with valid email", func() { ... });
 
// Avoid
test("Test 1", func() { ... });

2. Test One Thing Per Test

// Good - focused test
test("Email validation", func() {
    assert(validateEmail("test@example.com"), "Valid email should pass");
});
 
// Avoid - multiple concerns
test("User validation", func() {
    assert(validateEmail("test@example.com"), "Email");
    assert(validateAge(25), "Age");
    assert(validateName("Alice"), "Name");
});

3. Use Descriptive Assertion Messages

// Good
assert(
  result == expected,
  "Expected " + tostring(expected) + " but got " + tostring(result)
);
 
// Avoid
assert(result == expected, "Failed");

Next Steps