-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
var loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ultrices sagittis orci a scelerisque purus. At volutpat diam ut venenatis tellus in. Mattis rhoncus urna neque viverra justo nec ultrices dui sapien. Tellus orci ac auctor augue mauris. Eu scelerisque felis imperdiet proin fermentum. Tortor id aliquet lectus proin nibh nisl condimentum id. Id donec ultrices tincidunt arcu non sodales. Ultrices dui sapien eget mi proin. Bibendum neque egestas congue quisque egestas diam. Sem fringilla ut morbi tincidunt augue interdum. Vel risus commodo viverra maecenas accumsan lacus vel facilisis volutpat. Morbi blandit cursus risus at ultrices mi tempus. Adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus. Sapien pellentesque habitant morbi tristique senectus et netus et malesuada.` | ||
|
||
var loremIpsumWords = strings.Fields(loremIpsum) | ||
var loremIpsumSentences = strings.Split(loremIpsum, ".") | ||
|
||
func generateLoremIpsum(lengthType string, count int) string { | ||
switch lengthType { | ||
case "words": | ||
return generateWords(count) | ||
case "sentences": | ||
return generateSentences(count) | ||
case "paragraphs": | ||
return generateParagraphs(count) | ||
default: | ||
return "Invalid length type. Please specify 'words', 'sentences', or 'paragraphs'." | ||
} | ||
} | ||
|
||
func generateWords(count int) string { | ||
var words strings.Builder | ||
for i := range count { | ||
index := i % len(loremIpsumWords) | ||
if i > 0 { | ||
words.WriteString(" ") | ||
} | ||
words.WriteString(loremIpsumWords[index]) | ||
} | ||
return words.String() | ||
} | ||
|
||
func generateSentences(count int) string { | ||
var sentences strings.Builder | ||
for i := range count { | ||
index := i % len(loremIpsumSentences) | ||
sentences.WriteString(loremIpsumSentences[index]) | ||
sentences.WriteString(".") | ||
} | ||
return sentences.String() | ||
} | ||
|
||
func generateParagraphs(count int) string { | ||
paragraphs := []string{} | ||
for range count { | ||
paragraphs = append(paragraphs, loremIpsum) | ||
} | ||
return strings.Join(paragraphs, "\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGenerateWords(t *testing.T) { | ||
tests := []struct { | ||
count int | ||
expected string | ||
}{ | ||
{5, "Lorem ipsum dolor sit amet,"}, | ||
{10, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"}, | ||
{50, strings.Join(loremIpsumWords[:50], " ")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(strings.Join(loremIpsumWords[:tt.count], " "), func(t *testing.T) { | ||
result := generateWords(tt.count) | ||
if result != tt.expected { | ||
t.Errorf("generateWords(%d) = %q; want %q", tt.count, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenerateSentences(t *testing.T) { | ||
tests := []struct { | ||
count int | ||
expected string | ||
}{ | ||
{1, loremIpsumSentences[0] + "."}, | ||
{2, strings.Join(loremIpsumSentences[:2], ".") + "."}, | ||
{5, strings.Join(loremIpsumSentences[:5], ".") + "."}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(strings.Join(loremIpsumSentences[:tt.count], ".")+".", func(t *testing.T) { | ||
result := generateSentences(tt.count) | ||
if result != tt.expected { | ||
t.Errorf("generateSentences(%d) = %q; want %q", tt.count, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenerateParagraphs(t *testing.T) { | ||
tests := []struct { | ||
count int | ||
expected string | ||
}{ | ||
{1, loremIpsum}, | ||
{2, loremIpsum + "\n\n" + loremIpsum}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(strings.Repeat(loremIpsum+"\n\n", tt.count)[:len(strings.Repeat(loremIpsum+"\n\n", tt.count))-2], func(t *testing.T) { | ||
result := generateParagraphs(tt.count) | ||
if result != tt.expected { | ||
t.Errorf("generateParagraphs(%d) = %q; want %q", tt.count, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenerateLoremIpsum(t *testing.T) { | ||
tests := []struct { | ||
lengthType string | ||
count int | ||
expected string | ||
}{ | ||
{"words", 5, "Lorem ipsum dolor sit amet,"}, | ||
{"sentences", 2, strings.Join(loremIpsumSentences[:2], ".") + "."}, | ||
{"paragraphs", 1, loremIpsum}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.lengthType, func(t *testing.T) { | ||
result := generateLoremIpsum(tt.lengthType, tt.count) | ||
if result != tt.expected { | ||
t.Errorf("generateLoremIpsum(%q, %d) = %q; want %q", tt.lengthType, tt.count, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |