Skip to content

Tests as examples

Starcel's tests double as its examples. The code shown in the From the tests boxes on these pages is copied from the test files every time this site is built, so it always matches the code that actually runs.

The spawn tests (in the game)

Content/Scripts/test_spawn.py has one test_* function per feature. Each spawns its objects in a band along the Y axis starting at base_y, and test_spawn_all() runs a set of them side by side and writes a PASS/FAIL report to the Output Log.

Run them from a code cell or the console:

from test_spawn import test_spawn_all

test_spawn_all()                              # everything
test_spawn_all(tests=['table', 'entity'])     # some, with or without the test_ prefix

main.py runs a set of them when the map starts:

results = test_spawn_all(uobject=self.uobject, input_manager=self.input,
                         tests=['test_table', 'test_plot', 'test_manim', 'test_ga_graph', ...])

Pass uobject and input_manager, as main.py does, to also run the interactive tests (test_gizmo, test_text3d_click).

Test Shows Page
test_primitives, test_image, test_video, test_sound, test_3d_object, test_glb_object_runtime Shapes, pictures, video, sound, 3D models spawn
test_cameras, test_earth Camera presets, Cesium Earth spawn
test_text3d_executor, test_gizmo, test_icon, test_desktop_icons, test_file_explorer, test_system_monitor Code cells and desktop tools spawn
test_table, test_3d_table_resize, test_3d_table_5x5x5, test_nd_table, test_nd_table_text, test_nd_table_spread Tables Tables
test_plot, test_math_examples, test_swaptube, test_zoom_plot Plots Plots
test_ga_graph Geometric algebra Geometric algebra
test_manim Manim scenes Manim
test_latex LaTeX LaTeX
test_entity Ursina-style entities Entities
test_change_background Every background, then the one before Backgrounds
test_camera_settings The camera settings table Camera settings

The offline tests

These run in plain Python 3.9, without Unreal. Run them from Content/Scripts:

python -m ue_math.test_ga_graph
python -m ue_math.manim_render.test_manim_render
python -m ue_math.latex.test_latex
python -m ue_math.nd_table.test_zarr

Adding an example to the docs

Write a test, then put a line like this on its own in any page under docs/:

??? example "From the tests: test_table (Content/Scripts/test_spawn.py:737)"

    ```python
    def test_table(base_y=0):
        """Spawn two nd_tables: a basic 3×4 file listing and an advanced formula
        spreadsheet that exercises in-cell Python, spreadsheet labels (A–C, AA, AB),
        named cells, and cross-cell dependencies."""
        from ue_spawn import spawn_table
        results = {}

        # Basic table (unchanged)
        renderer = None
        t0 = time.monotonic()
        try:
            from ue_math.nd_table.ndtable import Table
            t = Table(shape=(3, 4))
            t[(0, 0)] = 'Name';    t[(0, 1)] = 'Size';    t[(0, 2)] = 'Date';    t[(0, 3)] = 'Type'
            t[(1, 0)] = 'file.py'; t[(1, 1)] = '10 KB';   t[(1, 2)] = '2024-01'; t[(1, 3)] = '.py'
            t[(2, 0)] = 'readme';  t[(2, 1)] = '1 KB';    t[(2, 2)] = '2024-01'; t[(2, 3)] = '.md'
            renderer = spawn_table(t, location=FVector(0, base_y, 500))
        except Exception as e:
            _log_exception('table', e)
        n_cells = len(renderer.cell_actors) if renderer and hasattr(renderer, 'cell_actors') else 0
        results.update(_result('table', renderer, extra=f'{n_cells} cells',
                               elapsed=time.monotonic() - t0))

        # Advanced formula spreadsheet
        #  Uses spreadsheet labels A–C and extended AA, AB.
        #  Row 0: headers
        #  Rows 1-5: numeric data + formulas referencing other cells
        adv_renderer = None
        t0 = time.monotonic()
        try:
            from ue_math.nd_table.ndtable import Table
            adv = Table(shape=(8, 28), lazy_eval=True)  # 8 rows, 28 cols (A..AB)

            # Headers in row 0
            adv['A1'] = 'Item'
            adv['B1'] = 'Qty'
            adv['C1'] = 'Price'
            # Column D = total per row (Qty * Price)
            adv[(0, 3)] = 'Total'
            # Column E = running sum
            adv[(0, 4)] = 'RunSum'
            # Extended label columns
            adv[(0, 26)] = 'AA:Sum'     # col 26 = AA
            adv[(0, 27)] = 'AB:Avg'     # col 27 = AB

            # Data rows  (A=0, B=1, C=2)
            items = [
                ('Widget',   5,  12.50),
                ('Gadget',   3,  27.00),
                ('Bracket', 10,   4.75),
                ('Washer',  50,   0.30),
                ('Bolt',    20,   1.10),
            ]
            for i, (name, qty, price) in enumerate(items, start=2):
                row = i - 1   # row index (0-based)
                adv[(row, 0)] = name
                adv[(row, 1)] = qty
                adv[(row, 2)] = price
                # D = Qty * Price  (formula using spreadsheet labels)
                adv[(row, 3)] = f'=B{i} * C{i}'

            # Running sum in column E  (cumulative formula chain)
            adv[(1, 4)] = '=D2'
            for i in range(3, 7):
                adv[(i - 1, 4)] = f'=E{i - 1} + D{i}'

            # Named cell for grand total
            try:
                adv.name_cell('E6', 'grand_total')
            except Exception:
                pass

            # AA column (col 26): sum of all totals via formula
            adv[(1, 26)] = '=D2 + D3 + D4 + D5 + D6'
            # AB column (col 27): average using Python math in formula
            adv[(1, 27)] = '=(D2 + D3 + D4 + D5 + D6) / 5'

            # Row 7: summary using math functions (use builtins explicitly)
            adv[(6, 0)] = 'sqrt(sum)'
            adv[(6, 3)] = '=math.sqrt(D2 + D3 + D4 + D5 + D6)'
            adv[(6, 4)] = '=max(D2, D3, D4, D5, D6)'

            # Force evaluation to verify formulas work
            formula_ok = True
            try:
                val_d2 = adv['D2']  # should be 5 * 12.50 = 62.50
                val_aa = adv[(1, 26)]  # sum
                val_ab = adv[(1, 27)]  # average
                _log(f'  adv_table formulas: D2={val_d2}, AA2={val_aa}, AB2={val_ab}')
            except Exception as e:
                _log(f'  adv_table formula eval failed: {e}')
                formula_ok = False

            adv_renderer = spawn_table(
                adv, location=FVector(500, base_y + 400, 500))
            if formula_ok:
                _log(f'  adv_table: formulas evaluated successfully')
        except Exception as e:
            _log_exception('adv_table', e)

        n_adv = len(adv_renderer.cell_actors) if adv_renderer and hasattr(adv_renderer, 'cell_actors') else 0
        results.update(_result('table_formulas', adv_renderer,
                               extra=f'{n_adv} cells, labels A-C + AA + AB',
                               elapsed=time.monotonic() - t0))

        return results
    ```

    [View it on GitHub](https://github.com/recursionis/starcel/blob/master/Content/Scripts/test_spawn.py#L737)

It becomes a collapsible box with that function's source. Add open at the end to show it expanded. Class.method works for unittest methods. The hook that does this is docs/hooks/test_source.py; the build stops if the file or function is missing, so a renamed test can't leave a stale example behind.