Skip to content

Commit

Permalink
add example/genericfunc/
Browse files Browse the repository at this point in the history
  • Loading branch information
dengsgo committed Oct 7, 2023
1 parent c0552e1 commit 221ce26
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example/genericfunc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.vscode
genericfunc*
22 changes: 22 additions & 0 deletions example/genericfunc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/dengsgo/go-decorator/decor"
"log"
)

func logging(ctx *decor.Context) {
log.Println("logging print target in", ctx.TargetIn)
ctx.TargetDo()
log.Println("logging print target out", ctx.TargetOut)
}

//go:decor logging
func plus[T int8 | int16 | int | int32 | int64 | float32 | float64](a, b T) T {
return a + b
}

func main() {
log.SetFlags(0)
log.Println("plus(1, 10) = ", plus(1, 10))
}
35 changes: 35 additions & 0 deletions example/genericfunc/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"bytes"
"log"
"os"
"os/exec"
"strings"
"testing"
)

const out = `logging print target in [1 10]
logging print target out [11]
plus(1, 10) = 11
`

func TestPlus(t *testing.T) {
args := []string{
"go", "run", "-toolexec", "decorator", "./main.go",
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
log.Println("args", args)
bf := bytes.NewBuffer([]byte{})
cmd.Stdout = bf
cmd.Stderr = bf
err := cmd.Run()
if err != nil {
t.Fatalf("Run plus fail %s", err)
}
if strings.ReplaceAll(bf.String(), "\r\n", "\n") !=
strings.ReplaceAll(out, "\r\n", "\n") {
t.Fatalf("plus out fail, out %s", bf.String())
}
}

0 comments on commit 221ce26

Please sign in to comment.