Browse Source

Proper type

Piotr Czajkowski 3 years ago
parent
commit
7f1067c642
3 changed files with 22 additions and 8 deletions
  1. 2 2
      app.go
  2. 6 6
      app_test.go
  3. 14 0
      encodedText.go

+ 2 - 2
app.go

@@ -27,7 +27,7 @@ func encodeWord(word []rune, wordLength int) {
 }
 
 //EncodeText returns encoded provided text and sorted array of encoded words
-func EncodeText(text string) (string, []string) {
+func EncodeText(text string) EncodedText {
 	runes := []rune(text)
 	var currentWord []rune
 	var newString []rune
@@ -58,7 +58,7 @@ func EncodeText(text string) (string, []string) {
 	}
 
 	sort.Strings(encodedWords)
-	return string(newString), encodedWords
+	return EncodedText{string(newString), encodedWords}
 }
 
 func removeString(array []string, index int) []string {

+ 6 - 6
app_test.go

@@ -16,17 +16,17 @@ func TestDecodeEncode(t *testing.T) {
 	}
 
 	for _, item := range testCases {
-		encodedText, encodedWords := EncodeText(item.text)
-		if encodedText == item.text {
-			t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encodedText, item.text)
+		encoded := EncodeText(item.text)
+		if encoded.text == item.text {
+			t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encoded.text, item.text)
 		}
 
-		count := len(encodedWords)
+		count := len(encoded.encodedWords)
 		if count != item.count {
-			t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encodedWords)
+			t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encoded.encodedWords)
 		}
 
-		decodedText := DecodeText(encodedText, encodedWords)
+		decodedText := DecodeText(encoded.text, encoded.encodedWords)
 		if decodedText != item.text {
 			t.Errorf("Decoded text '%s' should be same as provided text '%s'!", decodedText, item.text)
 		}

+ 14 - 0
encodedText.go

@@ -0,0 +1,14 @@
+package main
+
+import (
+	"fmt"
+)
+
+type EncodedText struct {
+	text         string
+	encodedWords []string
+}
+
+func (e EncodedText) String() string {
+	return fmt.Sprintf("\n---weird---\n%s\n---weird---\n%v", e.text, e.encodedWords)
+}