DictionarylIdentifierNamesGenerator.spec.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  4. import { IIdentifierNamesGenerator } from '../../../../src/interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  5. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  7. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  8. describe('DictionaryIdentifierNamesGenerator', () => {
  9. describe('generate', () => {
  10. let identifierNamesGenerator: IIdentifierNamesGenerator,
  11. dictionaryIdentifierName: string;
  12. describe('Base behaviour', () => {
  13. before(() => {
  14. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  15. inversifyContainerFacade.load('', '', {
  16. identifiersDictionary: ['a', 'b']
  17. });
  18. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  19. ServiceIdentifiers.IIdentifierNamesGenerator,
  20. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  21. );
  22. });
  23. describe('Variant #1: first dictionary iteration', () => {
  24. const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|b]/;
  25. beforeEach(() => {
  26. dictionaryIdentifierName = identifierNamesGenerator.generate();
  27. });
  28. it('Match #1: should return first identifier name', () => {
  29. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  30. });
  31. it('Match #2: should return second identifier name', () => {
  32. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  33. });
  34. });
  35. describe('Variant #2: second dictionary iteration', () => {
  36. const expectedDictionaryIdentifierNameRegExp: RegExp = /[A|B]/;
  37. beforeEach(() => {
  38. dictionaryIdentifierName = identifierNamesGenerator.generate();
  39. });
  40. it('Match #1: should return third identifier name', () => {
  41. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  42. });
  43. it('Match #2: should return fourth identifier name', () => {
  44. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  45. });
  46. });
  47. });
  48. describe('Empty string as dictionary value', () => {
  49. before(() => {
  50. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  51. inversifyContainerFacade.load('', '', {
  52. identifiersDictionary: ['', 'a']
  53. });
  54. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  55. ServiceIdentifiers.IIdentifierNamesGenerator,
  56. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  57. );
  58. });
  59. describe('Variant #1: Should ignore empty strings from dictionary', () => {
  60. const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|A]/;
  61. beforeEach(() => {
  62. dictionaryIdentifierName = identifierNamesGenerator.generate();
  63. });
  64. it('Match #1: should return first identifier name', () => {
  65. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  66. });
  67. it('Match #2: should return second identifier name', () => {
  68. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  69. });
  70. });
  71. });
  72. describe('Multi-character string as dictionary value', () => {
  73. before(() => {
  74. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  75. inversifyContainerFacade.load('', '', {
  76. identifiersDictionary: ['aa']
  77. });
  78. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  79. ServiceIdentifiers.IIdentifierNamesGenerator,
  80. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  81. );
  82. });
  83. describe('Variant #1: Should generate identifier names based on all variants of word from dictionary', () => {
  84. const expectedFirstIterationIdentifierName: string = 'aa';
  85. const expectedSecondIterationIdentifierName: string = 'Aa';
  86. const expectedThirdIterationIdentifierName: string = 'aA';
  87. const expectedFourthIterationIdentifierName: string = 'AA';
  88. beforeEach(() => {
  89. dictionaryIdentifierName = identifierNamesGenerator.generate();
  90. });
  91. it('Match #1: should return first identifier name', () => {
  92. assert.equal(dictionaryIdentifierName, expectedFirstIterationIdentifierName);
  93. });
  94. it('Match #2: should return second identifier name', () => {
  95. assert.equal(dictionaryIdentifierName, expectedSecondIterationIdentifierName);
  96. });
  97. it('Match #3: should return third identifier name', () => {
  98. assert.equal(dictionaryIdentifierName, expectedThirdIterationIdentifierName);
  99. });
  100. it('Match #4: should return fourth identifier name', () => {
  101. assert.equal(dictionaryIdentifierName, expectedFourthIterationIdentifierName);
  102. });
  103. });
  104. });
  105. describe('Generate with prefix', () => {
  106. before(() => {
  107. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  108. inversifyContainerFacade.load('', '', {
  109. identifiersDictionary: ['a'],
  110. identifiersPrefix: 'foo'
  111. });
  112. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  113. ServiceIdentifiers.IIdentifierNamesGenerator,
  114. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  115. );
  116. });
  117. describe('Variant #1: Should generate identifier names with prefix', () => {
  118. const expectedDictionaryIdentifierNameRegExp: RegExp = /foo[a|A]/;
  119. beforeEach(() => {
  120. dictionaryIdentifierName = identifierNamesGenerator.generateWithPrefix();
  121. });
  122. it('Match #1: should return first identifier name', () => {
  123. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  124. });
  125. it('Match #2: should return second identifier name', () => {
  126. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  127. });
  128. });
  129. });
  130. describe('Errors', () => {
  131. let inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  132. beforeEach(() => {
  133. inversifyContainerFacade = new InversifyContainerFacade();
  134. });
  135. describe('Variant #1: No more identifier variants for generation', () => {
  136. const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|A]/;
  137. let testFunc: () => string;
  138. before(() => {
  139. inversifyContainerFacade.load('', '', {
  140. identifiersDictionary: ['a']
  141. });
  142. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  143. ServiceIdentifiers.IIdentifierNamesGenerator,
  144. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  145. );
  146. testFunc = () => identifierNamesGenerator.generate();
  147. });
  148. it('Match #1: should return first identifier name', () => {
  149. dictionaryIdentifierName = testFunc();
  150. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  151. });
  152. it('Match #2: should return second identifier name', () => {
  153. dictionaryIdentifierName = testFunc();
  154. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  155. });
  156. it('Should throw an error when all identifier variants are used', () => {
  157. assert.throws(testFunc, Error);
  158. });
  159. });
  160. describe('Variant #2: Empty identifiers dictionary', () => {
  161. let testFunc: () => string;
  162. before(() => {
  163. inversifyContainerFacade.load('', '', {
  164. identifiersDictionary: []
  165. });
  166. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  167. ServiceIdentifiers.IIdentifierNamesGenerator,
  168. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  169. );
  170. testFunc = () => identifierNamesGenerator.generate();
  171. });
  172. it('Should throw an error when identifiers dictionary is empty', () => {
  173. assert.throws(testFunc, Error);
  174. });
  175. });
  176. });
  177. });
  178. });