-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Lab4 numeronym and test cases (#542)
* Implemented Lab4 numeronym and test cases
- Loading branch information
1 parent
6f1460e
commit c85a563
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |