Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodo Junglas committed Feb 25, 2016
1 parent 749f2e5 commit 8aa0aca
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
22 changes: 22 additions & 0 deletions flag_test.go
@@ -0,0 +1,22 @@
package gopter_test

import (
"testing"

"github.com/leanovate/gopter"
)

func TestFlag(t *testing.T) {
flag := &gopter.Flag{}
if flag.Get() {
t.Errorf("Flag should be initially unset: %#v", flag)
}
flag.Set()
if !flag.Get() {
t.Errorf("Flag should be set: %#v", flag)
}
flag.Unset()
if flag.Get() {
t.Errorf("Flag should be unset: %#v", flag)
}
}
54 changes: 54 additions & 0 deletions gen_parameter_test.go
@@ -0,0 +1,54 @@
package gopter_test

import (
"math/rand"
"testing"

"github.com/leanovate/gopter"
)

type fixedSeed struct {
fixed int64
}

func (f *fixedSeed) Int63() int64 { return f.fixed }
func (f *fixedSeed) Seed(seed int64) { f.fixed = seed }

func TestGenParameters(t *testing.T) {
parameters := &gopter.GenParameters{
Size: 100,
Rng: rand.New(&fixedSeed{}),
}

if !parameters.NextBool() {
t.Error("Bool should be true")
}
if parameters.NextInt64() != 0 {
t.Error("int64 should be 0")
}
if parameters.NextUint64() != 0 {
t.Error("uint64 should be 0")
}

parameters.Rng.Seed(1)
if parameters.NextBool() {
t.Error("Bool should be false")
}
if parameters.NextInt64() != 1 {
t.Error("int64 should be 1")
}
if parameters.NextUint64() != 3 {
t.Error("uint64 should be 3")
}

parameters.Rng.Seed(2)
if !parameters.NextBool() {
t.Error("Bool should be true")
}
if parameters.NextInt64() != -2 {
t.Error("int64 should be 1")
}
if parameters.NextUint64() != 6 {
t.Error("uint64 should be 6")
}
}
36 changes: 36 additions & 0 deletions gen_test.go
@@ -0,0 +1,36 @@
package gopter_test

import (
"testing"

"github.com/leanovate/gopter"
)

func TestGenSample(t *testing.T) {
gen := gopter.Gen(func(*gopter.GenParameters) *gopter.GenResult {
return gopter.NewGenResult("sample", gopter.NoShrinker)
})

value, ok := gen.Sample()
if !ok || value != "sample" {
t.Errorf("Invalid gen sample: %#v", value)
}
}

func TestGenMap(t *testing.T) {
gen := gopter.Gen(func(*gopter.GenParameters) *gopter.GenResult {
return gopter.NewGenResult("sample", gopter.NoShrinker)
})
var mappedWith interface{}
mapper := func(v interface{}) interface{} {
mappedWith = v
return "other"
}
value, ok := gen.Map(mapper).Sample()
if !ok || value != "other" {
t.Errorf("Invalid gen sample: %#v", value)
}
if mappedWith.(string) != "sample" {
t.Errorf("Invalid mapped with: %#v", mappedWith)
}
}
65 changes: 65 additions & 0 deletions prop_result_test.go
@@ -0,0 +1,65 @@
package gopter_test

import (
"testing"

"github.com/leanovate/gopter"
)

func TestPropResult(t *testing.T) {
result := &gopter.PropResult{Status: gopter.PropProof}
if !result.Success() || result.Status.String() != "PROOF" {
t.Errorf("Invalid status: %#v", result)
}
other := &gopter.PropResult{Status: gopter.PropTrue}
if !result.And(other).Success() || result.And(other).Status.String() != "TRUE" {
t.Errorf("Invalid combined state: %#v", result.And(other))
}
if !other.And(result).Success() || other.And(result).Status.String() != "TRUE" {
t.Errorf("Invalid combined state: %#v", other.And(result))
}

result = &gopter.PropResult{Status: gopter.PropTrue}
if !result.Success() || result.Status.String() != "TRUE" {
t.Errorf("Invalid status: %#v", result)
}
if !result.And(other).Success() || result.And(other).Status.String() != "TRUE" {
t.Errorf("Invalid combined state: %#v", result.And(other))
}
if !other.And(result).Success() || other.And(result).Status.String() != "TRUE" {
t.Errorf("Invalid combined state: %#v", other.And(result))
}

result = &gopter.PropResult{Status: gopter.PropFalse}
if result.Success() || result.Status.String() != "FALSE" {
t.Errorf("Invalid status: %#v", result)
}
if result.And(other) != result {
t.Errorf("Invalid combined state: %#v", result.And(other))
}
if other.And(result) != result {
t.Errorf("Invalid combined state: %#v", other.And(result))
}

result = &gopter.PropResult{Status: gopter.PropUndecided}
if result.Success() || result.Status.String() != "UNDECIDED" {
t.Errorf("Invalid status: %#v", result)
}
if result.And(other) != result {
t.Errorf("Invalid combined state: %#v", result.And(other))
}
if other.And(result) != result {
t.Errorf("Invalid combined state: %#v", other.And(result))
}

result = &gopter.PropResult{Status: gopter.PropError}
if result.Success() || result.Status.String() != "ERROR" {
t.Errorf("Invalid status: %#v", result)
}
if result.And(other) != result {
t.Errorf("Invalid combined state: %#v", result.And(other))
}
if other.And(result) != result {
t.Errorf("Invalid combined state: %#v", other.And(result))
}
}

0 comments on commit 8aa0aca

Please sign in to comment.