@@ -140,9 +140,14 @@ def assert_documents_match(actual, expected)
140140 end
141141 end
142142
143- def assert_document_matches ( actual , expected , msg )
144- unless actual == expected
145- raise Error ::ResultMismatch , "#{ msg } does not match"
143+ def assert_document_matches ( actual , expected , msg , as_root : false )
144+ if !as_root && actual . keys . to_set != expected . keys . to_set
145+ raise Error ::ResultMismatch , "#{ msg } keys do not match: expected #{ expected . keys } , actual #{ actual . keys } "
146+ end
147+ expected . each do |key , expected_value |
148+ raise Error ::ResultMismatch , "#{ msg } has no key #{ key } " unless actual . key? ( key )
149+ actual_value = actual [ key ]
150+ assert_value_matches ( actual_value , expected_value , "#{ msg } key #{ key } " )
146151 end
147152 end
148153
@@ -383,6 +388,14 @@ def assert_value_matches(actual, expected, msg)
383388 if actual . nil? || actual >= expected_v
384389 raise Error ::ResultMismatch , "Actual value #{ actual } should be less than #{ expected_v } "
385390 end
391+ when '$$matchAsDocument'
392+ actual_v = BSON ::ExtJSON . parse ( actual )
393+ match_as_root = false
394+ if expected_v . keys . first == '$$matchAsRoot'
395+ expected_v = expected_v . values . first
396+ match_as_root = true
397+ end
398+ assert_document_matches ( actual_v , expected_v , msg , as_root : match_as_root )
386399 else
387400 raise NotImplementedError , "Unknown operator #{ operator } "
388401 end
@@ -392,5 +405,44 @@ def assert_value_matches(actual, expected, msg)
392405 end
393406 end
394407 end
408+
409+ def assert_tracing_messages
410+ return unless @expected_tracing_messages
411+ @expected_tracing_messages . each do |spec |
412+ spec = UsingHash [ spec ]
413+ client_id = spec . use! ( 'client' )
414+ client = entities . get ( :client , client_id )
415+ tracer = @tracers . fetch ( client )
416+ expected_spans = spec . use! ( 'spans' )
417+ ignore_extra_spans = if ignore = spec . use ( 'ignoreExtraSpans' )
418+ # Ruby treats 0 as truthy, whereas the spec tests use it as falsy.
419+ ignore == 0 ? false : ignore
420+ else
421+ false
422+ end
423+ actual_spans = tracer . span_hierarchy
424+ if ( !ignore_extra_spans && actual_spans . length != expected_spans . length ) ||
425+ ( ignore_extra_spans && actual_spans . length < expected_spans . length )
426+ raise Error ::ResultMismatch , "Span count mismatch: expected #{ expected_spans . length } , actual #{ actual_spans . length } \n Expected: #{ expected_spans } \n Actual: #{ actual_spans } "
427+ end
428+ expected_spans . each_with_index do |expected , i |
429+ assert_span_matches ( actual_spans [ i ] , expected )
430+ end
431+ end
432+ end
433+
434+ def assert_span_matches ( actual , expected )
435+ assert_eq ( actual . name , expected . use! ( 'name' ) , 'Span name does not match' )
436+ expected_attributes = UsingHash [ expected . use! ( 'tags' ) ]
437+ expected_attributes . each do |key , value |
438+ actual_value = actual . attributes [ key ]
439+ assert_value_matches ( actual_value , value , "Span attribute #{ key } " )
440+ end
441+
442+ expected_nested_spans = expected . use ( 'nested' ) || [ ]
443+ expected_nested_spans . each_with_index do |nested_expected , i |
444+ assert_span_matches ( actual . nested [ i ] , nested_expected )
445+ end
446+ end
395447 end
396448end
0 commit comments