1
+ # truffleruby_primitives: true
2
+
1
3
# Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved. This
2
4
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3
5
# redistribute it and/or modify it under the terms of the:
@@ -243,6 +245,33 @@ def spec_it(subject)
243
245
module : Subject . ( name : AN_INSTANCE ) { Module . new } ,
244
246
hash : Subject . ( name : AN_INSTANCE , doc : true ) { { } } ,
245
247
array : Subject . ( name : AN_INSTANCE , doc : true ) { [ ] } ,
248
+ # raise & rescue to give it a backtrace
249
+ exception : Subject . ( name : "an `Exception`" , doc : true ) do
250
+ begin
251
+ raise "the exception message"
252
+ rescue => e
253
+ e
254
+ end
255
+ end ,
256
+ exception_with_cause : Subject . ( name : "an `Exception` with a cause" , doc : true ) do
257
+ begin
258
+ raise "the cause"
259
+ rescue
260
+ begin
261
+ raise "the exception message"
262
+ rescue => e
263
+ e
264
+ end
265
+ end
266
+ end ,
267
+ # also test RaiseException since it is what other languages see when they catch an exception from Ruby
268
+ raise_exception : Subject . ( name : AN_INSTANCE ) do
269
+ begin
270
+ raise "the exception message"
271
+ rescue => e
272
+ Primitive . exception_get_raise_exception ( e )
273
+ end
274
+ end ,
246
275
247
276
proc : Subject . ( proc { |v | v } , name : code ( "proc {...}" ) , doc : true ) ,
248
277
lambda : Subject . ( -> v { v } , name : code ( "lambda {...}" ) , doc : true ) ,
@@ -286,6 +315,7 @@ def spec_it(subject)
286
315
immediate_subjects = [ :false , :true , :zero , :small_integer , :zero_float , :small_float ]
287
316
non_immediate_subjects = SUBJECTS . keys - immediate_subjects
288
317
frozen_subjects = [ :big_decimal , :nil , :symbol , :strange_symbol , :frozen_object ]
318
+ exception_subjects = [ :exception , :exception_with_cause , :raise_exception ]
289
319
290
320
# not part of the standard matrix, not considered in last rest case
291
321
EXTRA_SUBJECTS = {
@@ -298,7 +328,7 @@ def spec_it(subject)
298
328
def predicate ( name , is , *message_args , &setup )
299
329
-> subject do
300
330
setup . call subject if setup
301
- Truffle ::Interop . send ( name , subject , *message_args ) . send ( is ? : should : :should_not , be_true )
331
+ Truffle ::Interop . send ( name , subject , *message_args ) . should == is
302
332
end
303
333
end
304
334
@@ -580,7 +610,7 @@ def array_element_predicate(message, predicate, insert_on_true_case)
580
610
Delimiter [ "Members related messages (incomplete)" ] ,
581
611
Message [ :readMember ,
582
612
Test . new ( "returns a method with the given name when the method is defined" , "any non-immediate `Object`" ,
583
- *non_immediate_subjects - [ :polyglot_object ] ) do |subject |
613
+ *non_immediate_subjects - [ :polyglot_object , :raise_exception ] ) do |subject |
584
614
Truffle ::Interop . read_member ( subject , 'to_s' ) . should == subject . method ( :to_s )
585
615
end ,
586
616
Test . new ( "fails with `UnknownIdentifierException` when the method is not defined" , "any non-immediate `Object`" ,
@@ -626,9 +656,48 @@ def array_element_predicate(message, predicate, insert_on_true_case)
626
656
end ,
627
657
unsupported_test { |subject | Truffle ::Interop . write_member ( subject , :something , 'val' ) } ] ,
628
658
659
+ Delimiter [ "Exception related messages" ] ,
660
+ Message [ :isException ,
661
+ Test . new ( "returns true" , *exception_subjects , &predicate ( :exception? , true ) ) ,
662
+ Test . new ( "returns false" , &predicate ( :exception? , false ) ) ] ,
663
+ Message [ :throwException ,
664
+ Test . new ( "throws the exception" , *exception_subjects ) do |subject |
665
+ -> { Truffle ::Interop . throw_exception ( subject ) } . should raise_error { |e | e . should . equal? ( subject ) }
666
+ end ,
667
+ unsupported_test { |subject | Truffle ::Interop . throw_exception ( subject ) } ] ,
668
+ Message [ :getExceptionType ,
669
+ Test . new ( "returns the exception type" , *exception_subjects ) do |subject |
670
+ Truffle ::Interop . exception_type ( subject ) . should == :RUNTIME_ERROR
671
+ end ,
672
+ unsupported_test { |subject | Truffle ::Interop . exception_type ( subject ) } ] ,
673
+ Message [ :hasExceptionMessage ,
674
+ Test . new ( "returns true" , *exception_subjects , &predicate ( :has_exception_message? , true ) ) ,
675
+ Test . new ( "returns false" , &predicate ( :has_exception_message? , false ) ) ] ,
676
+ Message [ :getExceptionMessage ,
677
+ Test . new ( "returns the message of the exception" , *exception_subjects ) do |subject |
678
+ Truffle ::Interop . exception_message ( subject ) . should == "the exception message"
679
+ end ,
680
+ unsupported_test { |subject | Truffle ::Interop . exception_message ( subject ) } ] ,
681
+ Message [ :hasExceptionStackTrace ,
682
+ Test . new ( "returns true" , *exception_subjects , &predicate ( :has_exception_stack_trace? , true ) ) ,
683
+ Test . new ( "returns false" , &predicate ( :has_exception_stack_trace? , false ) ) ] ,
684
+ Message [ :getExceptionStackTrace ,
685
+ Test . new ( "returns the stacktrace of the exception" , *exception_subjects ) do |subject |
686
+ stacktrace = Truffle ::Interop . exception_stack_trace ( subject )
687
+ Truffle ::Interop . should . has_array_elements? ( stacktrace )
688
+ end ,
689
+ unsupported_test { |subject | Truffle ::Interop . exception_stack_trace ( subject ) } ] ,
690
+ Message [ :hasExceptionCause ,
691
+ Test . new ( "returns true" , :exception_with_cause , &predicate ( :has_exception_cause? , true ) ) ,
692
+ Test . new ( "returns false" , &predicate ( :has_exception_cause? , false ) ) ] ,
693
+ Message [ :getExceptionCause ,
694
+ Test . new ( "returns the cause of the exception" , :exception_with_cause ) do |subject |
695
+ Truffle ::Interop . exception_cause ( subject ) . should == subject . cause
696
+ end ,
697
+ unsupported_test { |subject | Truffle ::Interop . exception_cause ( subject ) } ] ,
698
+
629
699
Delimiter [ "Number related messages (missing)" ] ,
630
700
Delimiter [ "Instantiation related messages (missing)" ] ,
631
- Delimiter [ "Exception related messages (missing)" ] ,
632
701
Delimiter [ "Time related messages (unimplemented)" ] ,
633
702
]
634
703
0 commit comments