From 8aa0aca319df26c8beaf799c2ff6a0d181b53261 Mon Sep 17 00:00:00 2001 From: Bodo Junglas Date: Thu, 25 Feb 2016 13:11:55 +0100 Subject: [PATCH] Improve coverage --- flag_test.go | 22 +++++++++++++++ gen_parameter_test.go | 54 +++++++++++++++++++++++++++++++++++ gen_test.go | 36 ++++++++++++++++++++++++ prop_result_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 flag_test.go create mode 100644 gen_parameter_test.go create mode 100644 gen_test.go create mode 100644 prop_result_test.go diff --git a/flag_test.go b/flag_test.go new file mode 100644 index 0000000..37dec9e --- /dev/null +++ b/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) + } +} diff --git a/gen_parameter_test.go b/gen_parameter_test.go new file mode 100644 index 0000000..22adf46 --- /dev/null +++ b/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") + } +} diff --git a/gen_test.go b/gen_test.go new file mode 100644 index 0000000..2aff407 --- /dev/null +++ b/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) + } +} diff --git a/prop_result_test.go b/prop_result_test.go new file mode 100644 index 0000000..cd3005c --- /dev/null +++ b/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)) + } +}