From c30ba7504c579c89c6a24dc8269c7558516dbf8e Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 20 Dec 2014 17:53:37 +0800 Subject: [PATCH] unit tests :lantern: --- log/logs_test.go | 42 ++++++++++++++++++++++ util/file_test.go | 59 +++++++++++++++++++++++++++++++ util/go_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ util/net_test.go | 27 ++++++++++++++ util/os_test.go | 38 ++++++++++++++++++++ util/string_test.go | 35 ++++++++++++++++++ 6 files changed, 287 insertions(+) create mode 100644 log/logs_test.go create mode 100644 util/file_test.go create mode 100644 util/go_test.go create mode 100644 util/net_test.go create mode 100644 util/os_test.go create mode 100644 util/string_test.go diff --git a/log/logs_test.go b/log/logs_test.go new file mode 100644 index 0000000..e6d58fe --- /dev/null +++ b/log/logs_test.go @@ -0,0 +1,42 @@ +// Copyright (c) 2014, B3log +// +// 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 log + +import ( + "os" + "testing" +) + +var logger = NewLogger(os.Stdout) + +func TestSetLevel(t *testing.T) { + SetLevel("trace") +} + +func TestTrace(t *testing.T) { + logger.Trace("trace") +} + +func TestTracef(t *testing.T) { + logger.Tracef("tracef") +} + +func TestInfo(t *testing.T) { + logger.Info("info") +} + +func TestInfof(t *testing.T) { + logger.Infof("infof") +} diff --git a/util/file_test.go b/util/file_test.go new file mode 100644 index 0000000..c544af1 --- /dev/null +++ b/util/file_test.go @@ -0,0 +1,59 @@ +// Copyright (c) 2014, B3log +// +// 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 util + +import "testing" + +func TestGetFileSize(t *testing.T) { + size := File.GetFileSize(".") + + if 4096 != size { + t.Error("Size of a directory should be 4096") + + return + } +} + +func TestIsExist(t *testing.T) { + if !File.IsExist(".") { + t.Error(". must exist") + + return + } +} + +func TestIdBinary(t *testing.T) { + if File.IsBinary("not binary content") { + t.Error("The content should not be binary") + + return + } +} + +func TestIsImg(t *testing.T) { + if !File.IsImg(".jpg") { + t.Error(".jpg should be a valid extension of a image file") + + return + } +} + +func TestIsDir(t *testing.T) { + if !File.IsDir(".") { + t.Error(". should be a directory") + + return + } +} diff --git a/util/go_test.go b/util/go_test.go new file mode 100644 index 0000000..6eb4422 --- /dev/null +++ b/util/go_test.go @@ -0,0 +1,86 @@ +// Copyright (c) 2014, B3log +// +// 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 util + +import ( + "runtime" + "strconv" + "strings" + "testing" +) + +func TestGetAPIPath(t *testing.T) { + apiPath := Go.GetAPIPath() + + v := runtime.Version()[2:] + + verNum, err := strconv.ParseFloat(v, 64) + + if nil != err { + t.Error(err) + + return + } + + if verNum >= 1.4 { + if !strings.HasSuffix(apiPath, "src") { + t.Error("api path should end with \"src\"") + + return + } + } else { + if !strings.HasSuffix(apiPath, "pkg") { + t.Error("api path should end with \"pkg\"") + } + } +} + +func TestIsAPI(t *testing.T) { + apiPath := Go.GetAPIPath() + + if !Go.IsAPI(apiPath) { + t.Error("api path root should belong to api path") + + return + } + + root := "/root" + + if Go.IsAPI(root) { + t.Error("root should not belong to api path") + + return + } +} + +func TestGetGoFormats(t *testing.T) { + formats := Go.GetGoFormats() + + if len(formats) < 1 { + t.Error("should have one go format tool [gofmt] at least") + } +} + +func TestGetExecutableInGOBIN(t *testing.T) { + bin := Go.GetExecutableInGOBIN("test") + + if OS.IsWindows() { + if !strings.HasSuffix(bin, ".exe") { + t.Error("Executable binary should end with .exe") + + return + } + } +} diff --git a/util/net_test.go b/util/net_test.go new file mode 100644 index 0000000..d109f40 --- /dev/null +++ b/util/net_test.go @@ -0,0 +1,27 @@ +// Copyright (c) 2014, B3log +// +// 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 util + +import "testing" + +func TestLocalIP(t *testing.T) { + ip, err := Net.LocalIP() + + if nil != err { + t.Error(err) + } + + t.Log(ip) +} diff --git a/util/os_test.go b/util/os_test.go new file mode 100644 index 0000000..f177dc1 --- /dev/null +++ b/util/os_test.go @@ -0,0 +1,38 @@ +// Copyright (c) 2014, B3log +// +// 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 util + +import ( + "runtime" + "testing" +) + +func TestIsWiindows(t *testing.T) { + goos := runtime.GOOS + + if "windows" == goos && !OS.IsWindows() { + t.Error("runtime.GOOS returns [windows]") + + return + } +} + +func TestPwd(t *testing.T) { + if "" == OS.Pwd() { + t.Error("Working directory should not be empty") + + return + } +} diff --git a/util/string_test.go b/util/string_test.go new file mode 100644 index 0000000..86ec45f --- /dev/null +++ b/util/string_test.go @@ -0,0 +1,35 @@ +// Copyright (c) 2014, B3log +// +// 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 util + +import "testing" + +func TestContains(t *testing.T) { + if !Str.Contains("123", []string{"123", "345"}) { + t.Error("[\"123\", \"345\"] should contain \"123\"") + + return + } +} + +func TestLCS(t *testing.T) { + str := Str.LCS("123456", "abc34def") + + if "34" != str { + t.Error("[\"123456\"] and [\"abc34def\"] should have the longest common substring [\"34\"]") + + return + } +}