Skip to content

Commit

Permalink
Implemented Lab4 numeronym and test cases (#542)
Browse files Browse the repository at this point in the history
* Implemented Lab4 numeronym and test cases
  • Loading branch information
sirigithub authored Jul 15, 2019
1 parent 6f1460e commit c85a563
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
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])
}
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)
})
}
}

0 comments on commit c85a563

Please sign in to comment.