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 1 commit
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
26 changes: 26 additions & 0 deletions 04_numeronym/sirigithub/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 < 4 {
result[i] = val
} else {
result[i] = fmt.Sprintf("%c%d%c", r[0], runeLength-2, r[runeLength-1])
}
sirigithub marked this conversation as resolved.
Show resolved Hide resolved
}
return result
}
func main() {
fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc"))
}
45 changes: 45 additions & 0 deletions 04_numeronym/sirigithub/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 charecters",
sirigithub marked this conversation as resolved.
Show resolved Hide resolved
input: []string{"a##67&a$", "**(]]"},
expected: []string{"a6$", "*3]"}},
}
for _, test := range tests {
actual := numeronyms(test.input...)
sirigithub marked this conversation as resolved.
Show resolved Hide resolved
expected := test.expected
t.Run(test.description, func(t *testing.T) {
assert.Equal(t, actual, expected, "actual %v but expected %v", actual, expected)
})
}
}