Skip to content

Commit e684704

Browse files
committed
Export Name testing interfaces.
1 parent 2c6d455 commit e684704

File tree

7 files changed

+67
-17
lines changed

7 files changed

+67
-17
lines changed

internal/name/interface.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 Nicholas Ng <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package name
16+
17+
// Setter means a name is mutable (can change Name).
18+
type Setter interface {
19+
SetName(string)
20+
}
21+
22+
// TypeHinter means a name has associated type-hint.
23+
type TypeHinter interface {
24+
TypeHint() string
25+
}

name/interface.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 Nicholas Ng <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package name
16+
17+
type Setter interface {
18+
SetName(string)
19+
}

name/interface_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package name
2+
3+
import (
4+
"testing"
5+
6+
"go.nickng.io/asyncpi/internal/name"
7+
)
8+
9+
// This test ensures the Setter is in sync with internal version.
10+
func TestSetterSync(t *testing.T) {
11+
var n Setter = name.New("base")
12+
if _, ok := n.(name.Setter); !ok {
13+
t.Fatalf("asyncpi/name.Setter and asyncpi/internal/name.Setter out of sync")
14+
}
15+
}

name/uniq.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020
"go.nickng.io/asyncpi"
2121
)
2222

23-
type setter interface {
24-
SetName(string)
25-
}
26-
2723
func MakeNamesUnique(p asyncpi.Process) error {
2824
if err := Walk(new(uniqueNamer), p); err != nil {
2925
return err
@@ -45,7 +41,7 @@ func (u *uniqueNamer) VisitName(n asyncpi.Name) error {
4541
}
4642
s := fmt.Sprintf("%s_%d", n.Ident(), len(u.names))
4743
u.names[n] = s
48-
if uniq, canSetName := n.(setter); canSetName {
44+
if uniq, canSetName := n.(Setter); canSetName {
4945
uniq.SetName(s)
5046
return nil
5147
}

names.go

-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ func newNames(names ...string) []Name {
1414
return pn
1515
}
1616

17-
type TypeHinter interface {
18-
TypeHint() string
19-
}
20-
21-
type nameSetter interface {
22-
SetName(string)
23-
}
24-
2517
// freeNameser is an interface which Name should
2618
// provide to have custom FreeNames implementation.
2719
type freeNameser interface {

reduce.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package asyncpi
22

33
import (
44
"fmt"
5+
6+
"go.nickng.io/asyncpi/internal/name"
57
)
68

79
// Subst is the substitution of variables xs by names vs in Process p.
@@ -22,13 +24,13 @@ func Subst(p Process, vs, xs []Name) error {
2224
case *Recv:
2325
for i, x := range xs {
2426
if IsSameName(p.Chan, x) {
25-
if ch, canSetName := p.Chan.(nameSetter); canSetName {
27+
if ch, canSetName := p.Chan.(name.Setter); canSetName {
2628
ch.SetName(vs[i].Ident())
2729
}
2830
}
2931
for _, rv := range p.Vars {
3032
if IsSameName(rv, x) {
31-
if ch, canSetName := rv.(nameSetter); canSetName {
33+
if ch, canSetName := rv.(name.Setter); canSetName {
3234
ch.SetName(vs[i].Ident())
3335
}
3436
}
@@ -42,7 +44,7 @@ func Subst(p Process, vs, xs []Name) error {
4244
case *Send:
4345
for i, x := range xs {
4446
if IsSameName(p.Chan, x) {
45-
if ch, canSetName := p.Chan.(nameSetter); canSetName {
47+
if ch, canSetName := p.Chan.(name.Setter); canSetName {
4648
ch.SetName(vs[i].Ident())
4749
}
4850
}

types/name.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package types
1616

1717
import (
1818
"go.nickng.io/asyncpi"
19+
"go.nickng.io/asyncpi/internal/name"
1920
)
2021

2122
// TypedName is a typed wrapper for Name.
@@ -65,7 +66,7 @@ func AttachType(n asyncpi.Name) TypedName {
6566
return tn
6667
}
6768
// Use type hint
68-
if th, hasHint := n.(asyncpi.TypeHinter); hasHint {
69+
if th, hasHint := n.(name.TypeHinter); hasHint {
6970
tn := newTypedName(n)
7071
tn.setType(NewBase(th.TypeHint()))
7172
return tn

0 commit comments

Comments
 (0)