Skip to content

Commit 2abe0ba

Browse files
committed
test: add fname package tests
1 parent e6eecee commit 2abe0ba

5 files changed

Lines changed: 301 additions & 0 deletions

File tree

Taskfile.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ tasks:
1515
desc: Runs golangci-lint
1616
cmds:
1717
- golangci-lint run
18+
test:
19+
desc: Runs tests
20+
cmds:
21+
- go test ./...
1822
release:test:
1923
desc: Tests release process without publishing
2024
cmds:

data/noun

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
aardvark
12
abbey
23
ability
34
absence

data/verb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
abandoned
2+
abandons
13
abides
24
abuses
35
accepted

dictionary_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package fname
2+
3+
import "testing"
4+
5+
func TestNewDictionary(t *testing.T) {
6+
t.Log("Given the need to test the NewDictionary function")
7+
{
8+
t.Log("\tWhen creating a new Dictionary with default values")
9+
{
10+
d := NewDictionary()
11+
if d == nil {
12+
t.Fatal("\t\tShould be able to create a Dictionary instance.")
13+
}
14+
t.Log("\t\tShould be able to create a Dictionary instance.")
15+
16+
if len(d.adectives) == 0 {
17+
t.Error("\t\tShould be able to load the adjective file.")
18+
}
19+
t.Log("\t\tShould be able to load the adjective file.")
20+
21+
if len(d.adverbs) == 0 {
22+
t.Error("\t\tShould be able to load the adverb file.")
23+
}
24+
t.Log("\t\tShould be able to load the adverb file.")
25+
26+
if len(d.nouns) == 0 {
27+
t.Error("\t\tShould be able to load the noun file.")
28+
}
29+
t.Log("\t\tShould be able to load the noun file.")
30+
31+
if len(d.verbs) == 0 {
32+
t.Error("\t\tShould be able to load the verb file.")
33+
}
34+
35+
adj, err := d.Adjective(0)
36+
if err != nil {
37+
t.Error("\t\tShould be able to get an adjective from the dictionary.")
38+
}
39+
t.Log("\t\tShould be able to get an adjective from the dictionary.")
40+
if adj != "able" {
41+
t.Error("\t\tShould be able to get the first adjective from the dictionary.")
42+
}
43+
t.Log("\t\tShould be able to get the first adjective from the dictionary.")
44+
45+
_, err = d.Adjective(-1)
46+
if err == nil {
47+
t.Error("\t\tShould not be able to get an adjective from the dictionary with a negative index.")
48+
}
49+
t.Log("\t\tShould not be able to get an adjective from the dictionary with a negative index.")
50+
51+
adv, err := d.Adverb(0)
52+
if err != nil {
53+
t.Error("\t\tShould be able to get an adverb from the dictionary.")
54+
}
55+
t.Log("\t\tShould be able to get an adverb from the dictionary.")
56+
if adv != "abnormally" {
57+
t.Error("\t\tShould be able to get the first adverb from the dictionary.")
58+
}
59+
t.Log("\t\tShould be able to get the first adverb from the dictionary.")
60+
61+
_, err = d.Adverb(-1)
62+
if err == nil {
63+
t.Error("\t\tShould not be able to get an adverb from the dictionary with a negative index.")
64+
}
65+
t.Log("\t\tShould not be able to get an adverb from the dictionary with a negative index.")
66+
67+
noun, err := d.Noun(0)
68+
if err != nil {
69+
t.Error("\t\tShould be able to get a noun from the dictionary.")
70+
}
71+
t.Log("\t\tShould be able to get a noun from the dictionary.")
72+
73+
if noun != "aardvark" {
74+
t.Error("\t\tShould be able to get the first noun from the dictionary.")
75+
}
76+
t.Log("\t\tShould be able to get the first noun from the dictionary.")
77+
78+
_, err = d.Noun(-1)
79+
if err == nil {
80+
t.Error("\t\tShould not be able to get a noun from the dictionary with a negative index.")
81+
}
82+
t.Log("\t\tShould not be able to get a noun from the dictionary with a negative index.")
83+
84+
verb, err := d.Verb(0)
85+
if err != nil {
86+
t.Error("\t\tShould be able to get a verb from the dictionary.")
87+
}
88+
t.Log("\t\tShould be able to get a verb from the dictionary.")
89+
90+
if verb != "abandoned" {
91+
t.Error("\t\tShould be able to get the first verb from the dictionary.")
92+
}
93+
t.Log("\t\tShould be able to get the first verb from the dictionary.")
94+
95+
_, err = d.Verb(-1)
96+
if err == nil {
97+
t.Error("\t\tShould not be able to get a verb from the dictionary with a negative index.")
98+
}
99+
t.Log("\t\tShould not be able to get a verb from the dictionary with a negative index.")
100+
101+
if len(d.adectives) != d.LengthAdjective() {
102+
t.Error("\t\tShould be able to get the length of the adjective file.")
103+
}
104+
t.Log("\t\tShould be able to get the length of the adjective file.")
105+
106+
if len(d.adverbs) != d.LengthAdverb() {
107+
t.Error("\t\tShould be able to get the length of the adverb file.")
108+
}
109+
t.Log("\t\tShould be able to get the length of the adverb file.")
110+
111+
if len(d.nouns) != d.LengthNoun() {
112+
t.Error("\t\tShould be able to get the length of the noun file.")
113+
}
114+
t.Log("\t\tShould be able to get the length of the noun file.")
115+
116+
if len(d.verbs) != d.LengthVerb() {
117+
t.Error("\t\tShould be able to get the length of the verb file.")
118+
}
119+
t.Log("\t\tShould be able to get the length of the verb file.")
120+
}
121+
}
122+
}

generator_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package fname
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestNewGenerator(t *testing.T) {
9+
t.Log("Given the need to test the NewGenerator function")
10+
{
11+
t.Log("\tWhen creating a new Generator with default values")
12+
{
13+
g := NewGenerator()
14+
if g == nil {
15+
t.Fatal("\t\tShould be able to create a Generator instance.")
16+
}
17+
t.Log("\t\tShould be able to create a Generator instance.")
18+
19+
if g.dict == nil {
20+
t.Fatal("\t\tShould be able to create a Dictionary instance.")
21+
}
22+
t.Log("\t\tShould be able to create a Dictionary instance.")
23+
}
24+
25+
t.Log("\tWhen creating a new Generator with custom values")
26+
{
27+
g := NewGenerator(WithDelimiter("_"), WithSize(3), WithSeed(12345))
28+
if g == nil {
29+
t.Fatal("\t\tShould be able to create a Generator instance.")
30+
}
31+
t.Log("\t\tShould be able to create a Generator instance.")
32+
33+
if g.size != 3 {
34+
t.Fatal("\t\tShould be able to set the size of the phrase.")
35+
}
36+
t.Log("\t\tShould be able to set the size of the phrase.")
37+
38+
if g.delimiter != "_" {
39+
t.Fatal("\t\tShould be able to set the delimiter of the phrase.")
40+
}
41+
t.Log("\t\tShould be able to set the delimiter of the phrase.")
42+
43+
if g.seed != 12345 {
44+
t.Fatal("\t\tShould be able to set the seed of the phrase.")
45+
}
46+
t.Log("\t\tShould be able to set the seed of the phrase.")
47+
}
48+
}
49+
}
50+
51+
func TestGenerate(t *testing.T) {
52+
t.Log("Given the need to test the Generate function")
53+
{
54+
t.Log("\tWhen generating a phrase")
55+
{
56+
g := NewGenerator()
57+
phrase, err := g.Generate()
58+
if err != nil {
59+
t.Fatal("\t\tShould be able to generate a phrase without error.")
60+
}
61+
t.Log("\t\tShould be able to generate a phrase without error.")
62+
63+
if len(phrase) == 0 {
64+
t.Fatal("\t\tShould be able to generate a phrase of non-zero length.")
65+
}
66+
t.Log("\t\tShould be able to generate a phrase of non-zero length.")
67+
68+
parts := strings.Split(phrase, "-")
69+
if len(parts) != 2 {
70+
t.Fatal("\t\tShould be able to generate a phrase with 2 parts.")
71+
}
72+
t.Log("\t\tShould be able to generate a phrase with 2 parts.")
73+
}
74+
75+
t.Log("\tWhen generating a phrase with a custom delimiter")
76+
{
77+
g := NewGenerator(WithDelimiter("_"))
78+
phrase, err := g.Generate()
79+
if err != nil {
80+
t.Fatal("\t\tShould be able to generate a phrase without error.")
81+
}
82+
t.Log("\t\tShould be able to generate a phrase without error.")
83+
84+
if len(phrase) == 0 {
85+
t.Fatal("\t\tShould be able to generate a phrase of non-zero length.")
86+
}
87+
t.Log("\t\tShould be able to generate a phrase of non-zero length.")
88+
89+
parts := strings.Split(phrase, "_")
90+
if len(parts) != 2 {
91+
t.Fatal("\t\tShould be able to generate a phrase with 2 parts.")
92+
}
93+
t.Log("\t\tShould be able to generate a phrase with 2 parts.")
94+
95+
if !strings.Contains(phrase, "_") {
96+
t.Fatal("\t\tShould be able to generate a phrase with the custom delimiter.")
97+
}
98+
t.Log("\t\tShould be able to generate a phrase with the custom delimiter.")
99+
}
100+
101+
t.Log("\tWhen generating a phrase with a custom size")
102+
{
103+
g3 := NewGenerator(WithSize(3))
104+
phrase, err := g3.Generate()
105+
if err != nil {
106+
t.Fatal("\t\tShould be able to generate a phrase without error.")
107+
}
108+
t.Log("\t\tShould be able to generate a phrase without error.")
109+
110+
if len(phrase) == 0 {
111+
t.Fatal("\t\tShould be able to generate a phrase of non-zero length.")
112+
}
113+
t.Log("\t\tShould be able to generate a phrase of non-zero length.")
114+
115+
parts := strings.Split(phrase, "-")
116+
if len(parts) != 3 {
117+
t.Fatal("\t\tShould be able to generate a phrase with 3 parts.")
118+
}
119+
t.Log("\t\tShould be able to generate a phrase with 3 parts.")
120+
121+
g4 := NewGenerator(WithSize(4))
122+
phrase, err = g4.Generate()
123+
if err != nil {
124+
t.Fatal("\t\tShould be able to generate a phrase without error.")
125+
}
126+
t.Log("\t\tShould be able to generate a phrase without error.")
127+
128+
if len(phrase) == 0 {
129+
t.Fatal("\t\tShould be able to generate a phrase of non-zero length.")
130+
}
131+
t.Log("\t\tShould be able to generate a phrase of non-zero length.")
132+
133+
parts = strings.Split(phrase, "-")
134+
if len(parts) != 4 {
135+
t.Fatal("\t\tShould be able to generate a phrase with 4 parts.")
136+
}
137+
t.Log("\t\tShould be able to generate a phrase with 4 parts.")
138+
}
139+
140+
t.Log("\tWhen generating a phrase with a custom seed")
141+
{
142+
g1 := NewGenerator(WithSeed(12345))
143+
phrase1, err := g1.Generate()
144+
if err != nil {
145+
t.Fatal("\t\tShould be able to generate a phrase without error.")
146+
}
147+
t.Log("\t\tShould be able to generate a phrase without error.")
148+
149+
g2 := NewGenerator(WithSeed(12345))
150+
phrase2, err := g2.Generate()
151+
if err != nil {
152+
t.Fatal("\t\tShould be able to generate a phrase without error.")
153+
}
154+
t.Log("\t\tShould be able to generate a phrase without error.")
155+
156+
if phrase1 != phrase2 {
157+
t.Fatal("\t\tShould be able to generate the same phrase with the same seed.")
158+
}
159+
t.Log("\t\tShould be able to generate the same phrase with the same seed.")
160+
}
161+
162+
t.Log("\tWhen generating a phrase with an invalid size")
163+
{
164+
g := NewGenerator(WithSize(1))
165+
_, err := g.Generate()
166+
if err == nil {
167+
t.Fatal("\t\tShould not be able to generate a phrase with an invalid size.")
168+
}
169+
t.Log("\t\tShould not be able to generate a phrase with an invalid size.")
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)