1import { appendContentsInsideDeclarationBlock, findNewInstanceCodeBlock } from '../codeMod'; 2 3describe(findNewInstanceCodeBlock, () => { 4 it('should support classic new instance - java', () => { 5 const contents = 'final Foo instance = new Foo();'; 6 expect(findNewInstanceCodeBlock(contents, 'Foo', 'java')).toEqual({ 7 start: 21, 8 end: 29, 9 code: 'new Foo()', 10 }); 11 }); 12 13 it('should support classic new instance - kotlin', () => { 14 const contents = 'val instance = Foo()'; 15 expect(findNewInstanceCodeBlock(contents, 'Foo', 'kt')).toEqual({ 16 start: 15, 17 end: 19, 18 code: 'Foo()', 19 }); 20 }); 21 22 it('should support anonymous class new instance - java', () => { 23 const contents = [ 24 'final Runnable runnable = new Runnable() {', 25 ' @Override', 26 ' public void run() {', 27 ' Log.i(TAG, "runnable");', 28 ' }', 29 '};', 30 ].join('\n'); 31 expect(findNewInstanceCodeBlock(contents, 'Runnable', 'java')).toEqual({ 32 start: 26, 33 end: 109, 34 code: [ 35 'new Runnable() {', 36 ' @Override', 37 ' public void run() {', 38 ' Log.i(TAG, "runnable");', 39 ' }', 40 '}', 41 ].join('\n'), 42 }); 43 }); 44 45 it('should support anonymous class new instance - kotlin', () => { 46 const contents = [ 47 'val runnable = object : Runnable() {', 48 ' override fun run() {', 49 ' Log.i(TAG, "runnable")', 50 ' }', 51 '}', 52 ].join('\n'); 53 expect(findNewInstanceCodeBlock(contents, 'Runnable', 'kt')).toEqual({ 54 start: 15, 55 end: 91, 56 code: [ 57 'object : Runnable() {', 58 ' override fun run() {', 59 ' Log.i(TAG, "runnable")', 60 ' }', 61 '}', 62 ].join('\n'), 63 }); 64 }); 65 66 it('should return null if not found', () => { 67 const contents = 'final Foo instance = new Foo();'; 68 expect(findNewInstanceCodeBlock(contents, 'Bar', 'java')).toBe(null); 69 }); 70}); 71 72describe(appendContentsInsideDeclarationBlock, () => { 73 it('should support class declaration', () => { 74 const contents = ` 75public class App { 76 public static void main(String[] args) { 77 System.out.println("Hello App!"); 78 } 79}`; 80 81 const expectContents = ` 82public class App { 83 public static void main(String[] args) { 84 System.out.println("Hello App!"); 85 } 86 87 public void foo() { 88 System.out.println("Hello foo!"); 89 } 90}`; 91 92 expect( 93 appendContentsInsideDeclarationBlock( 94 contents, 95 'public class App', 96 ` 97 public void foo() { 98 System.out.println("Hello foo!"); 99 }\n` 100 ) 101 ).toEqual(expectContents); 102 }); 103 104 it('should support method declaration', () => { 105 const contents = ` 106public class App { 107 public static void main(String[] args) { 108 System.out.println("Hello App!"); 109 } 110}`; 111 112 const expectContents = ` 113public class App { 114 public static void main(String[] args) { 115 System.out.println("Hello App!"); 116 System.out.println("Hello from generated code."); 117 } 118}`; 119 120 expect( 121 appendContentsInsideDeclarationBlock( 122 contents, 123 'public static void main', 124 ' System.out.println("Hello from generated code.");\n ' 125 ) 126 ).toEqual(expectContents); 127 }); 128}); 129