From 37920f3b983d3b06aaf6c792105d4fdd99f60bd4 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Fri, 8 May 2026 11:09:43 +0200 Subject: [PATCH] csys testing: helpers for sending and receiving complex messages --- lib/csys/program.ex | 3 ++- lib/shape/shape.ex | 4 ++++ test/csys_test.exs | 18 ++++++++++++++---- test/shape_test.exs | 5 ++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/csys/program.ex b/lib/csys/program.ex index 04ceab6..8d2aadf 100644 --- a/lib/csys/program.ex +++ b/lib/csys/program.ex @@ -18,10 +18,11 @@ defmodule Scopes.CSys.Program do # processors - def basic_active({head, _info}, scope) do + def basic_active(msg = {head, _info}, scope) do case head do [:csys, :create, :parent | _rest] -> create_parent(scope) [:csys, :create, :child | _rest] -> create_child(scope) + _ -> forward(msg, scope) || notify(msg, scope) end end diff --git a/lib/shape/shape.ex b/lib/shape/shape.ex index d9150f9..8ac8149 100644 --- a/lib/shape/shape.ex +++ b/lib/shape/shape.ex @@ -2,4 +2,8 @@ defmodule Scopes.Shape do def create(head, info \\ []) do {head, info} end + + def head(rec), do: elem(rec, 0) + def info(rec), do: elem(rec, 1) + def data(rec), do: info(rec)[:data] end diff --git a/test/csys_test.exs b/test/csys_test.exs index 58d4f7a..02a5f90 100644 --- a/test/csys_test.exs +++ b/test/csys_test.exs @@ -6,6 +6,17 @@ defmodule Scopes.CSysTest do alias Scopes.CSys alias Scopes.CSys.Environ alias Scopes.CSys.Program + alias Scopes.Shape + + def send_value(rcvr, val) do + msg = Shape.create([:csys, :data], data: %{value: val}) + Actor.send(rcvr, msg) + end + + def receive_head(head) do + assert_receive {^head, info} + {head, info} + end describe "basic:" do test "minimal-neural-net" do @@ -13,10 +24,9 @@ defmodule Scopes.CSysTest do Environ.put_prog(env, :basic, Program.basic_prog()) proc = Environ.get_stage(env, :basic, :initial) zero = CSys.neuron({[], proc, [], env}) - Actor.send(zero, "Hello Zero!") - # msg = Message.create([:csys, :data], data: %{value: "Hello Zero!"}) - # Actor.send(zero, msg) - assert_receive "Hello Zero!" + #Actor.send(zero, "Hello Zero!") + send_value(zero, "Hello Zero!") + assert "Hello Zero!" = Shape.data(receive_head([:csys, :data])).value Actor.send(zero, {[:csys, :create, :parent], nil}) assert_receive {:created, new1} Actor.send(new1, {[:csys, :create, :child], nil}) diff --git a/test/shape_test.exs b/test/shape_test.exs index 8a977d2..55d6255 100644 --- a/test/shape_test.exs +++ b/test/shape_test.exs @@ -5,9 +5,8 @@ defmodule Scopes.ShapeTest do describe "shape:" do test "basic" do - obj = Shape.create([:dom, :data], data: %{value: 42}) - {_head, info} = obj - assert 42 = info[:data].value + rec = Shape.create([:dom, :data], data: %{value: 42}) + assert 42 = Shape.data(rec).value end end end