|
| 1 | +const { JSGenerate } = require('../generate'); |
| 2 | +global.last = jest.fn((array) => array[array.length - 1]); |
| 3 | +const globalActivity = { |
| 4 | + blocks: { |
| 5 | + stackList: [], |
| 6 | + blockList: {}, |
| 7 | + findStacks: jest.fn(), |
| 8 | + }, |
| 9 | +}; |
| 10 | +global.globalActivity = globalActivity; |
| 11 | +global.console = { |
| 12 | + log: jest.fn(), |
| 13 | + error: jest.fn(), |
| 14 | + warn: jest.fn(), |
| 15 | +}; |
| 16 | +const ASTUtils = { |
| 17 | + BAREBONE_AST: { type: 'Program', body: [] }, |
| 18 | + getMethodAST: jest.fn(), |
| 19 | + getMouseAST: jest.fn(), |
| 20 | +}; |
| 21 | +const astring = { |
| 22 | + generate: jest.fn(), |
| 23 | +}; |
| 24 | + |
| 25 | +global.ASTUtils = ASTUtils; |
| 26 | +global.astring = astring; |
| 27 | + |
| 28 | +describe('JSGenerate Class', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + JSGenerate.startBlocks = []; |
| 32 | + JSGenerate.actionBlocks = []; |
| 33 | + JSGenerate.startTrees = []; |
| 34 | + JSGenerate.actionTrees = []; |
| 35 | + JSGenerate.actionNames = []; |
| 36 | + JSGenerate.AST = { type: 'Program', body: [] }; |
| 37 | + JSGenerate.code = ""; |
| 38 | + JSGenerate.generateFailed = false; |
| 39 | + }); |
| 40 | + |
| 41 | + test('should generate correct AST structure', () => { |
| 42 | + JSGenerate.actionTrees = [[['action', null, null]]]; |
| 43 | + JSGenerate.actionNames = ['action1']; |
| 44 | + JSGenerate.startTrees = [[['start', null, null]]]; |
| 45 | + |
| 46 | + const expectedAST = { |
| 47 | + type: 'Program', |
| 48 | + body: [ |
| 49 | + { type: 'Method' }, |
| 50 | + { type: 'Mouse' }, |
| 51 | + ], |
| 52 | + }; |
| 53 | + |
| 54 | + ASTUtils.getMethodAST.mockReturnValue({ type: 'Method' }); |
| 55 | + ASTUtils.getMouseAST.mockReturnValue({ type: 'Mouse' }); |
| 56 | + astring.generate.mockReturnValue('generated code'); |
| 57 | + |
| 58 | + JSGenerate.generateCode(); |
| 59 | + |
| 60 | + expect(JSGenerate.AST).toEqual(expectedAST); |
| 61 | + expect(JSGenerate.code).toBe('generated code'); |
| 62 | + expect(JSGenerate.generateFailed).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should handle code generation failure', () => { |
| 66 | + JSGenerate.actionTrees = [[['action', null, null]]]; |
| 67 | + JSGenerate.actionNames = ['action1']; |
| 68 | + JSGenerate.startTrees = [[['start', null, null]]]; |
| 69 | + |
| 70 | + ASTUtils.getMethodAST.mockImplementation(() => { |
| 71 | + throw new Error('Failed to generate AST'); |
| 72 | + }); |
| 73 | + |
| 74 | + JSGenerate.generateCode(); |
| 75 | + |
| 76 | + expect(JSGenerate.generateFailed).toBe(true); |
| 77 | + expect(console.error).toHaveBeenCalledWith( |
| 78 | + "CANNOT GENERATE ABSTRACT SYNTAX TREE\nError:", |
| 79 | + expect.any(Error) |
| 80 | + ); |
| 81 | + expect(JSGenerate.code).toBe(astring.generate(ASTUtils.BAREBONE_AST)); |
| 82 | + }); |
| 83 | + |
| 84 | + test('should print stacks tree', () => { |
| 85 | + JSGenerate.startTrees = [[['start', null, null]]]; |
| 86 | + JSGenerate.actionTrees = [[['action', null, null]]]; |
| 87 | + JSGenerate.actionNames = ['action1']; |
| 88 | + |
| 89 | + JSGenerate.printStacksTree(); |
| 90 | + |
| 91 | + expect(console.log).toHaveBeenCalledWith( |
| 92 | + "\n %c START ", |
| 93 | + "background: navy; color: white; font-weight: bold" |
| 94 | + ); |
| 95 | + expect(console.log).toHaveBeenCalledWith( |
| 96 | + "\n %c ACTION ", |
| 97 | + "background: green; color: white; font-weight: bold" |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should handle empty start and action trees', () => { |
| 102 | + JSGenerate.startTrees = []; |
| 103 | + JSGenerate.actionTrees = []; |
| 104 | + |
| 105 | + JSGenerate.printStacksTree(); |
| 106 | + |
| 107 | + expect(console.log).toHaveBeenCalledWith("%cno start trees generated", "color: tomato"); |
| 108 | + expect(console.log).toHaveBeenCalledWith("%cno action trees generated", "color: tomato"); |
| 109 | + }); |
| 110 | + |
| 111 | + test('should handle invalid action name', () => { |
| 112 | + globalActivity.blocks.stackList = [1]; |
| 113 | + globalActivity.blocks.blockList = { |
| 114 | + 1: { name: 'action', trash: false, connections: [null, 2, 3] }, |
| 115 | + 2: { name: 'namedbox', value: null, connections: [null] }, |
| 116 | + 3: { name: 'value', value: 'arg1', connections: [null] }, |
| 117 | + }; |
| 118 | + |
| 119 | + JSGenerate.generateStacksTree(); |
| 120 | + |
| 121 | + expect(JSGenerate.actionNames).toEqual([]); |
| 122 | + }); |
| 123 | + |
| 124 | + test('should handle invalid block connections', () => { |
| 125 | + globalActivity.blocks.stackList = [1]; |
| 126 | + globalActivity.blocks.blockList = { |
| 127 | + 1: { name: 'start', trash: false, connections: [] }, |
| 128 | + }; |
| 129 | + |
| 130 | + JSGenerate.generateStacksTree(); |
| 131 | + |
| 132 | + expect(JSGenerate.startTrees).toEqual([[]]); |
| 133 | + }); |
| 134 | + |
| 135 | + test('should run code generator with print options', () => { |
| 136 | + JSGenerate.actionTrees = [[['action', null, null]]]; |
| 137 | + JSGenerate.actionNames = ['action1']; |
| 138 | + JSGenerate.startTrees = [[['start', null, null]]]; |
| 139 | + |
| 140 | + ASTUtils.getMethodAST.mockReturnValue({ type: 'Method' }); |
| 141 | + ASTUtils.getMouseAST.mockReturnValue({ type: 'Mouse' }); |
| 142 | + astring.generate.mockReturnValue('generated code'); |
| 143 | + |
| 144 | + JSGenerate.run(true, true); |
| 145 | + |
| 146 | + expect(console.log).toHaveBeenCalledWith( |
| 147 | + "\n %c STACK TREES ", |
| 148 | + "background: greenyellow; color: midnightblue; font-weight: bold" |
| 149 | + ); |
| 150 | + expect(console.log).toHaveBeenCalledWith( |
| 151 | + "\n %c CODE ", |
| 152 | + "background: greenyellow; color: midnightblue; font-weight: bold" |
| 153 | + ); |
| 154 | + expect(console.log).toHaveBeenCalledWith('generated code'); |
| 155 | + }); |
| 156 | +}); |
0 commit comments