@@ -899,4 +899,189 @@ describe('RubyToBlocksConverter/Looks', () => {
899899 } ) ;
900900 } ) ;
901901 } ) ;
902+
903+ describe ( 'costume existence check' , ( ) => {
904+ let targetWithCostumes ;
905+
906+ beforeEach ( ( ) => {
907+ // Mock target with costumes
908+ targetWithCostumes = {
909+ getCostumes : ( ) => [
910+ { name : 'costume1' } ,
911+ { name : 'costume2' }
912+ ]
913+ } ;
914+ } ) ;
915+
916+ describe ( 'switch_costume with costume existence check' , ( ) => {
917+ test ( 'existing costume should work' , ( ) => {
918+ code = 'switch_costume("costume1")' ;
919+ expected = [
920+ {
921+ opcode : 'looks_switchcostumeto' ,
922+ inputs : [
923+ {
924+ name : 'COSTUME' ,
925+ block : {
926+ opcode : 'looks_costume' ,
927+ fields : [
928+ {
929+ name : 'COSTUME' ,
930+ value : 'costume1'
931+ }
932+ ] ,
933+ shadow : true
934+ }
935+ }
936+ ]
937+ }
938+ ] ;
939+ convertAndExpectToEqualBlocks ( converter , targetWithCostumes , code , expected ) ;
940+ } ) ;
941+
942+ test ( 'non-existing costume should throw error' , ( ) => {
943+ code = 'switch_costume("NonExistentCostume")' ;
944+ const result = converter . targetCodeToBlocks ( targetWithCostumes , code ) ;
945+ expect ( result ) . toBeFalsy ( ) ;
946+ expect ( converter . errors ) . toHaveLength ( 1 ) ;
947+ expect ( converter . errors [ 0 ] . text ) . toContain ( 'costume "NonExistentCostume" does not exist' ) ;
948+ } ) ;
949+ } ) ;
950+
951+ describe ( 'costume_number and costume_name with costume existence check' , ( ) => {
952+ test ( 'costume_number should work without costume check' , ( ) => {
953+ code = 'costume_number' ;
954+ expected = [
955+ {
956+ opcode : 'looks_costumenumbername' ,
957+ fields : [
958+ {
959+ name : 'NUMBER_NAME' ,
960+ value : 'number'
961+ }
962+ ]
963+ }
964+ ] ;
965+ convertAndExpectToEqualBlocks ( converter , targetWithCostumes , code , expected ) ;
966+ } ) ;
967+
968+ test ( 'costume_name should work without costume check' , ( ) => {
969+ code = 'costume_name' ;
970+ expected = [
971+ {
972+ opcode : 'looks_costumenumbername' ,
973+ fields : [
974+ {
975+ name : 'NUMBER_NAME' ,
976+ value : 'name'
977+ }
978+ ]
979+ }
980+ ] ;
981+ convertAndExpectToEqualBlocks ( converter , targetWithCostumes , code , expected ) ;
982+ } ) ;
983+ } ) ;
984+ } ) ;
985+
986+ describe ( 'backdrop existence check' , ( ) => {
987+ let stageWithBackdrops ;
988+ let converterWithVM ;
989+
990+ beforeEach ( ( ) => {
991+ // Mock stage with backdrops
992+ stageWithBackdrops = {
993+ getCostumes : ( ) => [
994+ { name : 'backdrop1' } ,
995+ { name : 'backdrop2' }
996+ ]
997+ } ;
998+
999+ // Mock converter with VM runtime
1000+ converterWithVM = new RubyToBlocksConverter ( {
1001+ runtime : {
1002+ getTargetForStage : ( ) => stageWithBackdrops
1003+ }
1004+ } ) ;
1005+ } ) ;
1006+
1007+ [
1008+ {
1009+ opcode : 'looks_switchbackdropto' ,
1010+ methodName : 'switch_backdrop'
1011+ } ,
1012+ {
1013+ opcode : 'looks_switchbackdroptoandwait' ,
1014+ methodName : 'switch_backdrop_and_wait'
1015+ }
1016+ ] . forEach ( info => {
1017+ describe ( `${ info . opcode } with backdrop existence check` , ( ) => {
1018+ test ( 'existing backdrop should work' , ( ) => {
1019+ code = `${ info . methodName } ("backdrop1")` ;
1020+ expected = [
1021+ {
1022+ opcode : info . opcode ,
1023+ inputs : [
1024+ {
1025+ name : 'BACKDROP' ,
1026+ block : {
1027+ opcode : 'looks_backdrops' ,
1028+ fields : [
1029+ {
1030+ name : 'BACKDROP' ,
1031+ value : 'backdrop1'
1032+ }
1033+ ] ,
1034+ shadow : true
1035+ }
1036+ }
1037+ ]
1038+ }
1039+ ] ;
1040+ convertAndExpectToEqualBlocks ( converterWithVM , stageWithBackdrops , code , expected ) ;
1041+ } ) ;
1042+
1043+ test ( 'non-existing backdrop should throw error' , ( ) => {
1044+ code = `${ info . methodName } ("NonExistentBackdrop")` ;
1045+ const result = converterWithVM . targetCodeToBlocks ( stageWithBackdrops , code ) ;
1046+ expect ( result ) . toBeFalsy ( ) ;
1047+ expect ( converterWithVM . errors ) . toHaveLength ( 1 ) ;
1048+ expect ( converterWithVM . errors [ 0 ] . text ) . toContain ( 'backdrop "NonExistentBackdrop" does not exist' ) ;
1049+ } ) ;
1050+ } ) ;
1051+ } ) ;
1052+
1053+ describe ( 'backdrop_number and backdrop_name with backdrop existence check' , ( ) => {
1054+ test ( 'backdrop_number should work without backdrop check' , ( ) => {
1055+ code = 'backdrop_number' ;
1056+ expected = [
1057+ {
1058+ opcode : 'looks_backdropnumbername' ,
1059+ fields : [
1060+ {
1061+ name : 'NUMBER_NAME' ,
1062+ value : 'number'
1063+ }
1064+ ]
1065+ }
1066+ ] ;
1067+ convertAndExpectToEqualBlocks ( converterWithVM , stageWithBackdrops , code , expected ) ;
1068+ } ) ;
1069+
1070+ test ( 'backdrop_name should work without backdrop check' , ( ) => {
1071+ code = 'backdrop_name' ;
1072+ expected = [
1073+ {
1074+ opcode : 'looks_backdropnumbername' ,
1075+ fields : [
1076+ {
1077+ name : 'NUMBER_NAME' ,
1078+ value : 'name'
1079+ }
1080+ ]
1081+ }
1082+ ] ;
1083+ convertAndExpectToEqualBlocks ( converterWithVM , stageWithBackdrops , code , expected ) ;
1084+ } ) ;
1085+ } ) ;
1086+ } ) ;
9021087} ) ;
0 commit comments