string.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. describe('StringService', function() {
  2. let $service;
  3. // Ignoring page reload request
  4. beforeAll(() => window.onbeforeunload = () => null);
  5. beforeEach(function() {
  6. module('3ema.services');
  7. // Inject the service
  8. inject(function(StringService) {
  9. $service = StringService;
  10. });
  11. });
  12. describe('getWord', function () {
  13. it('parse null string', () => {
  14. expect($service.getWord(null, 1)).toEqual(jasmine.objectContaining({
  15. word: null,
  16. realLength: 0
  17. }));
  18. });
  19. it('parse empty string', () => {
  20. expect($service.getWord('', 1)).toEqual(jasmine.objectContaining({
  21. word: null,
  22. realLength: 0
  23. }));
  24. });
  25. it('parse string (spaces)', () => {
  26. expect($service.getWord('When the man comes around.', 12)).toEqual(jasmine.objectContaining({
  27. word: 'man',
  28. realLength: 3
  29. }));
  30. expect($service.getWord('When the man comes around.', 13)).toEqual(jasmine.objectContaining({
  31. word: 'man',
  32. realLength: 4
  33. }));
  34. expect($service.getWord('When the man comes around.', 16)).toEqual(jasmine.objectContaining({
  35. word: 'man',
  36. realLength: 7
  37. }));
  38. });
  39. it('parse string (newline)', () => {
  40. expect($service.getWord("When\nthe\nman\ncomes\naround.", 12)).toEqual(jasmine.objectContaining({
  41. word: 'man',
  42. realLength: 3
  43. }));
  44. expect($service.getWord("When\nthe\nman\ncomes\naround.", 13)).toEqual(jasmine.objectContaining({
  45. word: 'man',
  46. realLength: 4
  47. }));
  48. expect($service.getWord("When\nthe\nman\n\n\n\n\n\n\n\ncomes\naround.", 16)).toEqual(jasmine.objectContaining({
  49. word: 'man',
  50. realLength: 7
  51. }));
  52. });
  53. it('parse string (newline/spaces)', () => {
  54. expect($service.getWord("When the\nman comes around.", 12)).toEqual(jasmine.objectContaining({
  55. word: 'man',
  56. realLength: 3
  57. }));
  58. expect($service.getWord("When the\nman \ncomes around.", 13)).toEqual(jasmine.objectContaining({
  59. word: 'man',
  60. realLength: 4
  61. }));
  62. expect($service.getWord("When the\nman \n \n \n \ncomes around.", 16)).toEqual(jasmine.objectContaining({
  63. word: 'man',
  64. realLength: 7
  65. }));
  66. });
  67. it('parse string (special character)', () => {
  68. expect($service.getWord('When the :man: comes around.', 15)).toEqual(jasmine.objectContaining({
  69. word: ':man:',
  70. realLength: 6
  71. }));
  72. expect($service.getWord('When the :man: comes around.', 14)).toEqual(jasmine.objectContaining({
  73. word: ':man:',
  74. realLength: 5
  75. }));
  76. });
  77. it('parse string (with emoji (2 chars))', () => {
  78. expect($service.getWord('this 😄 is a :smile: face', 19)).toEqual(jasmine.objectContaining({
  79. word: ':smile:',
  80. realLength: 7
  81. }));
  82. });
  83. it('parse string (additional separators)', () => {
  84. expect($service.getWord('When the spider:man: comes around.', 20, [':'])).toEqual(jasmine.objectContaining({
  85. word: ':man:',
  86. realLength: 5
  87. }));
  88. });
  89. });
  90. describe('byteChunkSplit', function() {
  91. this.testPatterns = (cases, size, offset) => {
  92. for (let testcase of cases) {
  93. const input = testcase[0];
  94. const expected = testcase[1];
  95. expect($service.byteChunk(input, size, offset)).toEqual(expected);
  96. }
  97. };
  98. it('short chunks', () => {
  99. this.testPatterns([
  100. ['abc',
  101. ['abc',]],
  102. ['abcdefghijklmn',
  103. ['abcdef', 'ghijkl', 'mn',]],
  104. // four byte emoji
  105. ['😅😅',
  106. ['😅', '😅']]
  107. ], 6, null);
  108. });
  109. it('chunks with offset', () => {
  110. this.testPatterns([
  111. ['The quick white 🐼. He jumped over the lazy 🐶.',
  112. ['The', 'quick', 'white', '🐼.', 'He', 'jumped', 'over', 'the', 'lazy', '🐶.',]],
  113. ], 6, 10);
  114. this.testPatterns([
  115. ['The quick white 🐼. He jumped over the lazy 🐶.',
  116. ['The quick white 🐼', '. He jumped over the', 'lazy 🐶.',]],
  117. ], 20, 10);
  118. });
  119. });
  120. });