From b3fd320be7f6808284270965fe739f2b12aa358c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 19 Feb 2018 23:01:00 +0100 Subject: [PATCH] more test cases --- common/log/logger_test.go | 40 ++++++++++++++++++++++++++++++++++++ common/serial/string_test.go | 28 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 common/log/logger_test.go create mode 100644 common/serial/string_test.go diff --git a/common/log/logger_test.go b/common/log/logger_test.go new file mode 100644 index 000000000..03018e2c0 --- /dev/null +++ b/common/log/logger_test.go @@ -0,0 +1,40 @@ +package log_test + +import ( + "io/ioutil" + "os" + "testing" + "time" + + "v2ray.com/core/common" + "v2ray.com/core/common/buf" + . "v2ray.com/core/common/log" + . "v2ray.com/ext/assert" +) + +func TestFileLogger(t *testing.T) { + assert := With(t) + + f, err := ioutil.TempFile("", "vtest") + assert(err, IsNil) + path := f.Name() + common.Must(f.Close()) + + creator, err := CreateFileLogWriter(path) + assert(err, IsNil) + + handler := NewLogger(creator) + handler.Handle(&GeneralMessage{Content: "Test Log"}) + time.Sleep(2 * time.Second) + + common.Must(common.Close(handler)) + + f, err = os.Open(path) + assert(err, IsNil) + + b, err := buf.ReadAllToBytes(f) + assert(err, IsNil) + assert(string(b), HasSubstring, "Test Log") + + common.Must(f.Close()) +} diff --git a/common/serial/string_test.go b/common/serial/string_test.go new file mode 100644 index 000000000..47d044683 --- /dev/null +++ b/common/serial/string_test.go @@ -0,0 +1,28 @@ +package serial_test + +import ( + "errors" + "testing" + + . "v2ray.com/core/common/serial" + . "v2ray.com/ext/assert" +) + +func TestToString(t *testing.T) { + assert := With(t) + + s := "a" + data := []struct { + Value interface{} + String string + }{ + {Value: s, String: s}, + {Value: &s, String: s}, + {Value: errors.New("t"), String: "t"}, + {Value: []byte{'b', 'c'}, String: "[62,63]"}, + } + + for _, c := range data { + assert(ToString(c.Value), Equals, c.String) + } +}