✅ Examples for everything
This commit is contained in:
parent
bd448eb5db
commit
9402d80704
4 changed files with 124 additions and 51 deletions
|
|
@ -7,7 +7,14 @@ import (
|
||||||
"codeberg.org/danjones000/nomino"
|
"codeberg.org/danjones000/nomino"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleTimestamp_withTime() {
|
func ExampleTimestamp() {
|
||||||
|
gen := nomino.Timestamp()
|
||||||
|
conf := nomino.NewConfig(nomino.WithGenerator(gen))
|
||||||
|
s, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleTimestampTime() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts))
|
gen := nomino.Timestamp(nomino.TimestampTime(ts))
|
||||||
|
|
@ -17,7 +24,7 @@ func ExampleTimestamp_withTime() {
|
||||||
// Output: 2009-01-20T12-05-00-0500.txt
|
// Output: 2009-01-20T12-05-00-0500.txt
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleTimestamp_withFormat() {
|
func ExampleTimestampFormat() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampFormat("2006#01#02<>15|04|05-0700"))
|
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampFormat("2006#01#02<>15|04|05-0700"))
|
||||||
|
|
@ -27,7 +34,7 @@ func ExampleTimestamp_withFormat() {
|
||||||
// Output: 2009#01#20<>12|05|00-0500.txt
|
// Output: 2009#01#20<>12|05|00-0500.txt
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleTimestamp_withUTC() {
|
func ExampleTimestampUTC() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampUTC())
|
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampUTC())
|
||||||
|
|
|
||||||
|
|
@ -25,3 +25,41 @@ func ExampleWithGenerator_customGenerator() {
|
||||||
// hello.txt
|
// hello.txt
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleMultiGeneratorInOrder() {
|
||||||
|
gen1 := func(*nomino.Config) (string, error) {
|
||||||
|
return "hello", nil
|
||||||
|
}
|
||||||
|
gen2 := func(*nomino.Config) (string, error) {
|
||||||
|
return "goodbye", nil
|
||||||
|
}
|
||||||
|
gen := nomino.MultiGeneratorInOrder(gen1, gen2)
|
||||||
|
option := nomino.WithGenerator(gen)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello.txt
|
||||||
|
// goodbye.txt
|
||||||
|
// hello.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUUID() {
|
||||||
|
option := nomino.WithGenerator(nomino.UUID())
|
||||||
|
|
||||||
|
str, _ := nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(nomino.NewConfig(option))
|
||||||
|
fmt.Println(str)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,82 @@ import (
|
||||||
"codeberg.org/danjones000/nomino"
|
"codeberg.org/danjones000/nomino"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func ExampleWithExtension() {
|
||||||
|
st, _ := nomino.Make(nomino.NewConfig(
|
||||||
|
nomino.WithExtension("xml"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
))
|
||||||
|
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output: 0.xml
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithoutExtension() {
|
||||||
|
st, _ := nomino.Make(nomino.NewConfig(
|
||||||
|
nomino.WithoutExtension(),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
))
|
||||||
|
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithPrefix() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("pref"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
)
|
||||||
|
st, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
|
||||||
|
st, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output:
|
||||||
|
// pref_0.txt
|
||||||
|
// pref_1.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithSeparator() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("pref"),
|
||||||
|
nomino.WithSeparator("---"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
)
|
||||||
|
st, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
|
||||||
|
st, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output:
|
||||||
|
// pref---0.txt
|
||||||
|
// pref---1.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithSuffix() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithSuffix("suff"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
)
|
||||||
|
st, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
|
||||||
|
st, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output:
|
||||||
|
// 0_suff.txt
|
||||||
|
// 1_suff.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithOriginal() {
|
||||||
|
st, _ := nomino.Make(nomino.NewConfig(
|
||||||
|
nomino.WithOriginal("Hello, World"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
))
|
||||||
|
|
||||||
|
fmt.Println(st)
|
||||||
|
// Output: 0_Hello, World.txt
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleWithOriginalSlug() {
|
func ExampleWithOriginalSlug() {
|
||||||
st, _ := nomino.Make(nomino.NewConfig(
|
st, _ := nomino.Make(nomino.NewConfig(
|
||||||
nomino.WithOriginalSlug("Hello, World"),
|
nomino.WithOriginalSlug("Hello, World"),
|
||||||
|
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
package nomino
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWithOriginal(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
name := "foobar"
|
|
||||||
WithOriginal(name)(&c)
|
|
||||||
assert.Equal(t, name, c.original)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithPrefix(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
pref := "draft"
|
|
||||||
WithPrefix(pref)(&c)
|
|
||||||
assert.Equal(t, pref, c.prefix)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithSuffix(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
suff := "out"
|
|
||||||
WithSuffix(suff)(&c)
|
|
||||||
assert.Equal(t, suff, c.suffix)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithoutExtension(t *testing.T) {
|
|
||||||
c := Config{extension: ".foobar"}
|
|
||||||
WithoutExtension()(&c)
|
|
||||||
assert.Equal(t, "", c.extension)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithExtension(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
ext := "yaml"
|
|
||||||
WithExtension(ext)(&c)
|
|
||||||
assert.Equal(t, "."+ext, c.extension)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithSeparator(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
sep := "---"
|
|
||||||
WithSeparator(sep)(&c)
|
|
||||||
assert.Equal(t, sep, c.separator)
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue