Skip to content

Commit

Permalink
fix(desugarer:tests): network/union (add missing directive to __new__…
Browse files Browse the repository at this point in the history
… node)
  • Loading branch information
emil14 committed Jan 11, 2025
1 parent dbbef68 commit f45e882
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 211 deletions.
16 changes: 16 additions & 0 deletions internal/compiler/desugarer/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,22 @@ func (d *Desugarer) desugarSingleSender(
}, nil
}

if sender.Union != nil {
result, err := d.desugarUnionSender(
*sender.Union,
normConn,
nodesToInsert,
constsToInsert,
)
if err != nil {
return desugarSenderResult{}, fmt.Errorf("desugar union sender: %w", err)
}
return desugarSenderResult{

Check failure on line 804 in internal/compiler/desugarer/network.go

View workflow job for this annotation

GitHub Actions / lint

S1016: should convert result (type handleUnionSenderResult) to desugarSenderResult instead of using struct literal (gosimple)
replace: result.replace,
insert: result.insert,
}, nil
}

if sender.Ternary != nil {
result, err := d.desugarTernarySender(
iface,
Expand Down
195 changes: 195 additions & 0 deletions internal/compiler/desugarer/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,201 @@ func TestDesugarNetwork(t *testing.T) {
},
},
},
{
name: "union_sender_tag_only",
net: []src.Connection{
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{
{
Union: &src.UnionSender{
EntityRef: core.EntityRef{Name: "Input"},
Tag: "Int",
},
},
},
Receivers: []src.ConnectionReceiver{
{PortAddr: &src.PortAddr{Node: "foo", Port: "bar"}},
},
},
},
},
nodes: map[string]src.Node{
"foo": {EntityRef: core.EntityRef{Name: "Foo"}},
},
expectedResult: handleNetworkResult{
desugaredConnections: []src.Connection{
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{{
PortAddr: &src.PortAddr{
Node: "__new__1",
Port: "res",
},
}},
Receivers: []src.ConnectionReceiver{
{PortAddr: &src.PortAddr{Node: "foo", Port: "bar"}},
},
},
},
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{{
Const: &src.Const{
Value: src.ConstValue{
Message: &src.MsgLiteral{
Union: &src.UnionLiteral{
EntityRef: core.EntityRef{Name: "Input"},
Tag: "Int",
},
},
},
},
}},
Receivers: []src.ConnectionReceiver{{
PortAddr: &src.PortAddr{
Node: "__new__1",
Port: "data",
},
}},
},
},
},
nodesToInsert: map[string]src.Node{
"__new__1": {
EntityRef: core.EntityRef{
Pkg: "builtin",
Name: "New",
},
Directives: map[src.Directive][]string{
compiler.BindDirective: {"__union_const__1"},
},
},
},
constsToInsert: map[string]src.Const{
"__union_const__1": {
Value: src.ConstValue{
Message: &src.MsgLiteral{
Union: &src.UnionLiteral{
EntityRef: core.EntityRef{Name: "Input"},
Tag: "Int",
},
},
},
},
},
},
},
{
name: "union_sender_with_value",
net: []src.Connection{
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{
{
Union: &src.UnionSender{
EntityRef: core.EntityRef{Name: "Input"},
Tag: "Int",
Data: &src.ConnectionSender{
Const: &src.Const{
TypeExpr: ts.Expr{
Inst: &ts.InstExpr{
Ref: core.EntityRef{Name: "int"},
},
},
Value: src.ConstValue{
Message: &src.MsgLiteral{Int: compiler.Pointer(42)},
},
},
},
},
},
},
Receivers: []src.ConnectionReceiver{
{PortAddr: &src.PortAddr{Node: "foo", Port: "bar"}},
},
},
},
},
nodes: map[string]src.Node{
"foo": {EntityRef: core.EntityRef{Name: "Foo"}},
},
expectedResult: handleNetworkResult{
desugaredConnections: []src.Connection{
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{{
PortAddr: &src.PortAddr{
Node: "__union__1",
Port: "res",
},
}},
Receivers: []src.ConnectionReceiver{
{PortAddr: &src.PortAddr{Node: "foo", Port: "bar"}},
},
},
},
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{{
Const: &src.Const{
Value: src.ConstValue{
Message: &src.MsgLiteral{
Str: compiler.Pointer("Int"),
},
},
},
}},
Receivers: []src.ConnectionReceiver{{
PortAddr: &src.PortAddr{
Node: "__union__1",
Port: "tag",
},
}},
},
},
{
Normal: &src.NormalConnection{
Senders: []src.ConnectionSender{{
Const: &src.Const{
TypeExpr: ts.Expr{
Inst: &ts.InstExpr{
Ref: core.EntityRef{Name: "int"},
},
},
Value: src.ConstValue{
Message: &src.MsgLiteral{Int: compiler.Pointer(42)},
},
},
}},
Receivers: []src.ConnectionReceiver{{
PortAddr: &src.PortAddr{
Node: "__union__1",
Port: "data",
},
}},
},
},
},
nodesToInsert: map[string]src.Node{
"__union__1": {
EntityRef: core.EntityRef{
Pkg: "builtin",
Name: "UnionWrap",
},
},
},
constsToInsert: map[string]src.Const{
"__union_tag__1": {
Value: src.ConstValue{
Message: &src.MsgLiteral{
Str: compiler.Pointer("Int"),
},
},
},
},
},
},
}

for _, tt := range tests {
Expand Down
Loading

0 comments on commit f45e882

Please sign in to comment.