@@ -1204,6 +1204,55 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
12041204 } ) ;
12051205 } ) ;
12061206
1207+ suite ( 'ternary expressions' , ( ) => {
1208+ test ( 'ternary changes color based on left/right side of canvas' , ( ) => {
1209+ myp5 . createCanvas ( 50 , 25 , myp5 . WEBGL ) ;
1210+ const testShader = myp5 . baseMaterialShader ( ) . modify ( ( ) => {
1211+ myp5 . getPixelInputs ( inputs => {
1212+ inputs . color = inputs . texCoord . x > 0.5 ? [ 1 , 0 , 0 , 1 ] : [ 0 , 0 , 1 , 1 ] ;
1213+ return inputs ;
1214+ } ) ;
1215+ } , { myp5 } ) ;
1216+ myp5 . noStroke ( ) ;
1217+ myp5 . shader ( testShader ) ;
1218+ myp5 . plane ( myp5 . width , myp5 . height ) ;
1219+
1220+ const leftPixel = myp5 . get ( 12 , 12 ) ;
1221+ assert . approximately ( leftPixel [ 0 ] , 0 , 5 ) ;
1222+ assert . approximately ( leftPixel [ 1 ] , 0 , 5 ) ;
1223+ assert . approximately ( leftPixel [ 2 ] , 255 , 5 ) ;
1224+
1225+ const rightPixel = myp5 . get ( 37 , 12 ) ;
1226+ assert . approximately ( rightPixel [ 0 ] , 255 , 5 ) ;
1227+ assert . approximately ( rightPixel [ 1 ] , 0 , 5 ) ;
1228+ assert . approximately ( rightPixel [ 2 ] , 0 , 5 ) ;
1229+ } ) ;
1230+
1231+ test ( 'ternary with scalar values' , ( ) => {
1232+ myp5 . createCanvas ( 50 , 25 , myp5 . WEBGL ) ;
1233+ const testShader = myp5 . baseMaterialShader ( ) . modify ( ( ) => {
1234+ myp5 . getPixelInputs ( inputs => {
1235+ const brightness = inputs . texCoord . x > 0.5 ? 1.0 : 0.0 ;
1236+ inputs . color = [ brightness , brightness , brightness , 1 ] ;
1237+ return inputs ;
1238+ } ) ;
1239+ } , { myp5 } ) ;
1240+ myp5 . noStroke ( ) ;
1241+ myp5 . shader ( testShader ) ;
1242+ myp5 . plane ( myp5 . width , myp5 . height ) ;
1243+
1244+ const leftPixel = myp5 . get ( 12 , 12 ) ;
1245+ assert . approximately ( leftPixel [ 0 ] , 0 , 5 ) ;
1246+ assert . approximately ( leftPixel [ 1 ] , 0 , 5 ) ;
1247+ assert . approximately ( leftPixel [ 2 ] , 0 , 5 ) ;
1248+
1249+ const rightPixel = myp5 . get ( 37 , 12 ) ;
1250+ assert . approximately ( rightPixel [ 0 ] , 255 , 5 ) ;
1251+ assert . approximately ( rightPixel [ 1 ] , 255 , 5 ) ;
1252+ assert . approximately ( rightPixel [ 2 ] , 255 , 5 ) ;
1253+ } ) ;
1254+ } ) ;
1255+
12071256 suite ( 'for loop statements' , ( ) => {
12081257 test ( 'handle simple for loop with known iteration count' , ( ) => {
12091258 myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
@@ -2215,5 +2264,26 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
22152264 assert . include ( errMsg , 'Expected properties' ) ;
22162265 assert . include ( errMsg , 'Received properties' ) ;
22172266 } ) ;
2267+
2268+ test ( 'ternary with mismatched branch types shows both types in error' , ( ) => {
2269+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
2270+
2271+ try {
2272+ myp5 . baseMaterialShader ( ) . modify ( ( ) => {
2273+ myp5 . getPixelInputs ( inputs => {
2274+ // float1 vs float4 - type mismatch
2275+ const val = inputs . texCoord . x > 0.5 ? myp5 . float ( 1.0 ) : [ 1 , 0 , 0 , 1 ] ;
2276+ inputs . color = [ val , val , val , 1 ] ;
2277+ return inputs ;
2278+ } ) ;
2279+ } , { myp5 } ) ;
2280+ } catch ( e ) { /* expected */ }
2281+
2282+ assert . isAbove ( mockUserError . mock . calls . length , 0 , 'FES.userError should have been called' ) ;
2283+ const errMsg = mockUserError . mock . calls [ 0 ] [ 1 ] ;
2284+ assert . include ( errMsg , 'ternary' ) ;
2285+ assert . include ( errMsg , 'float1' ) ;
2286+ assert . include ( errMsg , 'float4' ) ;
2287+ } ) ;
22182288 } ) ;
22192289} ) ;
0 commit comments