@@ -43,6 +43,7 @@ const astring = {
4343
4444global . ASTUtils = ASTUtils ;
4545global . astring = astring ;
46+ global . JSInterface = { isGetter : jest . fn ( ( ) => false ) } ;
4647
4748global . JSInterface = {
4849 isGetter : jest . fn ( name => name === "myGetter" )
@@ -173,6 +174,94 @@ describe("JSGenerate Class", () => {
173174 ) ;
174175 expect ( console . log ) . toHaveBeenCalledWith ( "generated code" ) ;
175176 } ) ;
177+
178+ test ( "should set generateFailed when astring.generate throws" , ( ) => {
179+ JSGenerate . actionTrees = [ ] ;
180+ JSGenerate . startTrees = [ [ [ "start" , null , null ] ] ] ;
181+
182+ ASTUtils . getMouseAST . mockReturnValue ( { type : "Mouse" } ) ;
183+ astring . generate . mockImplementationOnce ( ( ) => {
184+ throw new Error ( "Invalid AST" ) ;
185+ } ) ;
186+ astring . generate . mockReturnValueOnce ( "fallback code" ) ;
187+
188+ JSGenerate . generateCode ( ) ;
189+
190+ expect ( JSGenerate . generateFailed ) . toBe ( true ) ;
191+ expect ( console . error ) . toHaveBeenCalledWith (
192+ "CANNOT GENERATE CODE\nError: INVALID ABSTRACT SYNTAX TREE"
193+ ) ;
194+ expect ( JSGenerate . code ) . toBe ( "fallback code" ) ;
195+ } ) ;
196+
197+ test ( "should generate action tree for valid action block with connections" , ( ) => {
198+ globalActivity . blocks . stackList = [ 1 ] ;
199+ globalActivity . blocks . blockList = {
200+ 1 : { name : "action" , trash : false , connections : [ null , 2 , 3 , null ] } ,
201+ 2 : { name : "text" , value : "myAction" , connections : [ 1 ] } ,
202+ 3 : {
203+ name : "forward" ,
204+ connections : [ 1 , 4 , null ] ,
205+ protoblock : { args : 1 , style : "arg" }
206+ } ,
207+ 4 : {
208+ name : "number" ,
209+ value : 100 ,
210+ connections : [ 3 ] ,
211+ protoblock : {
212+ args : 0 ,
213+ style : "value" ,
214+ __proto__ : { __proto__ : { constructor : { name : "ValueBlock" } } }
215+ }
216+ }
217+ } ;
218+
219+ JSGenerate . generateStacksTree ( ) ;
220+
221+ expect ( JSGenerate . actionNames ) . toEqual ( [ "myAction" ] ) ;
222+ expect ( JSGenerate . actionTrees . length ) . toBe ( 1 ) ;
223+ } ) ;
224+
225+ test ( "should print tree with nested args including null and object" , ( ) => {
226+ JSGenerate . startTrees = [ [ [ "forward" , [ 100 , null , [ "add" , [ 3 , 4 ] ] ] , null ] ] ] ;
227+ JSGenerate . actionTrees = [ ] ;
228+
229+ JSGenerate . printStacksTree ( ) ;
230+
231+ expect ( console . log ) . toHaveBeenCalledWith (
232+ "\n %c START " ,
233+ "background: navy; color: white; font-weight: bold"
234+ ) ;
235+ } ) ;
236+
237+ test ( "should print tree with clamp sub-tree" , ( ) => {
238+ JSGenerate . startTrees = [ [ [ "repeat" , [ 3 ] , [ [ "forward" , [ 100 ] , null ] ] ] ] ] ;
239+ JSGenerate . actionTrees = [ ] ;
240+
241+ JSGenerate . printStacksTree ( ) ;
242+
243+ expect ( console . log ) . toHaveBeenCalledTimes ( 4 ) ;
244+ } ) ;
245+
246+ test ( "should run without printing separator when printCode is false" , ( ) => {
247+ globalActivity . blocks . stackList = [ ] ;
248+ globalActivity . blocks . blockList = { } ;
249+ ASTUtils . getMethodAST . mockReturnValue ( { type : "Method" } ) ;
250+ ASTUtils . getMouseAST . mockReturnValue ( { type : "Mouse" } ) ;
251+ astring . generate . mockReturnValue ( "code" ) ;
252+
253+ JSGenerate . run ( true , false ) ;
254+
255+ expect ( console . log ) . not . toHaveBeenCalledWith (
256+ "%c _______________________________________" ,
257+ "color: darkorange"
258+ ) ;
259+ expect ( console . log ) . not . toHaveBeenCalledWith (
260+ "\n %c CODE " ,
261+ "background: greenyellow; color: midnightblue; font-weight: bold"
262+ ) ;
263+ } ) ;
264+
176265 test ( "should generate stack trees with various block types and arguments" , ( ) => {
177266 globalActivity . blocks . stackList = [ 1 , 20 ] ;
178267 const booleanGrandParent = { constructor : { name : "BooleanBlock" } } ;
0 commit comments