Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regular expressions fixed, check whole string #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions client/go-simple-bot/mia-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
const serverAddr = "127.0.0.1:9000"

func validName(input string) bool {
valid, _ := regexp.Compile(`[^\s,;:]{1,20}`)
valid, _ := regexp.Compile(`^[^\s,;:]{1,20}$`)
return valid.MatchString(input)
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func main() {
fmt.Print(">>>> ")
_, err := fmt.Scanf("%s", &name)
if err != nil {
log.Fatalf("Reading username failed:", err.Error())
log.Fatalf("Reading username failed: %v", err.Error())
}
}

Expand All @@ -99,3 +99,5 @@ func main() {
}
}
}


120 changes: 120 additions & 0 deletions client/go-simple-bot/mia-client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import (
"net"
"testing"
"time"
)

func TestValidName(t *testing.T) {
name := "foo:bar"
if validName(name) {
t.Fatalf("Name should't be valid: %s", name)
}

name = "foooooooooooooooooooo"
if validName(name) {
t.Fatalf("Name should't be valid: %s", name)
}

name = "foo bar"
if validName(name) {
t.Fatalf("Name should't be valid: %s", name)
}

name = "foo,bar"
if validName(name) {
t.Fatalf("Name should't be valid: %s", name)
}

name = "foo;bar"
if validName(name) {
t.Fatalf("Name should't be valid: %s", name)
}

name = "foobar5"
if !validName(name) {
t.Fatalf("Name should be valid: %s", name)
}
}

func TestNewConnection(t *testing.T) {
c := newConnection()
if c == nil {
t.Fatal("UDP connection should exists")
}
}

func TestReadFromServer(t *testing.T) {
c, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 9000})
if err != nil {
t.Fatalf("ListenUDP failed: %v", err)
}
defer c.Close()

replies := make(chan string)
done := make(chan bool)

cc := newConnection()
defer cc.Close()

go func() {
for {
timeout := time.After(5 * time.Second)
select {
case answer, more := <-replies:
if !more {
done <- true
return
} else if answer != "foo" && answer != "bar" {
t.Fatalf("Expected foo or bar, got %s", answer)
}
case <-timeout:
t.Fatal("timed out")
return
default:
done <- true
}

}
}()

cc.Write([]byte("foo"))
<-done
cc.Write([]byte("bar"))
<-done
}

func TestHandleResponse(t *testing.T) {
replies := make(chan string)
done := make(chan bool)
go func() {
for {
timeout := time.After(5 * time.Second)
select {
case answer, more := <-replies:
if !more {
done <- true
return
} else if answer != "JOIN;foo" && answer != "ROLL;foo" && answer != "ANNOUNCE;foo;bar" {
t.Fatalf("Expected JOIN;foo, got %s", answer)
}
case <-timeout:
t.Fatal("timed out")
return
default:
done <- true
}

}
}()

handleResponse("ROUND STARTING;foo", replies)
<-done
handleResponse("YOUR TURN;foo", replies)
<-done
handleResponse("ROLLED;foo;bar", replies)
}

func TestMessageServer(t *testing.T) {
}