mirror of
				https://gitea.invidious.io/iv-org/shard-spectator.git
				synced 2024-08-15 00:53:35 +00:00 
			
		
		
		
	Break apart runnable example and document it
This commit is contained in:
		
							parent
							
								
									34dce28663
								
							
						
					
					
						commit
						77f631205e
					
				
					 1 changed files with 32 additions and 13 deletions
				
			
		|  | @ -6,15 +6,10 @@ module Spectator | ||||||
|   # the example code, and capturing a result. |   # the example code, and capturing a result. | ||||||
|   # Sub-classes need to implement the `#run_instance` method. |   # Sub-classes need to implement the `#run_instance` method. | ||||||
|   abstract class RunnableExample < Example |   abstract class RunnableExample < Example | ||||||
|     # Runs the example, hooks, and captures the result. |     # Runs the example, hooks, and captures the result | ||||||
|  |     # and translates to a usable result. | ||||||
|     def run_inner : Result |     def run_inner : Result | ||||||
|       result = ResultCapture.new |       result = capture_result | ||||||
|       begin |  | ||||||
|         run_before_hooks |  | ||||||
|         wrapped_capture_result(result).call |  | ||||||
|       ensure |  | ||||||
|         run_after_hooks |  | ||||||
|       end |  | ||||||
|       expectations = Internals::Harness.current.expectation_results |       expectations = Internals::Harness.current.expectation_results | ||||||
|       translate_result(result, expectations) |       translate_result(result, expectations) | ||||||
|     end |     end | ||||||
|  | @ -26,29 +21,49 @@ module Spectator | ||||||
|     private def run_before_hooks |     private def run_before_hooks | ||||||
|       group.run_before_all_hooks |       group.run_before_all_hooks | ||||||
|       group.run_before_each_hooks |       group.run_before_each_hooks | ||||||
|  |     rescue ex | ||||||
|  |       # If an error occurs in the before hooks, skip running the example. | ||||||
|  |       raise Exception.new("Error encountered while running before hooks", ex) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Runs the hooks that should be performed after the test code finishes. |     # Runs the hooks that should be performed after the test code finishes. | ||||||
|     private def run_after_hooks |     private def run_after_hooks(result) | ||||||
|       group.run_after_each_hooks |       group.run_after_each_hooks | ||||||
|       group.run_after_all_hooks |       group.run_after_all_hooks | ||||||
|     rescue ex |     rescue ex | ||||||
|       # TODO: Pass along error to result. |       # Store the error from the hooks | ||||||
|  |       # if the example didn't encounter an error. | ||||||
|  |       result.error = ex unless result.error | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     # Runs all hooks and the example code. | ||||||
|  |     # A captured result is returned. | ||||||
|  |     private def capture_result : ResultCapture | ||||||
|  |       ResultCapture.new.tap do |result| | ||||||
|  |         # Get the proc that will call around-each hooks and the example. | ||||||
|  |         wrapper = wrapped_run_example(result) | ||||||
|  |         begin | ||||||
|  |           run_before_hooks | ||||||
|  |           wrapper.call | ||||||
|  |         ensure | ||||||
|  |           run_after_hooks(result) | ||||||
|  |         end | ||||||
|  |       end | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Creates a proc that runs the test code |     # Creates a proc that runs the test code | ||||||
|     # and captures the result. |     # and captures the result. | ||||||
|     private def wrapped_capture_result(result) |     private def wrapped_run_example(result) : -> | ||||||
|       # Wrap the method that runs and captures |       # Wrap the method that runs and captures | ||||||
|       # the test code with the around-each hooks. |       # the test code with the around-each hooks. | ||||||
|       group.wrap_around_each_hooks do |       group.wrap_around_each_hooks do | ||||||
|         # Pass along the result capture utility. |         # Pass along the result capture utility. | ||||||
|         capture_result(result) |         run_example(result) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Runs the test code and captures the result. |     # Runs the test code and captures the result. | ||||||
|     private def capture_result(result) |     private def run_example(result) | ||||||
|       # Capture how long it takes to run the test code. |       # Capture how long it takes to run the test code. | ||||||
|       result.elapsed = Time.measure do |       result.elapsed = Time.measure do | ||||||
|         begin |         begin | ||||||
|  | @ -60,12 +75,16 @@ module Spectator | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     # Creates a result instance from captured result information. | ||||||
|     private def translate_result(result, expectations) |     private def translate_result(result, expectations) | ||||||
|       case (error = result.error) |       case (error = result.error) | ||||||
|  |       # If no errors occurred, then the example ran successfully. | ||||||
|       when Nil |       when Nil | ||||||
|         SuccessfulResult.new(self, result.elapsed, expectations) |         SuccessfulResult.new(self, result.elapsed, expectations) | ||||||
|  |       # If a required expectation fails, then a `ExpectationRailed` exception will be raised. | ||||||
|       when ExpectationFailed |       when ExpectationFailed | ||||||
|         FailedResult.new(self, result.elapsed, expectations, error) |         FailedResult.new(self, result.elapsed, expectations, error) | ||||||
|  |       # Any other exception that is raised is unexpected and is an errored result. | ||||||
|       else |       else | ||||||
|         ErroredResult.new(self, result.elapsed, expectations, error) |         ErroredResult.new(self, result.elapsed, expectations, error) | ||||||
|       end |       end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue