Skip to content

Commit b559a85

Browse files
committed
refactor: remove dictionary accessor functions
Do not return errors from out-of-bounds indexing. Remove dictionary accessor methods, prefering direct access from slices. Remove extraneous test cases.
1 parent a1ca725 commit b559a85

3 files changed

Lines changed: 5 additions & 119 deletions

File tree

dictionary.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package fname
44
import (
55
"bufio"
66
"embed"
7-
"fmt"
87
)
98

109
const (
@@ -53,38 +52,6 @@ func NewDictionary() *Dictionary {
5352
}
5453
}
5554

56-
// Adjective returns a random adjective.
57-
func (d *Dictionary) Adjective(idx int) (string, error) {
58-
if idx < 0 || idx >= len(d.adectives) {
59-
return "", fmt.Errorf("index out of range: %d", idx)
60-
}
61-
return d.adectives[idx], nil
62-
}
63-
64-
// Adverb returns a random adverb.
65-
func (d *Dictionary) Adverb(idx int) (string, error) {
66-
if idx < 0 || idx >= len(d.adverbs) {
67-
return "", fmt.Errorf("index out of range: %d", idx)
68-
}
69-
return d.adverbs[idx], nil
70-
}
71-
72-
// Noun returns a random noun.
73-
func (d *Dictionary) Noun(idx int) (string, error) {
74-
if idx < 0 || idx >= len(d.nouns) {
75-
return "", fmt.Errorf("index out of range: %d", idx)
76-
}
77-
return d.nouns[idx], nil
78-
}
79-
80-
// Verb returns a random verb.
81-
func (d *Dictionary) Verb(idx int) (string, error) {
82-
if idx < 0 || idx >= len(d.verbs) {
83-
return "", fmt.Errorf("index out of range: %d", idx)
84-
}
85-
return d.verbs[idx], nil
86-
}
87-
8855
// LengthAdjective returns the number of adjectives in the dictionary.
8956
func (d *Dictionary) LengthAdjective() int {
9057
return len(d.adectives)

dictionary_test.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,6 @@ func TestNewDictionary(t *testing.T) {
3232
t.Error("\t\tShould be able to load the verb file.")
3333
}
3434

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-
10135
if len(d.adectives) != d.LengthAdjective() {
10236
t.Error("\t\tShould be able to get the length of the adjective file.")
10337
}

generator.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,20 @@ func NewGenerator(opts ...GeneratorOption) *Generator {
5858
// Generate generates a random name.
5959
func (r *Generator) Generate() (string, error) {
6060
// TODO: address case where adjective and noun are the same, such as "orange-orange" or "sound-sound"
61-
adjective, err := r.dict.Adjective(rand.Intn(r.dict.LengthAdjective()))
62-
if err != nil {
63-
return "", err
64-
}
65-
noun, err := r.dict.Noun(rand.Intn(r.dict.LengthNoun()))
66-
if err != nil {
67-
return "", err
68-
}
61+
adjective := r.dict.adectives[rand.Intn(r.dict.LengthAdjective())]
62+
noun := r.dict.nouns[rand.Intn(r.dict.LengthNoun())]
6963
words := []string{adjective, noun}
7064

7165
switch r.size {
7266
case 2:
7367
return strings.Join(words, r.delimiter), nil
7468
case 3:
75-
verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb()))
76-
if err != nil {
77-
return "", err
78-
}
69+
verb := r.dict.verbs[rand.Intn(r.dict.LengthVerb())]
7970
words = append(words, verb)
8071
case 4:
81-
verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb()))
82-
if err != nil {
83-
return "", err
84-
}
72+
verb := r.dict.verbs[rand.Intn(r.dict.LengthVerb())]
8573
words = append(words, verb)
86-
adverb, err := r.dict.Adverb(rand.Intn(r.dict.LengthAdverb()))
87-
if err != nil {
88-
return "", err
89-
}
74+
adverb := r.dict.adverbs[rand.Intn(r.dict.LengthAdverb())]
9075
words = append(words, adverb)
9176
default:
9277
return "", fmt.Errorf("invalid size: %d", r.size)

0 commit comments

Comments
 (0)