diff --git a/token/newGenerator_test.go b/token/newGenerator_test.go index d46bc7b..aed2f4d 100644 --- a/token/newGenerator_test.go +++ b/token/newGenerator_test.go @@ -83,3 +83,55 @@ func TestNewGeneratorNoEmojies(t *testing.T) { t.Error("Expected to return an error when creating new generator without any emojies:", err) } } + +// Instantiate a new generator with +// a secret, period, amount, and emojies. +// +// Use this generator to generate new codes +// from the options provided. +func ExampleNewGenerator() { + amount := 6 + secret := strings.Repeat("a", 32) + + gen, _ := NewGenerator( + OptionWithSecret(secret), + OptionWithPeriod(time.Second), + OptionWithAmount(amount), + OptionWithEmojies(emojies.CATS), + ) + + gen.Next() +} + +// Instantiates a generator as above, +// but also with a specified time. +// +// This will **always** generate the same +// codes *virtually forever*. +// +// If we chose to generate **five** codes, +// these would be as follows: +// +// 🙀 😾 😹 🙀 😼 😹 +// +// 😻 😹 😽 😹 😽 😿 +// +// 😹 😽 😻 😸 😻 🙀 +// +// 😼 😼 😾 😾 😿 😹 +// +// 😿 😽 😿 🙀 😼 😻 +func ExampleNewGenerator_withTime() { + amount := 6 + secret := strings.Repeat("a", 32) + + gen, _ := NewGenerator( + OptionWithSecret(secret), + OptionWithPeriod(time.Second), + OptionWithAmount(amount), + OptionWithEmojies(emojies.CATS), + OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)), + ) + + gen.Next() +} diff --git a/token/validate_test.go b/token/validate_test.go new file mode 100644 index 0000000..cde94b8 --- /dev/null +++ b/token/validate_test.go @@ -0,0 +1,28 @@ +package token + +import ( + "log" + "strings" + "time" + + "github.com/Mobilpadde/moths/v5/token/emojies" +) + +func ExampleGenerator_Validate() { + amount := 6 + secret := strings.Repeat("a", 32) + + var err error + var gen *Generator + if gen, err = NewGenerator( + OptionWithSecret(secret), + OptionWithPeriod(time.Second), + OptionWithAmount(amount), + OptionWithEmojies(emojies.CATS), + ); err != nil { + log.Fatalln(err) + } + + code, _ := gen.Next() + gen.Validate(code.String()) // This is true, as it's validated within specified period. +}