Browse Source

Added test

Piotr Czajkowski 3 years ago
parent
commit
a89a27a75a
2 changed files with 28 additions and 12 deletions
  1. 4 12
      app.go
  2. 24 0
      app_test.go

+ 4 - 12
app.go

@@ -1,7 +1,6 @@
 package main
 
 import (
-	"fmt"
 	"math/rand"
 	"sort"
 	"unicode"
@@ -20,7 +19,8 @@ func encodeWord(word []rune, wordLength int) {
 	})
 }
 
-func encodeText(text []rune) (string, []string) {
+//EncodeText returns encoded provided text and sorted array of encoded words
+func EncodeText(text []rune) (string, []string) {
 	var currentWord []rune
 	var newString []rune
 	var encodedWords []string
@@ -96,7 +96,8 @@ func decodeWord(word []rune, wordLength int, encodedWords []string) (string, []s
 	return string(word), encodedWords
 }
 
-func decodeText(text []rune, encodedWords []string) string {
+//DecodeText returns decoded provided text using provided array of encoded words
+func DecodeText(text []rune, encodedWords []string) string {
 	var currentWord []rune
 	var newString []rune
 
@@ -123,12 +124,3 @@ func decodeText(text []rune, encodedWords []string) string {
 
 	return string(newString)
 }
-
-func main() {
-	test := `This is a long looong test sentence,
-with some big (biiiiig) words!`
-
-	encodedText, encodedWords := encodeText([]rune(test))
-	fmt.Println(encodedText, encodedWords)
-	fmt.Println(decodeText([]rune(encodedText), encodedWords))
-}

+ 24 - 0
app_test.go

@@ -0,0 +1,24 @@
+package main
+
+import (
+	"testing"
+)
+
+func TestDecodeEncode(t *testing.T) {
+	text := `This is a long looong test sentence,
+with some big (biiiiig) words!`
+
+	encodedText, encodedWords := EncodeText([]rune(text))
+	if encodedText == text {
+		t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encodedText, text)
+	}
+
+	if len(encodedWords) != 8 {
+		t.Errorf("There should be 8 encoded words! %v", encodedWords)
+	}
+
+	decodedText := DecodeText([]rune(encodedText), encodedWords)
+	if decodedText != text {
+		t.Errorf("Decoded text '%s' should be same as provided text '%s'!", decodedText, text)
+	}
+}