Skip to content

Commit

Permalink
feat: add swear words analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
YasminTeles committed Nov 11, 2021
1 parent a5dabe0 commit e328654
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
35 changes: 35 additions & 0 deletions badword/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// nolint: errcheck
package badword

import (
"log"

"github.com/YasminTeles/CatMQ/client"
"github.com/YasminTeles/CatMQ/message"
)

func Start() {
client.Connect()
// defer client.Disconnect()

client.Consumer()

badwords, err := NewBadWords()
if err != nil {
log.Fatalf("Some error occurred. err: %v", err)
}

phrase := client.Get()

for isValid(phrase) {
if !badwords.Check(phrase) {
client.Publish(phrase)
}

phrase = client.Get()
}
}

func isValid(phrase string) bool {
return phrase != "" && phrase != message.MessageError
}
56 changes: 56 additions & 0 deletions badword/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package badword

import (
"testing"
"time"

"github.com/YasminTeles/CatMQ/client"
"github.com/YasminTeles/CatMQ/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type ConsumerTestSuite struct {
suite.Suite
}

func (suite *ConsumerTestSuite) SetupSuite() {
go server.ListenAndServe()
time.Sleep(1 * time.Second)
}

func (suite *ConsumerTestSuite) TearDownSuite() {
server.Close()
}

func (suite *ConsumerTestSuite) TestStart() {
client.Connect()
// defer client.Disconnect()

client.Producer()
coolMessage := "Me gusta oír el mar."
client.Publish(coolMessage)

niceMessage := "Arturo y Lucho son mis patas."
client.Publish(niceMessage)

badMessage := "O pato feio foi a feira."
client.Publish(badMessage)

Start()

result := client.Get()
assert.Equal(suite.T(), coolMessage, result)

result = client.Get()
assert.Equal(suite.T(), niceMessage, result)

result = client.Get()
assert.Equal(suite.T(), "", result)
}

func TestConsumerTestSuite(t *testing.T) {
t.Parallel()

suite.Run(t, new(ConsumerTestSuite))
}

0 comments on commit e328654

Please sign in to comment.