Skip to content

Instantly share code, notes, and snippets.

@zenspider
Last active July 20, 2023 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenspider/490f082c8b5988e3b412de7e559b9548 to your computer and use it in GitHub Desktop.
Save zenspider/490f082c8b5988e3b412de7e559b9548 to your computer and use it in GitHub Desktop.
diff -r old/lib/minitest.rb new/lib/minitest.rb
--- old/lib/minitest.rb
+++ new/lib/minitest.rb
--- /tmp/tmp.99247.0x2055f8280.49 2023-07-20 11:24:53
+++ /Users/ryan/Work/p4/zss/src/minitest/dev/lib/minitest.rb 2023-07-20 11:12:35
@@ -446,6 +446,31 @@
self.name = name
self.failures = []
self.assertions = 0
+ # lazy initializer for metadata
+ end
+
+ ##
+ # Metadata you attach to the test results that get sent to the reporter.
+ #
+ # Lazily initializes to a hash, to keep memory down.
+ #
+ # NOTE: this data *must* be plain (read: marshal-able) data!
+ # Hashes! Arrays! Strings!
+
+ def metadata
+ @metadata ||= {}
+ end
+
+ ##
+ # Sets metadata, mainly used for +Result.from+.
+
+ attr_writer :metadata
+
+ ##
+ # Returns true if metadata exists.
+
+ def metadata?
+ defined? @metadata
end
##
@@ -566,6 +591,7 @@
r.assertions = o.assertions
r.failures = o.failures.dup
r.time = o.time
+ r.metadata = o.metadata if o.metadata?
r.source_location = o.method(o.name).source_location rescue ["unknown", -1]
diff -r old/test/minitest/test_minitest_reporter.rb new/test/minitest/test_minitest_reporter.rb
--- old/test/minitest/test_minitest_reporter.rb
+++ new/test/minitest/test_minitest_reporter.rb
--- /tmp/tmp.99247.0x2055f8280.64 2023-07-20 11:24:53
+++ /Users/ryan/Work/p4/zss/src/minitest/dev/test/minitest/test_minitest_reporter.rb 2023-07-20 11:10:25
@@ -65,6 +65,12 @@
@pt ||= Minitest::Result.from Minitest::Test.new(:woot)
end
+ def passing_test_with_metadata
+ test = Minitest::Test.new(:woot)
+ test.metadata[:meta] = :data
+ @pt ||= Minitest::Result.from test
+ end
+
def skip_test
unless defined? @st then
@st = Minitest::Test.new(:woot)
@@ -166,6 +172,29 @@
assert_equal 0, r.assertions
end
+ def test_record_pass_with_metadata
+ reporter = self.r
+
+ def reporter.metadata
+ @metadata
+ end
+
+ def reporter.record result
+ super
+ @metadata = result.metadata if result.metadata?
+ end
+
+ r.record passing_test_with_metadata
+
+ exp = { :meta => :data }
+ assert_equal exp, reporter.metadata
+
+ assert_equal ".", io.string
+ assert_empty r.results
+ assert_equal 1, r.count
+ assert_equal 0, r.assertions
+ end
+
def test_record_fail
fail_test = self.fail_test
r.record fail_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment