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

Implement lab 04 numeronym #518

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions 04_numeronym/undrewb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc"))
}

func numeronyms(vals ...string) []string {
strs := make([]string, len(vals))
for i, s := range vals {
strs[i] = numeronym(s)
}
return strs
}

func numeronym(s string) string {
if len([]rune(s)) > 3 {
r := []rune(s)
s = fmt.Sprintf("%c%d%c", r[0], len(r)-2, r[len(r)-1])
}
return s
}
89 changes: 89 additions & 0 deletions 04_numeronym/undrewb/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"bytes"
"reflect"
"strconv"
"testing"
)

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf

main()

want := strconv.Quote("[a11y K8s abc]\n")
got := strconv.Quote(buf.String())

if got != want {
t.Errorf("actual: %s does not match expected: %s", got, want)
}
}

var numeronymsData = []struct {
name string
input []string
want []string
}{
{
name: "lab example",
input: []string{"accessibility", "Kubernetes", "abc"},
want: []string{"a11y", "K8s", "abc"},
},
{
name: "empty",
input: []string{},
want: []string{},
},
{
name: "unicode example",
input: []string{"ចឆជឈញដឋឌឍណតថទធនបផពភមយ", "ঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদ", "АБВ", "АБВГДЕЖ", "👍👍"},
want: []string{"ច19យ", "ঌ21দ", "АБВ", "А5Ж", "👍👍"},
},
{
name: "unicode/ascii mix example",
input: []string{"aaចឆជឈញដឋឌឍណតថទធនបផពភមយbb", "aঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদ", "АБc", "АБВerЕЖ", "a👍👍x"},
want: []string{"a23b", "a22দ", "АБc", "А5Ж", "a2x"},
},
}

func TestNumeronyms(t *testing.T) {
undrewb marked this conversation as resolved.
Show resolved Hide resolved
for _, tt := range numeronymsData {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := numeronyms(tt.input...)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("numeronyms() = \n%v,\nwant \n%v", got, tt.want)
}
})
}
}

var numeronymData = []struct {
name string
input string
want string
}{
{name: "lab example", input: "Kubernetes", want: "K8s"},
{name: "trailing newline", input: "aba", want: "aba"},
{name: "duplicate entries", input: "KuKuKuKuK", want: "K7K"},
{name: "emptyr", input: "", want: ""},
{name: "space example", input: "blah blah", want: "b7h"},
{name: "i1bn", input: "internationalization", want: "i18n"},
{name: "thai", input: "ចឆជឈញដឋឌឍណតថទធនបផពភមយ", want: "ច19យ"},
{name: "short greek", input: "АБВ", want: "АБВ"},
{name: "emoji mix", input: "👍👍axsx", want: "👍4x"},
}

func TestNumeronym(t *testing.T) {
for _, tt := range numeronymData {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := numeronym(tt.input)
if got != tt.want {
t.Errorf("numeronym() = \n%v,\nwant \n%v", got, tt.want)
}
})
}
}