forked from anz-bank/go-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lab 4 - Numeronym (anz-bank#640)
Add lab 4 - Numeronym
- Loading branch information
Showing
2 changed files
with
101 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,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"unicode/utf8" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
func main() { | ||
fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc")) | ||
} | ||
|
||
func numeronyms(vals ...string) []string { | ||
r := make([]string, len(vals)) | ||
for k, v := range vals { | ||
r[k] = numeronym(v) | ||
} | ||
return r | ||
} | ||
|
||
func numeronym(s string) string { | ||
numrunes := utf8.RuneCountInString(s) | ||
if numrunes < 4 { | ||
return s | ||
} | ||
firstrune, _ := utf8.DecodeRuneInString(s) | ||
lastrune, _ := utf8.DecodeLastRuneInString(s) | ||
|
||
return string(firstrune) + strconv.Itoa(numrunes-2) + string(lastrune) | ||
} |
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,67 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNumeronym(t *testing.T) { | ||
var tests = map[string]struct { | ||
arg string | ||
want string | ||
}{ | ||
"empty string": {"", ""}, | ||
"single string of 1 element": {"a", "a"}, | ||
"single long string": {"abracadabra", "a9a"}, | ||
"unicode at the start": {"🤪bracadabra", "🤪9a"}, | ||
"unicode in the middle": {"abra🤪🤪🤪abra", "a9a"}, | ||
"unicode in the end": {"abracadabr🤪", "a9🤪"}, | ||
"1 poo": {"💩", "💩"}, | ||
"2 poos": {"💩💩", "💩💩"}, | ||
"3 poos": {"💩💩💩", "💩💩💩"}, | ||
"4 poos": {"💩💩💩💩", "💩2💩"}, | ||
} | ||
|
||
for name, tt := range tests { | ||
test := tt | ||
t.Run(name, func(t *testing.T) { | ||
got := numeronym(test.arg) | ||
assert.Equal(t, test.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestNumeronyms(t *testing.T) { | ||
var tests = map[string]struct { | ||
arg []string | ||
want []string | ||
}{ | ||
"empty string": { | ||
[]string{""}, | ||
[]string{""}}, | ||
"mixed strings": { | ||
[]string{"", "alakazam", "hocuspocus", "mumbojumbo", "💩💩💩💩"}, | ||
[]string{"", "a6m", "h8s", "m8o", "💩2💩"}}, | ||
} | ||
|
||
for name, tt := range tests { | ||
test := tt | ||
t.Run(name, func(t *testing.T) { | ||
got := numeronyms(test.arg...) | ||
assert.Equal(t, test.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
want := "[a11y K8s abc]\n" | ||
t.Run("main test", func(t *testing.T) { | ||
var buf bytes.Buffer | ||
out = &buf | ||
main() | ||
got := buf.String() | ||
assert.Equal(t, got, want) | ||
}) | ||
} |