app_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  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. }
  14. for _, item := range testCases {
  15. encoded := EncodeText(item.text)
  16. if encoded.Text == item.text {
  17. t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encoded.Text, item.text)
  18. }
  19. count := len(encoded.EncodedWords)
  20. if count != item.count {
  21. t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encoded.EncodedWords)
  22. }
  23. decodedText := DecodeText(encoded)
  24. if decodedText != item.text {
  25. t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, item.text)
  26. }
  27. }
  28. }
  29. func TestSerializeEncodedText(t *testing.T) {
  30. expected := `
  31. ---weird---
  32. Tihs is a lnog loonog tset sneetcne,
  33. wtih smoe big (biiiiig) wodrs!
  34. ---weird---
  35. This long looong sentence some test with words`
  36. encoded := EncodeText("This is a long looong test sentence,\nwith some big (biiiiig) words!")
  37. if encoded.String() != expected {
  38. t.Errorf("Serialization error!\nShould be:%s\nIs:%s", expected, encoded.String())
  39. }
  40. }
  41. func TestDeserializeEncodedText(t *testing.T) {
  42. serialized := `
  43. ---weird---
  44. Tihs is a lnog loonog tset sneetcne,
  45. wtih smoe big (biiiiig) wodrs!
  46. ---weird---
  47. This long looong sentence some test with words`
  48. expected := "This is a long looong test sentence,\nwith some big (biiiiig) words!"
  49. encoded := EncodedText{}
  50. err := encoded.FromString(serialized)
  51. if err != nil {
  52. t.Errorf("Error deserializing encoded text: %s", err)
  53. }
  54. decodedText := DecodeText(encoded)
  55. if decodedText != expected {
  56. t.Errorf("Decoded text '%s' should be same as expected text '%s'!", decodedText, expected)
  57. }
  58. }