Skip to content

Commit

Permalink
Add lab 4 - Numeronym (anz-bank#640)
Browse files Browse the repository at this point in the history
Add lab 4 - Numeronym
  • Loading branch information
alextmz authored and Jim hejtmanek committed Jul 31, 2020
1 parent 999040b commit fc0ef5f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 04_numeronym/alextmz/main.go
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)
}
67 changes: 67 additions & 0 deletions 04_numeronym/alextmz/main_test.go
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)
})
}

0 comments on commit fc0ef5f

Please sign in to comment.