Browse Source

Added more checks

Piotr Czajkowski 3 years ago
parent
commit
3ec0f20c20
2 changed files with 28 additions and 6 deletions
  1. 8 0
      encodedText.go
  2. 20 6
      encodedText_test.go

+ 8 - 0
encodedText.go

@@ -28,7 +28,15 @@ func (e *EncodedText) FromString(serialized string) error {
 		return fmt.Errorf("Invalid string: %s", serialized)
 	}
 
+	if parts[1] == "" {
+		return fmt.Errorf("There's no encoded text: %s", serialized)
+	}
 	e.Text = parts[1]
+
+	if parts[2] == "" {
+		return fmt.Errorf("There are no encoded words: %s", serialized)
+	}
 	e.EncodedWords = strings.Split(parts[2], " ")
+
 	return nil
 }

+ 20 - 6
encodedText_test.go

@@ -20,15 +20,29 @@ This long looong sentence some test with words`
 }
 
 func TestInvalidString(t *testing.T) {
-	input := `
+	testCases := []string{
+		`
 ---weird---
 Tihs is a lnog lnooog tset setcnnee,
 wtih smoe big (biiiiig) wdros!
----weird---`
-	test := EncodedText{}
-	err := test.FromString(input)
-	if err == nil {
-		t.Errorf("There should be error as string is invalid!")
+---weird---`,
+		`
+---weird---
+---weird---
+This long looong sentence some test with words`,
+		`
+---weird---
+Tihs is a lnog lnooog tset setcnnee,
+wtih smoe big (biiiiig) wdros!
+---weird---
+`,
 	}
 
+	for _, input := range testCases {
+		test := EncodedText{}
+		err := test.FromString(input)
+		if err == nil {
+			t.Errorf("There should be error as string is invalid!")
+		}
+	}
 }