Skip to content

Commit

Permalink
build(github): merge pull request #12 from Aoi-hosizora/dev.v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Aoi-hosizora authored Jan 22, 2021
2 parents d2b8bd2 + 3153efe commit 203437c
Show file tree
Hide file tree
Showing 100 changed files with 8,310 additions and 6,186 deletions.
25 changes: 10 additions & 15 deletions Hierarchy
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)
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ahlib

[![Build Status](https://www.travis-ci.org/Aoi-hosizora/ahlib.svg?branch=master)](https://www.travis-ci.org/Aoi-hosizora/ahlib)
[![Build Status](https://travis-ci.com/Aoi-hosizora/ahlib.svg?branch=master)](https://travis-ci.com/Aoi-hosizora/ahlib)
[![codecov](https://codecov.io/gh/Aoi-hosizora/ahlib/branch/master/graph/badge.svg)](https://codecov.io/gh/Aoi-hosizora/ahlib)
[![Go Report Card](https://goreportcard.com/badge/github.com/Aoi-hosizora/ahlib)](https://goreportcard.com/report/github.com/Aoi-hosizora/ahlib)
[![License](http://img.shields.io/badge/license-mit-blue.svg)](./LICENSE)
Expand All @@ -17,26 +17,20 @@

### Packages

+ xcolor
+ xcondition
+ xlinkedhashmap
+ **xslice**
+ **xstring**
+ xentity
+ xproperty
+ **xdi**
+ xreflect
+ **xnumber**
+ xmodule
+ xnumber
+ xorderedmap
+ xpointer
+ xcolor
+ xreflect
+ xruntime
+ xsystem
+ **xtime**
+ xzone
+ **xtesting**
+ xregexp
+ xslice
+ xstatus
+ xmap
+ xstring
+ xtesting
+ xtime

### Dependency
### Dependencies

+ Nothing
+ None
Empty file added internal/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions internal/_example/exit.go
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
}
23 changes: 23 additions & 0 deletions internal/_example/exit_test.go
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)
})
}
}
75 changes: 75 additions & 0 deletions internal/_example/logger.go
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}
}
71 changes: 71 additions & 0 deletions internal/_example/logger_test.go
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)
}
})
}
}
52 changes: 52 additions & 0 deletions internal/_example/option.go
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
}
}
32 changes: 32 additions & 0 deletions internal/_example/option_test.go
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")
}
Loading

0 comments on commit 203437c

Please sign in to comment.