Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Pabón committed Oct 4, 2015
1 parent b264e2a commit 11c4508
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
8 changes: 6 additions & 2 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ package tests
import (
"fmt"
"runtime"
"testing"
)

type Tester interface {
Errorf(format string, args ...interface{})
FailNow()
}

// Simple assert call for unit and functional tests
func Assert(t *testing.T, b bool, message ...interface{}) {
func Assert(t Tester, b bool, message ...interface{}) {
if !b {
pc, file, line, _ := runtime.Caller(1)
caller_func_info := runtime.FuncForPC(pc)
Expand Down
65 changes: 65 additions & 0 deletions tests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright (c) 2015 The heketi 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 tests

import (
"strings"
"testing"
)

type FakeTester struct {
testing.T
failed bool
}

func (f *FakeTester) FailNow() {
f.failed = true
}

func (f *FakeTester) Failed() bool {
return f.failed
}

func TestAssertPass(t *testing.T) {
test := &FakeTester{}
Assert(test, true)

if test.Failed() {
t.Fail()
}
}

func TestAssertFail(t *testing.T) {

test := &FakeTester{}
Assert(test, false)

if !test.Failed() {
t.Fail()
}
}

func TestTempfile(t *testing.T) {
s1 := Tempfile()
Assert(t, strings.Contains(s1, "gounittest"))
Assert(t, strings.Contains(s1, "-1"))
Assert(t, !strings.Contains(s1, "-2"))

s2 := Tempfile()
Assert(t, strings.Contains(s2, "gounittest"))
Assert(t, !strings.Contains(s2, "-1"))
Assert(t, strings.Contains(s2, "-2"))
}

0 comments on commit 11c4508

Please sign in to comment.