版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

224 líneas
5.7 KiB

  1. const $ = require('../index');
  2. const config = require('./config');
  3. const jc1 = require('./code/simple1');
  4. const jc2 = require('./code/simple2');
  5. const hc1 = require('./code/simple1.html');
  6. test('$.before: empty string should throw error', () => {
  7. expect(() => {
  8. const G = $('var a = 1;');
  9. G.before('');
  10. }).toThrow();
  11. })
  12. test('$.before: null should throw error', () => {
  13. expect(() => {
  14. const G = $('var a = 1;');
  15. G.before(null);
  16. }).toThrow();
  17. })
  18. test('$.before: undefined should throw error', () => {
  19. expect(() => {
  20. const G = $('var a = 1;');
  21. G.before(undefined);
  22. }).toThrow();
  23. })
  24. test('$.before: simple code', () => {
  25. expect(() => {
  26. const code = `
  27. function a(){
  28. var a = 1;
  29. }
  30. `;
  31. const node = $('var a = 1;').node;
  32. $(code).before(node);
  33. }).not.toThrow();
  34. })
  35. test('$.before: insert string code', () => {
  36. expect(() => {
  37. const code = `
  38. function a(){
  39. var a = 1;
  40. }
  41. `;
  42. $(code).before('var a = 1;');
  43. }).not.toThrow();
  44. })
  45. test('$.before: insert string code ,result to equal', () => {
  46. const code = `
  47. function a(){
  48. var a = 1;
  49. }
  50. `;
  51. const newCode = $(code).before('var a = 1;').generate();
  52. const compareCode = `
  53. var a = 1;
  54. function a(){
  55. var a = 1;
  56. }
  57. `;
  58. expect(newCode).toBe(compareCode);
  59. })
  60. test('$.before: insert node object', () => {
  61. expect(() => {
  62. const code = `
  63. function a(){
  64. var a = 1;
  65. }
  66. `;
  67. $(code).before($('var a = 1;').node);
  68. }).not.toThrow();
  69. })
  70. test('$.before: insert node object,result to equal', () => {
  71. const code = `
  72. function a(){
  73. var a = 1;
  74. }
  75. `;
  76. const newCode = $(code).before($('var a = 1;').node).generate();
  77. const compareCode = `
  78. var a = 1;
  79. function a(){
  80. var a = 1;
  81. }
  82. `;
  83. expect(newCode).toBe(compareCode);
  84. })
  85. test('$.before: insert $ object', () => {
  86. expect(() => {
  87. const code = `
  88. function a(){
  89. var a = 1;
  90. }
  91. `;
  92. $(code).before($('var a = 1;'));
  93. }).not.toThrow();
  94. })
  95. test('$.before: insert node object,result to equal', () => {
  96. const code = `
  97. function a(){
  98. var a = 1;
  99. }
  100. `;
  101. const newCode = $(code).before($('var a = 1;').node).generate();
  102. const compareCode = `
  103. var a = 1;
  104. function a(){
  105. var a = 1;
  106. }
  107. `;
  108. expect(newCode).toBe(compareCode);
  109. })
  110. test('$.before: undefined html code', () => {
  111. const code = `<div>test</div>`;
  112. expect(() => {
  113. const G = $(code, config.html);
  114. G.before(undefined);
  115. }).toThrow();
  116. })
  117. test('$.before: empty simple html code', () => {
  118. const code = `<div>test</div>`;
  119. expect(() => {
  120. const G = $(code, config.html);
  121. G.before('');
  122. }).toThrow();
  123. })
  124. test('$.before: simple html code', () => {
  125. const code = `<div>test</div>`;
  126. expect(() => {
  127. const G = $(code, config.html);
  128. G.before('<span>span</span>');
  129. }).not.toThrow();
  130. })
  131. test('$.before: simple html code,result to equal', () => {
  132. const code = `<div>test</div>`;
  133. const G = $(code, config.html);
  134. const newCode = G.before('<span>span</span>').generate();
  135. const compareCode = `<span>span</span><div>test</div>`;
  136. expect(newCode).toBe(compareCode);
  137. })
  138. test('$.before: insert node', () => {
  139. const code = `<div>test</div>`;
  140. expect(() => {
  141. const G = $(code, config.html);
  142. G.before($('<span>span</span>', config.html).node);
  143. }).not.toThrow();
  144. })
  145. test('$.before: insert node,result to equal', () => {
  146. const code = `<div>test</div>`;
  147. const G = $(code, config.html);
  148. const newCode = G.before($('<span>span</span>', config.html).node).generate();
  149. const compareCode = `<span>span</span><div>test</div>`;
  150. expect(newCode).toBe(compareCode);
  151. })
  152. test('$.before: insert $ object', () => {
  153. const code = `<div>test</div>`;
  154. expect(() => {
  155. const G = $(code, config.html);
  156. G.before($('<span>span</span>', config.html));
  157. }).not.toThrow();
  158. })
  159. test('$.before: insert $ object,result to equal', () => {
  160. const code = `<div>test</div>`;
  161. const G = $(code, config.html);
  162. const newCode = G.before($('<span>span</span>', config.html)).generate();
  163. const compareCode = `<span>span</span><div>test</div>`;
  164. expect(newCode).toBe(compareCode);
  165. })
  166. test('$.before: simple1 html code', () => {
  167. expect(() => {
  168. const G = $(hc1, config.html).after('<span>after</span>');
  169. }).not.toThrow();
  170. })
  171. test('$.before: simple1 html code', () => {
  172. const code = $(hc1, config.html)
  173. .find('<span>$_$</span>')
  174. .before('<span>before</span>')
  175. .parent()
  176. .generate();
  177. expect(code.indexOf('<span>before</span>') > -1).toBeTruthy();
  178. })
  179. test('$.before: insert decorator', () => {
  180. const res = $(`class A {
  181. }`)
  182. .find(`class $_$ {}`)
  183. .before(`@decorator(a, b)
  184. @decorator11(a, b)
  185. `)
  186. .root()
  187. .generate()
  188. expect(res.match('@decorator') && res.match('@decorator11')).toBeTruthy();
  189. })
  190. test('$.before: insert decorator', () => {
  191. const res = $(`class A {
  192. a = 1
  193. }`)
  194. .find(`a = 1`)
  195. .before(`@decorator(a, b)
  196. @decorator11(a, b)
  197. `)
  198. .root()
  199. .generate()
  200. expect(res.match('@decorator') && res.match('@decorator11')).toBeTruthy();
  201. })
  202. test('$.before: insert decorator', () => {
  203. let res = $(`that.fun('test')
  204. `)
  205. .find('that.fun($_$)').parent()
  206. res.before("\n//before\n")
  207. res.after("\n//after\n")
  208. res = res.root().generate()
  209. expect(res.match('after')).toBeTruthy();
  210. })