版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

168 строки
5.0 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('$.replaceBy: simple code', () => {
  7. expect(()=>{
  8. const G = $('var a = 1;');
  9. G.replaceBy('function test(){}')
  10. }).not.toThrow();
  11. })
  12. test('$.replaceBy: this[0] is null', () => {
  13. expect(()=>{
  14. const G = $('var a = 1;');
  15. G[0] = null
  16. G.replaceBy();
  17. }).not.toThrow();
  18. })
  19. test('$.replaceBy: simple code', () => {
  20. expect(()=>{
  21. const G = $('var a = 1;');
  22. G.replaceBy($('function test(){}'))
  23. }).not.toThrow();
  24. })
  25. test('$.replaceBy: simple code replaceBy function result should be ok', () => {
  26. const G = $('var a = 1;');
  27. const result = G.replaceBy('function test(){}');
  28. const code = result.generate();
  29. expect(code).toBe('function test(){}');
  30. })
  31. test('$.replaceBy: simple code', () => {
  32. expect(()=>{
  33. const G = $('var a = 1;');
  34. G.replaceBy('var b = 1;');
  35. }).not.toThrow();
  36. })
  37. test('$.replaceBy: simple code result should be ok', () => {
  38. const G = $('var a = 1;');
  39. const result = G.replaceBy('var b = 1;');
  40. const code = result.generate();
  41. expect(code).toBe('var b = 1;');
  42. })
  43. test('$.replaceBy: simple1 code result should be ok', () => {
  44. const G = $(jc1);
  45. const result = G.replaceBy('var b = 1;');
  46. const code = result.generate();
  47. expect(code).toBe('\nvar b = 1;\n');
  48. })
  49. test('$.replaceBy: simple2 code result should be ok', () => {
  50. const G = $(jc2);
  51. //整个ast替换
  52. const result = G.replaceBy('var b = 1;');
  53. const code = result.generate();
  54. expect(code).toBe('\nvar b = 1;\n');
  55. })
  56. test('$.replaceBy: simple2 code2 result should be ok', () => {
  57. const G = $(jc2).find('this.updater.digest(loc)');
  58. const result = G.replaceBy('this.updater.set(loc)');
  59. const code = result.generate();
  60. expect(code.indexOf('this.updater.set(loc)') > -1).toBeTruthy();
  61. })
  62. test('$.replaceBy: replace use $_$ result should be ok', () => {
  63. const CODE = `alert({
  64. type: 'error',
  65. content: '请填写必填项',
  66. done: () => {}
  67. })`;
  68. const code = $(CODE)
  69. .find(`alert({ type: $_$1, content: $_$2, done: $_$3 })`)
  70. .each(item => {
  71. const typeValue = item.match[1][0].value,
  72. contentValue = item.match[2][0].value,
  73. doneValue = item.match[3][0].value
  74. item.replaceBy($(`
  75. alert( ${typeValue}, ${contentValue}, ${doneValue} )
  76. `))
  77. }).generate();
  78. const result = code.indexOf(`alert( error, 请填写必填项, () => {} )`) > -1 ;
  79. expect(result).toBeTruthy();
  80. })
  81. test('$.replaceBy: simple1 html code', () => {
  82. expect(() => {
  83. const G = $(hc1, config.html);
  84. G.replaceBy('<span>test</span>');
  85. }).not.toThrow();
  86. })
  87. test('$.replaceBy: simple1 html code result should be ok', () => {
  88. const G = $(hc1, config.html);
  89. G.replaceBy('<span>test</span>');
  90. const code = G.generate();
  91. expect(code).toBe('<span>test</span>');
  92. })
  93. test('$.replaceBy: simple1 html code use find result should be ok', () => {
  94. const G = $(hc1, config.html);
  95. const result = G.find('<head>$_$</head>').replaceBy('<span>test</span>');
  96. const code = result.root().generate();
  97. expect(code.indexOf('<head>') < 0).toBeTruthy();
  98. })
  99. test('$.replaceBy: simple1 html code use find result should be ok', () => {
  100. const res = $(`
  101. export default {
  102. a: 1,
  103. b: 2,
  104. render() {}
  105. }`).find(`render() {
  106. }`)
  107. .parent(1)
  108. .replaceBy(`function render() {}`)
  109. .root()
  110. .generate();
  111. expect(res.indexOf(`function render`) > -1).toBeTruthy();
  112. })
  113. test('$.replaceBy: simple1 html code use find result should be ok', () => {
  114. const res = $(`
  115. export default {
  116. name: 'HComp',
  117. props: {
  118. msg: String,
  119. },
  120. functional: true,
  121. render(h, { props }) {
  122. return h('p', \`render by h: \${props.msg}\`);
  123. },
  124. };`)
  125. .find('{ functional: true }')
  126. .each((ast) => {
  127. if (ast.has('render() {}')) {
  128. const renderFunction = ast.find('render() {}');
  129. let renderFunctionStr = `function ${renderFunction.generate()}`;
  130. renderFunction.parent(1).replaceBy(renderFunctionStr);
  131. }
  132. })
  133. .root()
  134. .generate();
  135. expect(!!res.match('function render')).toBeTruthy();
  136. })
  137. test('$.replaceBy: replace use $_$ result should be ok', () => {
  138. const CODE = `
  139. <view>打酱油</view>
  140. <view>不会被替换的节点</view>
  141. `;
  142. const res =
  143. $(CODE, { parseOptions: { language: 'html'}})
  144. .find(`<view>不会被替换的节点</view>`)
  145. .each((item) => {
  146. item.before(`<!-- 我是注释我是注释 -->
  147. `)
  148. //不加这行,上面的注释可以加上去
  149. item.replaceBy('<view>我是新来的</view>')
  150. })
  151. .root()
  152. .generate()
  153. expect(res.match('我是注释我是注释') && res.match('我是新来的')).toBeTruthy();
  154. })