github.com/nycdavid/zeus@v0.0.0-20201208104106-9ba439429e03/rubygem/lib/zeus/m/test_method.rb (about) 1 module Zeus 2 module M 3 ### Simple data structure for what a test method contains. 4 # 5 # Too lazy to make a class for this when it's really just a bag of data 6 # without any behavior. 7 # 8 # Includes the name of this method, what line on the file it begins on, 9 # and where it ends. 10 class TestMethod < Struct.new(:name, :start_line, :end_line) 11 # Set up a new test method for this test suite class 12 def self.create(suite_class, test_method, find_locations = true) 13 # Hopefully it's been defined as an instance method, so we'll need to 14 # look up the ruby Method instance for it 15 method = suite_class.instance_method(test_method) 16 17 if find_locations 18 # Ruby can find the starting line for us, so pull that out of the array 19 start_line = method.source_location.last 20 21 # Ruby can't find the end line however, and I'm too lazy to write 22 # a parser. Instead, `method_source` adds `Method#source` so we can 23 # deduce this ourselves. 24 # 25 # The end line should be the number of line breaks in the method source, 26 # added to the starting line and subtracted by one. 27 end_line = method.source.split("\n").size + start_line - 1 28 end 29 30 # Shove the given attributes into a new databag 31 new(test_method, start_line, end_line) 32 end 33 34 def escaped_name 35 Regexp.escape(name) 36 end 37 end 38 end 39 end