@@ -862,6 +862,80 @@ func TestUntilLoops(t *testing.T) {
862862 }
863863}
864864
865+ func TestCaseWhenExpressions (t * testing.T ) {
866+ script := compileScript (t , `
867+ def label(score)
868+ case score
869+ when 100
870+ "perfect"
871+ when 90, 95
872+ "great"
873+ else
874+ "ok"
875+ end
876+ end
877+
878+ def classify(value)
879+ case value
880+ when nil
881+ "missing"
882+ when true
883+ "yes"
884+ else
885+ "other"
886+ end
887+ end
888+
889+ def assign_case(v)
890+ result = case v
891+ when 1
892+ 10
893+ else
894+ 20
895+ end
896+ result
897+ end
898+
899+ def unmatched(v)
900+ case v
901+ when 1
902+ "one"
903+ end
904+ end
905+ ` )
906+
907+ if got := callFunc (t , script , "label" , []Value {NewInt (100 )}); ! got .Equal (NewString ("perfect" )) {
908+ t .Fatalf ("label(100) mismatch: %v" , got )
909+ }
910+ if got := callFunc (t , script , "label" , []Value {NewInt (95 )}); ! got .Equal (NewString ("great" )) {
911+ t .Fatalf ("label(95) mismatch: %v" , got )
912+ }
913+ if got := callFunc (t , script , "label" , []Value {NewInt (70 )}); ! got .Equal (NewString ("ok" )) {
914+ t .Fatalf ("label(70) mismatch: %v" , got )
915+ }
916+
917+ if got := callFunc (t , script , "classify" , []Value {NewNil ()}); ! got .Equal (NewString ("missing" )) {
918+ t .Fatalf ("classify(nil) mismatch: %v" , got )
919+ }
920+ if got := callFunc (t , script , "classify" , []Value {NewBool (true )}); ! got .Equal (NewString ("yes" )) {
921+ t .Fatalf ("classify(true) mismatch: %v" , got )
922+ }
923+ if got := callFunc (t , script , "classify" , []Value {NewInt (1 )}); ! got .Equal (NewString ("other" )) {
924+ t .Fatalf ("classify(1) mismatch: %v" , got )
925+ }
926+
927+ if got := callFunc (t , script , "assign_case" , []Value {NewInt (1 )}); ! got .Equal (NewInt (10 )) {
928+ t .Fatalf ("assign_case(1) mismatch: %v" , got )
929+ }
930+ if got := callFunc (t , script , "assign_case" , []Value {NewInt (2 )}); ! got .Equal (NewInt (20 )) {
931+ t .Fatalf ("assign_case(2) mismatch: %v" , got )
932+ }
933+
934+ if got := callFunc (t , script , "unmatched" , []Value {NewInt (7 )}); ! got .Equal (NewNil ()) {
935+ t .Fatalf ("unmatched(7) expected nil, got %v" , got )
936+ }
937+ }
938+
865939func TestLoopControlBreakAndNext (t * testing.T ) {
866940 script := compileScript (t , `
867941 def for_break()
0 commit comments