-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also add placeholders for untested files -> no more lies about test coverage.
- Loading branch information
Showing
14 changed files
with
144 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ cmd/elvispc/elvispc | |
|
||
# Test | ||
/coverage.txt | ||
/coverage.tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cjdns_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cjdns_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cjdns_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,49 @@ | ||
package database | ||
package database_test | ||
|
||
import ( | ||
"log" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/willeponken/elvisp/database" | ||
) | ||
|
||
const dbPath = "/tmp/testing-show.db" | ||
func tempFile() string { | ||
file, err := ioutil.TempFile("", "elvisp-") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var testDB Database | ||
if err := file.Close(); err != nil { | ||
panic(err) | ||
} | ||
|
||
func removeDatabase() error { | ||
return os.Remove(dbPath) | ||
} | ||
if err := os.Remove(file.Name()); err != nil { | ||
panic(err) | ||
} | ||
|
||
func setupDatabase() { | ||
var err error | ||
return file.Name() | ||
} | ||
|
||
removeDatabase() // Make sure there is no already existing database | ||
type TestDB struct { | ||
database.Database | ||
} | ||
|
||
testDB, err = Open(dbPath) | ||
func MustOpen() TestDB { | ||
db, err := database.Open(tempFile()) | ||
if err != nil { | ||
log.Fatalf("Open database should be successfull, returned error: %v", err) | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
runResult := m.Run() | ||
return TestDB{db} | ||
} | ||
|
||
removeDatabase() // Clean up the database | ||
func (t *TestDB) Close() error { | ||
defer os.Remove(t.Path()) | ||
return t.Database.Close() | ||
} | ||
|
||
os.Exit(runResult) | ||
func (t *TestDB) MustClose() { | ||
if err := t.Close(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package database | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
// uint64ToBin returns an 8-byte big endian representation of v. | ||
func uint64ToBin(v uint64) []byte { | ||
b := make([]byte, 8) | ||
binary.BigEndian.PutUint64(b, v) | ||
return b | ||
} | ||
|
||
// binToUint64 takes an 8-byte big endian and converts it into a uint64. | ||
func binToUint64(v []byte) uint64 { | ||
if len(v) != 8 { | ||
panic(fmt.Sprintf("invalid length of binary: %d", len(v))) | ||
} | ||
|
||
return binary.BigEndian.Uint64(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package database | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var binUint64Tests = []struct { | ||
v uint64 | ||
b []byte | ||
}{ | ||
{1, []byte{0, 0, 0, 0, 0, 0, 0, 1}}, | ||
{18446744073709551616 - 1, []byte{255, 255, 255, 255, 255, 255, 255, 255}}, | ||
} | ||
|
||
func TestUint64ToBin(t *testing.T) { | ||
for row, test := range binUint64Tests { | ||
b := uint64ToBin(test.v) | ||
|
||
if !reflect.DeepEqual(b, test.b) { | ||
t.Errorf("Row: %d returned unexpected binary, got: %v, wanted: %v", row, b, test.b) | ||
} | ||
} | ||
} | ||
|
||
func TestBinToUint64(t *testing.T) { | ||
for row, test := range binUint64Tests { | ||
v := binToUint64(test.b) | ||
|
||
if v != test.v { | ||
t.Errorf("Row: %d returned unexpected number, got: %d, wanted: %d", row, v, test.v) | ||
} | ||
} | ||
} | ||
|
||
func TestBinToUint64_invalidLength(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("Invalid length not equal to 8 should panic") | ||
} | ||
}() | ||
|
||
binToUint64(make([]byte, 1)) // should panic, must be equal to 8 | ||
binToUint64(make([]byte, 9)) // same as above | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.