Skip to content

Commit

Permalink
Make Generate work as it shold
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
Sergey Kibish committed Dec 1, 2019
1 parent 29f2c3a commit fd4804b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
103 changes: 59 additions & 44 deletions ipsum/ipsum.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
"strings"
)

func getWord() string {
return dictionary[rand.Intn(len(dictionary))]
}

// LoremIpsum is a struct that holds lorem ipsum generator
type LoremIpsum struct {
startLorem bool
Expand All @@ -41,7 +45,7 @@ func New(w io.Writer, s bool, o Option, v int) *LoremIpsum {
return &LoremIpsum{
w: w,
startLorem: s,
limiter: limiter{option: o, value: v, paragraphCount: 1},
limiter: limiter{option: o, value: v},
}
}

Expand All @@ -57,69 +61,80 @@ func (l *LoremIpsum) Generate() error {
}
}

beginning := true
var previousWord string
for {
w := l.getNextWord(&previousWord)

if beginning {
beginning = false
if err := l.printf("%s", strings.Title(w)); err != nil {
return err
}
} else {
if err := l.print(w); err != nil {
return err
}
}
capitalize := true
for !l.limiter.limitReached() {
l.printWord(capitalize)

if l.limiter.limitReached() {
if err := l.print("."); err != nil {
return err
}
break
var err error
var isParagraph bool
capitalize, isParagraph, err = l.printPunctuation()
if err != nil {
return fmt.Errorf("failed to print sign: %w", err)
}

if rand.Float32() >= 0.90 {
if err := l.print(", "); err != nil {
return err
}
if isParagraph {
continue
}

if rand.Float32() >= 0.90 {
beginning = true
if err := l.print(". "); err != nil {
if !l.limiter.limitReached() {
if err := l.print(" "); err != nil {
return err
}
continue
}
}

if rand.Float32() >= 0.98 {
beginning = true
l.limiter.addParagraph()
if err := l.print(".\n"); err != nil {
return err
}
continue
if l.limiter.option != paragraphs {
l.limiter.addParagraph()
if err := l.print(".\n"); err != nil {
return err
}
}

return nil
}

if err := l.print(" "); err != nil {
func (l *LoremIpsum) printWord(capitalized bool) error {
l.limiter.addWord()
w := getWord()

if capitalized {
if err := l.printf("%s", strings.Title(w)); err != nil {
return err
}

return nil
}

if err := l.print(w); err != nil {
return err
}

return nil
}

func (l *LoremIpsum) getNextWord(prev *string) string {
l.limiter.addWord()
w := dictionary[rand.Intn(len(dictionary))]
for *prev == w {
w = dictionary[rand.Intn(len(dictionary))]
func (l *LoremIpsum) printPunctuation() (beginning bool, isParagraph bool, err error) {
if rand.Float32() >= 0.90 {
if err := l.print(","); err != nil {
return false, false, err
}
return
}

if rand.Float32() >= 0.90 {
if err := l.print("."); err != nil {
return false, false, err
}
return true, false, nil
}

if rand.Float32() >= 0.98 {
l.limiter.addParagraph()
if err := l.print(".\n"); err != nil {
return false, false, err
}
return true, true, nil
}

return w
return
}

func (l *LoremIpsum) print(a string) error {
Expand Down
5 changes: 1 addition & 4 deletions ipsum/ispum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestGeneration(t *testing.T) {
}

realParagraphCount := strings.Count(str, "\n")
realParagraphCount++
expectedParagraphs := 1
if stats.ParagraphCount != realParagraphCount {
t.Errorf("Expected count of paragraphs to be %d, got: %d", expectedParagraphs, stats.ParagraphCount)
Expand All @@ -67,7 +66,6 @@ func TestGeneration(t *testing.T) {
}

realParagraphCount := strings.Count(str, "\n")
realParagraphCount++
expectedParagraphs := 1
if stats.ParagraphCount != realParagraphCount {
t.Errorf("Expected count of paragraphs to be %d, got: %d", expectedParagraphs, stats.ParagraphCount)
Expand All @@ -88,7 +86,7 @@ func TestGeneration(t *testing.T) {
t.Errorf("Expected count of paragraphs to be %d, got: %d", expectedParagraphs, stats.ParagraphCount)
}

expectedBytes := 455
expectedBytes := 456
if stats.ByteCount != expectedBytes {
t.Errorf("Expected count of bytes to be %d, got: %d", expectedBytes, stats.ByteCount)
}
Expand All @@ -99,7 +97,6 @@ func TestGeneration(t *testing.T) {

str, stats := runGenerator(t, testCase{paragraphs, expectedParagraphs, false})
realParag := strings.Count(str, "\n")
realParag++

if stats.ParagraphCount != realParag {
t.Errorf("Expected count of paragraphs to be %d, got: %d", expectedParagraphs, realParag)
Expand Down

0 comments on commit fd4804b

Please sign in to comment.