-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(github): merge pull request #12 from Aoi-hosizora/dev.v1.5
- Loading branch information
Showing
100 changed files
with
8,310 additions
and
6,186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
$0 -- the most basic library | ||
{xtesting < xreflect} | ||
xcolor | ||
$0 -- test library | ||
xtesting | ||
|
||
$1 -- the common library, base on {xtesting, xreflect, xcolor} | ||
$1 -- basic libraries, base on libraries in $0 group | ||
xcolor (xtesting) | ||
xreflect (xtesting) | ||
|
||
$2 -- common libraries, base on libraries in $0 and $1 group | ||
xcondition (xtesting) | ||
xdi (xtesting, xreflect, xcolor) | ||
xlinkedhashmap (xtesting, xreflect) | ||
xmap (xtesting) | ||
xmodule (xtesting, xcolor) | ||
xnumber (xtesting) | ||
xorderedmap (xtesting, xreflect) | ||
xpointer (xtesting) | ||
xregexp (xtesting) | ||
xruntime (xcolor) | ||
xruntime () | ||
xslice (xtesting) | ||
xstatus (xtesting) | ||
xstring (xtesting) | ||
xsystem (xtesting) | ||
xtime (xtesting) | ||
xzone (xtesting) | ||
|
||
$2 -- the advanced library, base on $0 and $1, maybe removed in the future | ||
xentity (xslice, xcondition, xtesting) | ||
xproperty (xtesting) |
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
Empty file.
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,36 @@ | ||
package _example | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
var _exit = func(code int) { os.Exit(code) } | ||
|
||
func Exit(code int) { | ||
_exit(code) | ||
} | ||
|
||
type StubbedExit struct { | ||
Exited bool | ||
ExitCode int | ||
prev func(int) | ||
} | ||
|
||
func (s *StubbedExit) UnStub() { | ||
_exit = s.prev | ||
} | ||
|
||
func (s *StubbedExit) exit(code int) { | ||
s.Exited = true | ||
s.ExitCode = code | ||
} | ||
|
||
func WithStub(f func()) *StubbedExit { | ||
s := &StubbedExit{prev: _exit} | ||
defer s.UnStub() | ||
_exit = s.exit | ||
if f != nil { | ||
f() | ||
} | ||
return s | ||
} |
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,23 @@ | ||
package _example | ||
|
||
import ( | ||
"github.com/Aoi-hosizora/ahlib/xtesting" | ||
"testing" | ||
) | ||
|
||
func TestExit(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
give *StubbedExit | ||
wantExited bool | ||
wantExitCode int | ||
}{ | ||
{"no_exit", WithStub(func() {}), false, 0}, | ||
{"exited", WithStub(func() { Exit(1) }), true, 1}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
xtesting.Equal(t, tc.give.Exited, tc.wantExited) | ||
xtesting.Equal(t, tc.give.ExitCode, tc.wantExitCode) | ||
}) | ||
} | ||
} |
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,75 @@ | ||
package _example | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type Logger interface { | ||
Debug(m string) | ||
Info(m string) | ||
Warn(m string) | ||
Error(m string) | ||
Panic(m string) | ||
Fatal(m string) | ||
} | ||
|
||
type Level uint8 | ||
|
||
const ( | ||
Debug = iota + 1 | ||
Info | ||
Warn | ||
Error | ||
Panic | ||
Fatal | ||
Silence | ||
) | ||
|
||
type defaultLogger struct { | ||
writer io.Writer | ||
level Level | ||
} | ||
|
||
var _ Logger = (*defaultLogger)(nil) | ||
|
||
func (d *defaultLogger) Debug(m string) { | ||
if d.level <= Debug { | ||
_, _ = d.writer.Write([]byte("[DEBUG] " + m)) | ||
} | ||
} | ||
|
||
func (d *defaultLogger) Info(m string) { | ||
if d.level <= Info { | ||
_, _ = d.writer.Write([]byte("[INFO ] " + m)) | ||
} | ||
} | ||
|
||
func (d *defaultLogger) Warn(m string) { | ||
if d.level <= Warn { | ||
_, _ = d.writer.Write([]byte("[WARN ] " + m)) | ||
} | ||
} | ||
|
||
func (d *defaultLogger) Error(m string) { | ||
if d.level <= Error { | ||
_, _ = d.writer.Write([]byte("[ERROR] " + m)) | ||
} | ||
} | ||
|
||
func (d *defaultLogger) Panic(m string) { | ||
if d.level <= Panic { | ||
_, _ = d.writer.Write([]byte("[PANIC] " + m)) | ||
} | ||
panic(m) | ||
} | ||
|
||
func (d *defaultLogger) Fatal(m string) { | ||
if d.level <= Fatal { | ||
_, _ = d.writer.Write([]byte("[FATAL] " + m)) | ||
} | ||
Exit(1) // <<< | ||
} | ||
|
||
func DefaultLogger(writer io.Writer, level Level) *defaultLogger { | ||
return &defaultLogger{writer: writer, level: level} | ||
} |
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,71 @@ | ||
package _example | ||
|
||
import ( | ||
"github.com/Aoi-hosizora/ahlib/xtesting" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLogger(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
giveLogger Level | ||
giveLevel Level | ||
giveString string | ||
wantString string | ||
}{ | ||
{"debug for silence level", Debug, Silence, "debug", ""}, | ||
{"info for silence level", Info, Silence, "debug", ""}, | ||
{"warn for silence level", Warn, Silence, "debug", ""}, | ||
{"error for silence level", Error, Silence, "debug", ""}, | ||
{"panic for silence level", Panic, Silence, "debug", ""}, | ||
|
||
{"debug for debug level", Debug, Debug, "debug", "[DEBUG] debug"}, | ||
{"debug for info level", Debug, Info, "info", ""}, | ||
{"info for info level", Info, Info, "info", "[INFO ] info"}, | ||
{"info for warn level", Info, Warn, "warn", ""}, | ||
{"warn for warn level", Warn, Warn, "warn", "[WARN ] warn"}, | ||
{"warn for error level", Warn, Error, "error", ""}, | ||
{"error for error level", Error, Error, "error", "[ERROR] error"}, | ||
{"error for panic level", Error, Panic, "panic", ""}, | ||
{"panic for panic level", Panic, Panic, "panic", "[PANIC] panic"}, | ||
{"panic for fatal level", Panic, Fatal, "fatal", ""}, | ||
{"fatal for fatal level", Fatal, Fatal, "fatal", "[FATAL] fatal"}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
out := &strings.Builder{} | ||
logger := DefaultLogger(out, tc.giveLevel) | ||
var f func(string) | ||
switch tc.giveLogger { | ||
case Debug: | ||
f = logger.Debug | ||
case Info: | ||
f = logger.Info | ||
case Warn: | ||
f = logger.Warn | ||
case Error: | ||
f = logger.Error | ||
case Panic: | ||
f = logger.Panic | ||
case Fatal: | ||
f = logger.Fatal | ||
default: | ||
f = func(string) {} | ||
} | ||
|
||
if tc.giveLogger != Fatal { | ||
if tc.giveLogger == Panic { | ||
xtesting.Panic(t, func() { f(tc.giveString) }) | ||
} else { | ||
xtesting.NotPanic(t, func() { f(tc.giveString) }) | ||
} | ||
xtesting.Equal(t, out.String(), tc.wantString) | ||
} else { | ||
r := WithStub(func() { f(tc.giveString) }) | ||
xtesting.Equal(t, r.Exited, true) | ||
xtesting.Equal(t, r.ExitCode, 1) | ||
xtesting.Equal(t, out.String(), tc.wantString) | ||
} | ||
}) | ||
} | ||
} |
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,52 @@ | ||
package _example | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Service struct { | ||
options *serviceOptions | ||
} | ||
|
||
type serviceOptions struct { | ||
Arg1 int | ||
Arg2 string | ||
Arg3 bool | ||
} | ||
|
||
func (s *Service) DoSomething() string { | ||
return fmt.Sprintf("%d-%s-%v", s.options.Arg1, s.options.Arg2, s.options.Arg3) | ||
} | ||
|
||
type ServiceOption func(*serviceOptions) | ||
|
||
func New(options ...ServiceOption) *Service { | ||
so := &serviceOptions{ | ||
Arg1: 2, // default option value | ||
Arg3: true, | ||
} | ||
for _, o := range options { | ||
if o != nil { | ||
o(so) // apply option | ||
} | ||
} | ||
return &Service{options: so} | ||
} | ||
|
||
func WithArg1(i int) ServiceOption { | ||
return func(o *serviceOptions) { | ||
o.Arg1 = i | ||
} | ||
} | ||
|
||
func WithArg2(s string) ServiceOption { | ||
return func(o *serviceOptions) { | ||
o.Arg2 = s | ||
} | ||
} | ||
|
||
func WithArg3(b bool) ServiceOption { | ||
return func(o *serviceOptions) { | ||
o.Arg3 = b | ||
} | ||
} |
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,32 @@ | ||
package _example | ||
|
||
import ( | ||
"github.com/Aoi-hosizora/ahlib/xtesting" | ||
"testing" | ||
) | ||
|
||
func TestOption(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
give *serviceOptions | ||
want *serviceOptions | ||
}{ | ||
// normal | ||
{"default", New().options, &serviceOptions{2, "", true}}, | ||
{"WithArg1", New(WithArg1(0)).options, &serviceOptions{0, "", true}}, | ||
{"WithArg2", New(WithArg2("test")).options, &serviceOptions{2, "test", true}}, | ||
{"WithArg3", New(WithArg3(false)).options, &serviceOptions{2, "", false}}, | ||
{"WithArgs", New(WithArg1(0), WithArg2("test"), WithArg3(false)).options, &serviceOptions{0, "test", false}}, | ||
|
||
// abnormal | ||
{"nil_option", New(nil).options, &serviceOptions{2, "", true}}, | ||
{"multi_options", New(WithArg1(0), WithArg1(2), WithArg1(1)).options, &serviceOptions{1, "", true}}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
xtesting.EqualValue(t, tc.give, tc.want) | ||
}) | ||
} | ||
|
||
service := New(WithArg1(0), WithArg2("test"), WithArg3(false)) | ||
xtesting.Equal(t, service.DoSomething(), "0-test-false") | ||
} |
Oops, something went wrong.