unit tests 🏮

This commit is contained in:
Liang Ding 2014-12-20 17:53:37 +08:00
parent a42152e43f
commit c30ba7504c
6 changed files with 287 additions and 0 deletions

42
log/logs_test.go Normal file
View File

@ -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")
}

59
util/file_test.go Normal file
View File

@ -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
}
}

86
util/go_test.go Normal file
View File

@ -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
}
}
}

27
util/net_test.go Normal file
View File

@ -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)
}

38
util/os_test.go Normal file
View File

@ -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
}
}

35
util/string_test.go Normal file
View File

@ -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
}
}