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 1 commit
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
30 changes: 30 additions & 0 deletions 04_numeronym/undrewb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 {
outs := s
undrewb marked this conversation as resolved.
Show resolved Hide resolved
if len(s) > 3 {
r := []rune(s)
outs = fmt.Sprintf("%c%d%c", r[0], len(r)-2, r[len(r)-1])
}
return outs
}
72 changes: 72 additions & 0 deletions 04_numeronym/undrewb/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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{}},
}

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"},
}

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)
}
})
}
}