Skip to content

Commit

Permalink
Add exmaple usages of generator and validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobilpadde committed Mar 7, 2023
1 parent a9f635a commit 117b383
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
52 changes: 52 additions & 0 deletions token/newGenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
28 changes: 28 additions & 0 deletions token/validate_test.go
Original file line number Diff line number Diff line change
@@ -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.
}

0 comments on commit 117b383

Please sign in to comment.