forked from flyingmutant/rapid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
20 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
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 @@ | ||
// Copyright 2022 Gregory Petrosyan <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// https://stackoverflow.com/questions/73864711/get-type-parameter-from-a-generic-struct-using-reflection | ||
|
||
// MakeVariant creates a generator of values of type V, using reflection to infer the required structure. | ||
func MakeVariant[V any](overrides ...*Generator[any]) *Generator[V] { | ||
var zero V | ||
gen := newMakeGen(reflect.TypeOf(zero), overrides) | ||
return newGenerator[V](&makeGen[V]{ | ||
gen: gen, | ||
}) | ||
} |
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,50 @@ | ||
// Copyright 2022 Gregory Petrosyan <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"pgregory.net/rapid" | ||
) | ||
|
||
type S struct { | ||
F1 string | ||
Int int | ||
T time.Time | ||
TPtr *time.Time | ||
} | ||
|
||
func (s S) String() string { | ||
return fmt.Sprintf("%s, %d, %s", s.F1, s.Int, s.T.String()) | ||
} | ||
|
||
func Test(t *testing.T) { | ||
now := time.Now() | ||
|
||
strGen := rapid.Just("Hello") | ||
intGen := rapid.IntRange(0, 100) | ||
timeGen := rapid.Just(now) | ||
sGen := rapid.MakeVariant[S](strGen.AsAny(), intGen.AsAny(), timeGen.AsAny()) | ||
s := sGen.Example(1) | ||
|
||
if s.F1 != "Hello" { | ||
t.Errorf("Unexpected string value") | ||
} | ||
if s.Int > 100 || s.Int < 0 { | ||
t.Errorf("Unexpected int value") | ||
} | ||
if !s.T.Equal(now) { | ||
t.Errorf("Unexpected time.Time value") | ||
} | ||
if s.TPtr != nil && !s.TPtr.Equal(now) { | ||
t.Errorf("Unexpected time.Time ptr value") | ||
} | ||
fmt.Println(s.String()) | ||
} |