Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoger committed Apr 10, 2021
1 parent 5769bb5 commit 1ff6cfd
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/caller_caller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 The ZKits Project Authors.
//
// 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 internal

import (
"testing"
)

func TestCallerReporter_GetCaller(t *testing.T) {
r := NewCallerReporter(1)
f1 := func() string { return r.GetCaller() }
f2 := func() string { return f1() }
f3 := func() string { return f2() }
f4 := func() string { return f3() }
f5 := func() string { return f4() }

got := f5() // Line 29
if want := "caller_caller_test.go:29"; got != want {
t.Fatalf("CallerReporter.GetCaller(): got %q, want %q", got, want)
}

got = r.GetCaller()
if want := "???:0"; got != want {
t.Fatalf("CallerReporter.GetCaller(): got %q, want %q", got, want)
}
}
38 changes: 38 additions & 0 deletions internal/caller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 The ZKits Project Authors.
//
// 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 internal

import (
"testing"
)

func TestNewCallerReporter(t *testing.T) {
if NewCallerReporter(0) == nil {
t.Fatal("NewCallerReporter(0): nil")
}
if NewCallerReporter(1) == nil {
t.Fatal("NewCallerReporter(1): nil")
}
}

func TestCallerReporter_Equal(t *testing.T) {
r := NewCallerReporter(1)
if r.Equal(0) {
t.Fatal("CallerReporter.Equal(0): true")
}
if !r.Equal(1) {
t.Fatal("CallerReporter.Equal(1): false")
}
}
65 changes: 65 additions & 0 deletions internal/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 The ZKits Project Authors.
//
// 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 internal

import (
"bytes"
"os"
"testing"
)

func TestEmptyExitFunc(t *testing.T) {
EmptyExitFunc(0) // nothing
}

func TestDefaultPanicFunc(t *testing.T) {
defer func() {
v := recover()
if v == nil {
t.Fatal("DefaultPanicFunc(): no panic")
}
s, ok := v.(string)
if !ok {
t.Fatalf("DefaultPanicFunc(): got %T - %v", v, v)
}
if s != "test" {
t.Fatalf("DefaultPanicFunc(): got %s", s)
}
}()
DefaultPanicFunc("test")
}

func TestEmptyPanicFunc(t *testing.T) {
defer func() {
v := recover()
if v != nil {
t.Fatalf("EmptyPanicFunc(): got %T - %v", v, v)
}
}()
EmptyPanicFunc("test")
}

func TestEchoError(t *testing.T) {
defer func() { ErrorWriter = os.Stderr }()

buf := new(bytes.Buffer)
ErrorWriter = buf

EchoError("test-%d", 1)

if got := buf.String(); got != "test-1\n" {
t.Fatalf("EchoError(): got %q", got)
}
}

0 comments on commit 1ff6cfd

Please sign in to comment.