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

Fix width #14

Merged
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go:
- master

before_script:
- go get -d
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Requires [git](https://git-scm.com/download/win) to clone and [Go](https://golan
```bash
$ git clone https://github.com/muhammadmuzzammil1998/catsay.git
$ cd catsay
$ go get -d
$ go build
```

Expand Down
12 changes: 7 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
"io"
"os"
"strings"
"unicode/utf8"

"github.com/mattn/go-runewidth"
)

//Defining a main cat
Expand Down Expand Up @@ -87,9 +88,9 @@ func readLines(reader *bufio.Reader) []string {
func getWidth(message []string) int {
ret := cat.MinLen
for _, l := range message {
len := utf8.RuneCountInString(l)
if len > ret {
ret = len
width := runewidth.StringWidth(l)
if width > ret {
ret = width
}
}
return ret
Expand Down Expand Up @@ -131,7 +132,8 @@ func removeTabs(message []string) []string {
func formatMessage(message []string, width int) []string {
var ret []string
for _, l := range message {
ret = append(ret, l+strings.Repeat(" ", width-utf8.RuneCountInString(l)))
w := runewidth.StringWidth(l)
ret = append(ret, l+strings.Repeat(" ", width-w))
}
return ret
}
Expand Down
16 changes: 16 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ func TestReadLines(t *testing.T) {
t.Fatalf("Couldn't parse bufio.Reader")
}
}

func TestGetWidth(t *testing.T) {
setup()
r := getWidth(message)
if r < 9 {
t.Fatalf("Expected to be greater or equal to 9 but got %d", r)
}
}

func TestRemoveTabs(t *testing.T) {
r := removeTabs(message)
if r[4] != "this one has tabs!" {
t.Fatalf("Tabs were not replaced with spaces")
}
}

func TestCreateMessage(t *testing.T) {
setup()
data := removeTabs(message)
Expand All @@ -68,6 +71,7 @@ func TestCreateMessage(t *testing.T) {
t.Fatalf("Expected\n%s\nbut got\n%s", e, r)
}
}

func TestFormatMessage(t *testing.T) {
setup()
data := removeTabs(message)
Expand All @@ -81,3 +85,15 @@ func TestFormatMessage(t *testing.T) {
t.Fatalf("Length of each line should be equal. Expected %d but got %d", eLen, rLen)
}
}

func TestMultiByteString(t *testing.T) {
setup()
data := removeTabs(buildMessage("こんにちわ世界"))
got := createMessage(data, getWidth(data))
var want = ` ________________
< こんにちわ世界 >
----------------`
if got != want {
t.Fatalf("Expected\n%s\nbut got\n%s", want, got)
}
}