DictionarylIdentifierNamesGenerator.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. let identifierNamesGenerator: IIdentifierNamesGenerator,
  10. dictionaryIdentifierName: string;
  11. describe('generateNext', () => {
  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.generateNext();
  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.generateNext();
  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.generateNext();
  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.generateNext();
  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('Errors', () => {
  106. let inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  107. beforeEach(() => {
  108. inversifyContainerFacade = new InversifyContainerFacade();
  109. });
  110. describe('Variant #1: No more identifier variants for generation', () => {
  111. const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|A]/;
  112. let testFunc: () => string;
  113. before(() => {
  114. inversifyContainerFacade.load('', '', {
  115. identifiersDictionary: ['a']
  116. });
  117. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  118. ServiceIdentifiers.IIdentifierNamesGenerator,
  119. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  120. );
  121. testFunc = () => identifierNamesGenerator.generateNext();
  122. });
  123. it('Match #1: should return first identifier name', () => {
  124. dictionaryIdentifierName = testFunc();
  125. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  126. });
  127. it('Match #2: should return second identifier name', () => {
  128. dictionaryIdentifierName = testFunc();
  129. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  130. });
  131. it('Should throw an error when all identifier variants are used', () => {
  132. assert.throws(testFunc, Error);
  133. });
  134. });
  135. describe('Variant #2: Empty identifiers dictionary', () => {
  136. let testFunc: () => string;
  137. before(() => {
  138. inversifyContainerFacade.load('', '', {
  139. identifiersDictionary: []
  140. });
  141. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  142. ServiceIdentifiers.IIdentifierNamesGenerator,
  143. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  144. );
  145. testFunc = () => identifierNamesGenerator.generateNext();
  146. });
  147. it('Should throw an error when identifiers dictionary is empty', () => {
  148. assert.throws(testFunc, Error);
  149. });
  150. });
  151. });
  152. });
  153. describe('generateForGlobalScope', () => {
  154. before(() => {
  155. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  156. inversifyContainerFacade.load('', '', {
  157. identifiersDictionary: ['a'],
  158. identifiersPrefix: 'foo'
  159. });
  160. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  161. ServiceIdentifiers.IIdentifierNamesGenerator,
  162. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  163. );
  164. });
  165. describe('Variant #1: Should generate identifier names with prefix', () => {
  166. const expectedDictionaryIdentifierNameRegExp: RegExp = /foo[a|A]/;
  167. beforeEach(() => {
  168. dictionaryIdentifierName = identifierNamesGenerator.generateForGlobalScope();
  169. });
  170. it('Match #1: should return first identifier name', () => {
  171. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  172. });
  173. it('Match #2: should return second identifier name', () => {
  174. assert.match(dictionaryIdentifierName, expectedDictionaryIdentifierNameRegExp);
  175. });
  176. });
  177. });
  178. describe('generateForLabel', () => {
  179. const label1: string = 'label1';
  180. const label2: string = 'label2';
  181. const dictionaryNames1: string[] = [];
  182. const dictionaryNames2: string[] = [];
  183. let identifierNamesGenerator: IIdentifierNamesGenerator;
  184. before(() => {
  185. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  186. inversifyContainerFacade.load('', '', {
  187. identifiersPrefix: 'foo',
  188. identifiersDictionary: ['a', 'b', 'c']
  189. });
  190. identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
  191. ServiceIdentifiers.IIdentifierNamesGenerator,
  192. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  193. );
  194. dictionaryNames1.push(identifierNamesGenerator.generateForLabel(label1));
  195. dictionaryNames1.push(identifierNamesGenerator.generateForLabel(label1));
  196. dictionaryNames1.push(identifierNamesGenerator.generateForLabel(label1));
  197. dictionaryNames2.push(identifierNamesGenerator.generateForLabel(label2));
  198. dictionaryNames2.push(identifierNamesGenerator.generateForLabel(label2));
  199. dictionaryNames2.push(identifierNamesGenerator.generateForLabel(label2));
  200. });
  201. it('should return different dictionary names for different labels', () => {
  202. assert.notDeepEqual(dictionaryNames1, dictionaryNames2);
  203. });
  204. });
  205. });