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

Implemented Lab4 numeronym and test cases #542

Merged
merged 4 commits into from
Jul 15, 2019
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
25 changes: 25 additions & 0 deletions 04_numeronym/sirigithub/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func numeronyms(vals ...string) []string {
result := make([]string, len(vals))
for i, val := range vals {
r := []rune(val)
runeLength := len(r)
if runeLength > 3 {
val = fmt.Sprintf("%c%d%c", r[0], runeLength-2, r[runeLength-1])
}
sirigithub marked this conversation as resolved.
Show resolved Hide resolved
result[i] = val
}
return result
}
func main() {
fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc"))
}
46 changes: 46 additions & 0 deletions 04_numeronym/sirigithub/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
actual := buf.String()
expected := "[a11y K8s abc]\n"
assert.Equal(t, expected, actual)

}
func TestNumeronyms(t *testing.T) {
tests := []struct {
description string
input,
expected []string
}{
{description: "Empty string", input: []string{""}, expected: []string{""}},
{description: "Short strings", input: []string{"sss", "ab", "a"}, expected: []string{"sss", "ab", "a"}},
{description: "Alphanumeric string", input: []string{"sss1245sdfg"}, expected: []string{"s9g"}},
{description: "Back slashes and spaces", input: []string{"sss\\", "A long string with spaces"},
expected: []string{"s2\\", "A23s"}},
{description: "Emoji strings", input: []string{"😄🐷🙈🐷🏃😄", "😄🏃😄"},
expected: []string{"😄4😄", "😄🏃😄"}},
{description: "Unicode Strings", input: []string{"日本語日本語", "äö߀’üüüöäßß"},
expected: []string{"日4語", "ä10ß"}},
{description: "Strings with special characters",
input: []string{"a##67&a$", "**(]]"},
expected: []string{"a6$", "*3]"}},
}
for _, test := range tests {
test := test
t.Run(test.description, func(t *testing.T) {
actual := numeronyms(test.input...)
expected := test.expected
assert.Equal(t, actual, expected, "actual %v but expected %v", actual, expected)
})
}
}