-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc9d275
commit 786346c
Showing
4 changed files
with
255 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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" | ||
"encoding/base64" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
zcore "github.com/go-zing/gozz-core" | ||
) | ||
|
||
const testTagData = `package tag05 | ||
import ( | ||
"time" | ||
) | ||
//go:generate gozz run -p "tag" ./ | ||
// +zz:tag:json,bson:{{ snake .FieldName }} | ||
type ( | ||
UserStruct struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
Friends []struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
} | ||
UserMap map[string]struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
UserSlice []struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
UserInterface interface { | ||
User() struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
} | ||
UserFunc func(struct { | ||
Id string | ||
Name string | ||
Address string | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
}) | ||
) | ||
` | ||
|
||
var testTagRetData, _ = base64.StdEncoding.DecodeString(`cGFja2FnZSB0YWcwNQoKaW1wb3J0ICgKCSJ0aW1lIgopCgovL2dvOmdlbmVyYXRlIGdvenogcnVuIC1wICJ0YWciIC4vCgovLyAreno6dGFnOmpzb24sYnNvbjp7eyBzbmFrZSAuRmllbGROYW1lIH19CnR5cGUgKAoJVXNlclN0cnVjdCBzdHJ1Y3QgewoJCUlkICAgICAgICBzdHJpbmcgICAgYGJzb246ImlkIiBqc29uOiJpZCJgCgkJTmFtZSAgICAgIHN0cmluZyAgICBgYnNvbjoibmFtZSIganNvbjoibmFtZSJgCgkJQWRkcmVzcyAgIHN0cmluZyAgICBgYnNvbjoiYWRkcmVzcyIganNvbjoiYWRkcmVzcyJgCgkJQ3JlYXRlZEF0IHRpbWUuVGltZSBgYnNvbjoiY3JlYXRlZF9hdCIganNvbjoiY3JlYXRlZF9hdCJgCgkJVXBkYXRlZEF0IHRpbWUuVGltZSBgYnNvbjoidXBkYXRlZF9hdCIganNvbjoidXBkYXRlZF9hdCJgCgkJRnJpZW5kcyAgIFtdc3RydWN0IHsKCQkJSWQgICAgICAgIHN0cmluZyAgICBgYnNvbjoiaWQiIGpzb246ImlkImAKCQkJTmFtZSAgICAgIHN0cmluZyAgICBgYnNvbjoibmFtZSIganNvbjoibmFtZSJgCgkJCUFkZHJlc3MgICBzdHJpbmcgICAgYGJ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1Y3QgewoJCQlJZCAgICAgICAgc3RyaW5nICAgIGBic29uOiJpZCIganNvbjoiaWQiYAoJCQlOYW1lICAgICAgc3RyaW5nICAgIGBic29uOiJuYW1lIiBqc29uOiJuYW1lImAKCQkJQWRkcmVzcyAgIHN0cmluZyAgICBgYnNvbjoiYWRkcmVzcyIganNvbjoiYWRkcmVzcyJgCgkJCUNyZWF0ZWRBdCB0aW1lLlRpbWUgYGJzb246ImNyZWF0ZWRfYXQiIGpzb246ImNyZWF0ZWRfYXQiYAoJCQlVcGRhdGVkQXQgdGltZS5UaW1lIGBic29uOiJ1cGRhdGVkX2F0IiBqc29uOiJ1cGRhdGVkX2F0ImAKCQl9Cgl9CgoJVXNlckZ1bmMgZnVuYyhzdHJ1Y3QgewoJCUlkICAgICAgICBzdHJpbmcgICAgYGJzb246ImlkIiBqc29uOiJpZCJgCgkJTmFtZSAgICAgIHN0cmluZyAgICBgYnNvbjoibmFtZSIganNvbjoibmFtZSJgCgkJQWRkcmVzcyAgIHN0cmluZyAgICBgYnNvbjoiYWRkcmVzcyIganNvbjoiYWRkcmVzcyJgCgkJQ3JlYXRlZEF0IHRpbWUuVGltZSBgYnNvbjoiY3JlYXRlZF9hdCIganNvbjoiY3JlYXRlZF9hdCJgCgkJVXBkYXRlZEF0IHRpbWUuVGltZSBgYnNvbjoidXBkYXRlZF9hdCIganNvbjoidXBkYXRlZF9hdCJgCgl9KQopCg==`) | ||
|
||
func TestTag(t *testing.T) { | ||
_ = os.MkdirAll("test", 0o775) | ||
defer os.RemoveAll("test") | ||
|
||
if err := os.WriteFile("test/types.go", []byte(testTagData), 0o664); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
decls, err := zcore.ParseFileOrDirectory("test/types.go", zcore.AnnotationPrefix) | ||
if err != nil { | ||
return | ||
} | ||
|
||
plugin := &Tag{} | ||
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, testTagRetData) { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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 ( | ||
"os" | ||
"testing" | ||
|
||
zcore "github.com/go-zing/gozz-core" | ||
) | ||
|
||
const testWireData = ` | ||
package x | ||
import ( | ||
"bytes" | ||
"database/sql" | ||
"io" | ||
"github.com/google/wire" | ||
) | ||
// provide value and interface value | ||
// +zz:wire:bind=io.Writer:aop | ||
// +zz:wire | ||
var Buffer = &bytes.Buffer{} | ||
// provide referenced type | ||
// +zz:wire | ||
type NullString nullString | ||
type nullString sql.NullString | ||
// use provider function to provide referenced type alias | ||
// +zz:wire | ||
type String = string | ||
func ProvideString() String { | ||
return "" | ||
} | ||
// provide value from implicit type | ||
// +zz:wire | ||
var Bool = false | ||
// +zz:wire:inject=./ | ||
type Target struct { | ||
Buffer *bytes.Buffer | ||
Writer io.Writer | ||
NullString NullString | ||
Int int | ||
Float Float | ||
Bytes *Bytes | ||
} | ||
type Float interface{} | ||
// +zz:wire:bind=Float | ||
func InitFloat() float64 { | ||
return 0 | ||
} | ||
type Bytes []byte | ||
// +zz:wire | ||
func InitBytes() *Bytes { | ||
return nil | ||
} | ||
// origin wire set | ||
// +zz:wire | ||
var Set = wire.NewSet(wire.Value(Int)) | ||
var Int = 0 | ||
// mock set injector | ||
// +zz:wire:inject=./inner:set=mock | ||
type MockString sql.NullString | ||
// mock set string | ||
// provide type from function | ||
// +zz:wire:set=mock | ||
func InitMockString() String { | ||
return "mock" | ||
} | ||
// mock set struct type provide fields | ||
// +zz:wire:set=mock:field=* | ||
type MockConfig struct{ Bool bool } | ||
// mock set value | ||
// +zz:wire:set=mock | ||
var MockConfigValue = &MockConfig{Bool: true} | ||
` | ||
|
||
func TestWire(t *testing.T) { | ||
_ = os.MkdirAll("test", 0o775) | ||
defer os.RemoveAll("test") | ||
if err := os.WriteFile("test/types.go", []byte(testWireData), 0o664); err != nil { | ||
t.Fatal(err) | ||
} | ||
decls, err := zcore.ParseFileOrDirectory("test/types.go", zcore.AnnotationPrefix) | ||
if err != nil { | ||
return | ||
} | ||
plugin := Wire{} | ||
if err = plugin.Run(decls.Parse(plugin, nil)); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |