From b845b1e4a122d70e93c6dc0ea48088a05925c43a Mon Sep 17 00:00:00 2001 From: "Bucknell, Andrew" Date: Sun, 30 Jun 2019 15:44:41 +1000 Subject: [PATCH 1/2] Implement lab 04 numeronym Implement functions Implement tests --- 04_numeronym/undrewb/main.go | 30 +++++++++++++ 04_numeronym/undrewb/main_test.go | 72 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 04_numeronym/undrewb/main.go create mode 100644 04_numeronym/undrewb/main_test.go diff --git a/04_numeronym/undrewb/main.go b/04_numeronym/undrewb/main.go new file mode 100644 index 000000000..34a65679c --- /dev/null +++ b/04_numeronym/undrewb/main.go @@ -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 + if len(s) > 3 { + r := []rune(s) + outs = fmt.Sprintf("%c%d%c", r[0], len(r)-2, r[len(r)-1]) + } + return outs +} diff --git a/04_numeronym/undrewb/main_test.go b/04_numeronym/undrewb/main_test.go new file mode 100644 index 000000000..9afd7baa9 --- /dev/null +++ b/04_numeronym/undrewb/main_test.go @@ -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) { + 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) + } + }) + } +} From 0b9fb92002bdb7ba9b3ad3e9d8ab6cc5873820e9 Mon Sep 17 00:00:00 2001 From: "Bucknell, Andrew" Date: Thu, 15 Aug 2019 22:17:38 +1000 Subject: [PATCH 2/2] Implement unicode handling Add test cases for unicode Modify numeronym to use rune length not byte len Add test cases for unicode --- 04_numeronym/undrewb/main.go | 7 +++---- 04_numeronym/undrewb/main_test.go | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/04_numeronym/undrewb/main.go b/04_numeronym/undrewb/main.go index 34a65679c..8844014ed 100644 --- a/04_numeronym/undrewb/main.go +++ b/04_numeronym/undrewb/main.go @@ -21,10 +21,9 @@ func numeronyms(vals ...string) []string { } func numeronym(s string) string { - outs := s - if len(s) > 3 { + if len([]rune(s)) > 3 { r := []rune(s) - outs = fmt.Sprintf("%c%d%c", r[0], len(r)-2, r[len(r)-1]) + s = fmt.Sprintf("%c%d%c", r[0], len(r)-2, r[len(r)-1]) } - return outs + return s } diff --git a/04_numeronym/undrewb/main_test.go b/04_numeronym/undrewb/main_test.go index 9afd7baa9..56f1ce601 100644 --- a/04_numeronym/undrewb/main_test.go +++ b/04_numeronym/undrewb/main_test.go @@ -26,12 +26,26 @@ var numeronymsData = []struct { input []string want []string }{ - {name: "lab example", + { + name: "lab example", input: []string{"accessibility", "Kubernetes", "abc"}, - want: []string{"a11y", "K8s", "abc"}}, - {name: "empty", + want: []string{"a11y", "K8s", "abc"}, + }, + { + name: "empty", input: []string{}, - want: []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) { @@ -57,6 +71,9 @@ var numeronymData = []struct { {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) {