Browse Source

Using same object for decode while not destroying EncodedWords

Piotr Czajkowski 3 years ago
parent
commit
208725c30e
2 changed files with 7 additions and 4 deletions
  1. 5 2
      app.go
  2. 2 2
      app_test.go

+ 5 - 2
app.go

@@ -105,8 +105,11 @@ func decodeWord(word []rune, wordLength int, encodedWords []string) (string, []s
 }
 
 //DecodeText returns decoded provided text using provided array of encoded words
-func DecodeText(text string, encodedWords []string) string {
-	runes := []rune(text)
+func DecodeText(encoded EncodedText) string {
+	runes := []rune(encoded.Text)
+	encodedWords := make([]string, len(encoded.EncodedWords))
+	copy(encodedWords, encoded.EncodedWords)
+
 	var currentWord []rune
 	var newString []rune
 

+ 2 - 2
app_test.go

@@ -26,7 +26,7 @@ func TestDecodeEncode(t *testing.T) {
 			t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encoded.EncodedWords)
 		}
 
-		decodedText := DecodeText(encoded.Text, encoded.EncodedWords)
+		decodedText := DecodeText(encoded)
 		if decodedText != item.text {
 			t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, item.text)
 		}
@@ -63,7 +63,7 @@ This long looong sentence some test with words`
 		t.Errorf("Error deserializing encoded text: %s", err)
 	}
 
-	decodedText := DecodeText(encoded.Text, encoded.EncodedWords)
+	decodedText := DecodeText(encoded)
 	if decodedText != expected {
 		t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, expected)
 	}