-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from patrickmarabeas/lab-04-pm
Add numeronyms functionality
- Loading branch information
Showing
2 changed files
with
117 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,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
// GenerateNumeronym creates a numeronym from the given string. | ||
func generateNumeronym(s string) string { | ||
// Convert to runes to handle multi byte characters | ||
runes := []rune(s) | ||
length := len(runes) | ||
if length < 4 { | ||
return string(runes) | ||
} | ||
return fmt.Sprintf("%c%d%c", runes[0], length-2, runes[length-1]) | ||
} | ||
|
||
// Numeronyms takes an input of n strings and returns a slice containing each string's numeronym. | ||
func numeronyms(vals ...string) []string { | ||
result := make([]string, len(vals)) | ||
for i, numeronym := range vals { | ||
result[i] = generateNumeronym(numeronym) | ||
} | ||
return result | ||
} | ||
|
||
func main() { | ||
fmt.Fprint(out, numeronyms("accessibility", "Kubernetes", "abc")) | ||
} |
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,84 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMainOutput(t *testing.T) { | ||
var buf bytes.Buffer | ||
out = &buf | ||
|
||
main() | ||
|
||
expected := "[a11y K8s abc]" | ||
got := buf.String() | ||
|
||
t.Run("Main function", func(t *testing.T) { | ||
if expected != got { | ||
t.Errorf("\nExpected: %s\nGot: %s", expected, got) | ||
} | ||
}) | ||
} | ||
|
||
func TestGenerateNumeronym(t *testing.T) { | ||
var cases = map[string]struct { | ||
input string | ||
expected string | ||
}{ | ||
"Lowercase": { | ||
input: "lowercase", | ||
expected: "l7e", | ||
}, | ||
"Uppercase": { | ||
input: "Uppercase", | ||
expected: "U7e", | ||
}, | ||
"Three letters": { | ||
input: "abc", | ||
expected: "abc", | ||
}, | ||
"Two letters": { | ||
input: "ab", | ||
expected: "ab", | ||
}, | ||
"Single letter": { | ||
input: "a", | ||
expected: "a", | ||
}, | ||
"": { | ||
input: "ππππππ", | ||
expected: "π4π", | ||
}, | ||
"Empty": { | ||
input: "", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for name, c := range cases { | ||
got, expected := generateNumeronym(c.input), c.expected | ||
t.Run(name, func(t *testing.T) { | ||
if got != expected { | ||
t.Errorf("\nExpected: %s\nGot: %s", expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNumeronyms(t *testing.T) { | ||
test := numeronyms("lowercase", "Uppercase") | ||
expected := []string{"l7e", "U7e"} | ||
|
||
for i, got := range test { | ||
// Lint error: Using the variable on range scope `got` / `i` in function literal | ||
i := i | ||
got := got | ||
t.Run(fmt.Sprintf("String of index %d", i), func(t *testing.T) { | ||
if got != expected[i] { | ||
t.Errorf("\nExpected: %s\nGot: %s", expected[i], got) | ||
} | ||
}) | ||
} | ||
} |