app_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package weirdtext
  2. import (
  3. "testing"
  4. )
  5. type testCase struct {
  6. text string
  7. count int
  8. }
  9. func TestDecodeEncode(t *testing.T) {
  10. testCases := []testCase{
  11. {"This is a long looong test sentence,\nwith some big (biiiiig) words!", 8},
  12. {"Pre-translation generally means applying the TM(s) to one or more files as whole instead of moving segment by segment.", 11},
  13. {"Szła dzieweczka do laseczka\nDo zielonego, do zielonego, do zielonego.\nNapotkała myśliweczka\nBardzo szwarnego, bardzo szwarnego, bardzo szwarnego.", 14},
  14. }
  15. for _, item := range testCases {
  16. encoded := EncodeText(item.text)
  17. if encoded.Text == item.text {
  18. t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encoded.Text, item.text)
  19. }
  20. count := len(encoded.EncodedWords)
  21. if count != item.count {
  22. t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encoded.EncodedWords)
  23. }
  24. decodedText := DecodeText(encoded)
  25. if decodedText != item.text {
  26. t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, item.text)
  27. }
  28. }
  29. }
  30. func TestSerializeDeserializeEncodedText(t *testing.T) {
  31. expected := "This is a long looong test sentence,\nwith some big (biiiiig) words!"
  32. encoded := EncodeText(expected)
  33. serialized := encoded.String()
  34. toDecode := EncodedText{}
  35. err := toDecode.FromString(serialized)
  36. if err != nil {
  37. t.Errorf("Error deserializing encoded text: %s", err)
  38. }
  39. decodedText := DecodeText(encoded)
  40. if decodedText != expected {
  41. t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, expected)
  42. }
  43. }