Skip to content

Commit

Permalink
feat(test): update
Browse files Browse the repository at this point in the history
  • Loading branch information
Just-maple committed Oct 27, 2023
1 parent ea144c2 commit 5c911bb
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 10 deletions.
21 changes: 11 additions & 10 deletions internal/plugins/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,20 @@ func (dst *implDstType) apply(set *zcore.ModifySet, key implDstKey) (err error)
func (i Impl) Run(entities zcore.DeclEntities) (err error) {
group := i.group(entities)
set := new(zcore.ModifySet)
eg := new(zcore.ErrGroup)

for k := range group {
key := k
keys := make([]implDstKey, 0)
for key := range group {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].Package+keys[i].Typename < keys[j].Package+keys[j].Typename
})
for _, key := range keys {
if dst := group[key]; len(dst.Methods) > 0 {
eg.Go(func() error { return dst.apply(set, key) })
if err = dst.apply(set, key); err != nil {
return
}
}
}

if err = eg.Wait(); err != nil {
return
}

return set.Apply()
}

Expand Down
130 changes: 130 additions & 0 deletions internal/plugins/impl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2023 Maple Wu <justmaplewu@gmail.com>
* National Electronics and Computer Technology Center, Thailand
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package plugins

import (
"bytes"
"io/ioutil"
"os"
"testing"

zcore "github.com/go-zing/gozz-core"
)

const testImplData = `package x
import "context"
type (
// +zz:impl:./types.go:type=*Impls
T0 interface {
Int() int
}
// +zz:impl:./types.go:type=Impls
T2 interface {
Int2(context.Context) map[context.Context]int
}
// +zz:impl:./types.go
T3 interface {
Int2(context.Context) map[context.Context]int
}
// +zz:impl:./types.go:aop
T4 interface {
Int2(context.Context) map[context.Context]int
}
Impls string
)
`

const testImplRetData = `package x
import "context"
type (
// +zz:impl:./types.go:type=*Impls
T0 interface {
Int() int
}
// +zz:impl:./types.go:type=Impls
T2 interface {
Int2(context.Context) map[context.Context]int
}
// +zz:impl:./types.go
T3 interface {
Int2(context.Context) map[context.Context]int
}
// +zz:impl:./types.go:aop
T4 interface {
Int2(context.Context) map[context.Context]int
}
Impls string
)
func (impls *Impls) Int() int {
panic("not implemented")
}
func (impls Impls) Int2(context.Context) map[context.Context]int {
panic("not implemented")
}
var (
_ T3 = (*T3Impl)(nil)
)
type T3Impl struct{}
func (t3impl *T3Impl) Int2(context.Context) map[context.Context]int {
panic("not implemented")
}
var (
_ T4 = (*T4Impl)(nil)
)
type T4Impl struct{}
func (t4impl *T4Impl) Int2(context.Context) map[context.Context]int {
panic("not implemented")
}
`

func TestImpl(t *testing.T) {
_ = os.MkdirAll("test", 0o775)
defer os.RemoveAll("test")
if err := os.WriteFile("test/types.go", []byte(testImplData), 0o664); err != nil {
t.Fatal(err)
}
decls, err := zcore.ParseFileOrDirectory("test/types.go", zcore.AnnotationPrefix)
if err != nil {
return
}
plugin := &Impl{}
if err = plugin.Run(decls.Parse(plugin, nil)); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile("test/types.go")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, []byte(testImplRetData)) {
t.Fatal(err)
}
}

0 comments on commit 5c911bb

Please sign in to comment.