diff --git a/emoji_test.go b/emoji_test.go new file mode 100644 index 0000000..8af652d --- /dev/null +++ b/emoji_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "strings" + "testing" +) + +func TestEmojiReplacer(t *testing.T) { + emoji = *emojiReplacer() + + if &emoji == nil { + t.Error("Emoji replacer is empty") + } +} + +func TestEmojiToDescription(t *testing.T) { + if &emoji == nil { + emoji = *emojiReplacer() + } + + if strings.ToLower(emoji.Replace("🕴️")) != "uomo con completo che levita" { + t.Error("Emoji replacer gave wrong description") + } +} diff --git a/main.go b/main.go index 14cc54d..7f0e0e0 100644 --- a/main.go +++ b/main.go @@ -70,9 +70,7 @@ func init() { break } - // Read adjective - foo, _ := ioutil.ReadFile("parole.txt") - adjectives = strings.Split(string(foo), "\n") + initializeAdjectives() // Initialize rand rand.Seed(time.Now().Unix()) @@ -163,3 +161,9 @@ func ready(s *discordgo.Session, _ *discordgo.Ready) { func guildCreate(_ *discordgo.Session, e *discordgo.GuildCreate) { initializeServer(e.Guild.ID) } + +// Reads adjectives +func initializeAdjectives() { + foo, _ := ioutil.ReadFile("parole.txt") + adjectives = strings.Split(string(foo), "\n") +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..229cbc7 --- /dev/null +++ b/main_test.go @@ -0,0 +1,61 @@ +package main + +import ( + "os" + "runtime" + "strings" + "testing" +) + +func BenchmarkBestemmia(b *testing.B) { + // Read adjectives if they are not loaded before + if len(adjectives) == 0 { + initializeAdjectives() + } + + for i := 0; i < b.N; i++ { + bestemmia() + } +} + +func TestInitializeAdjectives(t *testing.T) { + initializeAdjectives() + + if len(adjectives) == 0 { + t.Error("Adjectives slice is empty") + } +} + +func TestBestemmia(t *testing.T) { + // Read adjectives if they are not loaded before + if len(adjectives) == 0 { + initializeAdjectives() + } + + if strings.TrimSpace(bestemmia()) == "" { + t.Error("Generated string is empty") + } +} + +func TestGenAudio(t *testing.T) { + if runtime.GOOS == "windows" { + _ = os.Remove("./temp/NP5M2VS4G9AQEEIPC6V6DQH5J1RGS4PE.dca") + uuid := genAudio("AGAGAGAGAGA") + stat, err := os.Stat("./temp/NP5M2VS4G9AQEEIPC6V6DQH5J1RGS4PE.dca") + + if uuid != "NP5M2VS4G9AQEEIPC6V6DQH5J1RGS4PE.dca" { + t.Error("Hash mismatch") + } else { + if err != nil { + t.Error("File doesn't exist") + } else { + if !(stat.Size() > 0) { + t.Error("File is empty") + } + } + } + } else { + t.Skip("Audio generation is only supported by windows") + } + +}