Skip to content

Commit

Permalink
Merge pull request #356 from scarpe-team/unit_test_helpers_component
Browse files Browse the repository at this point in the history
Move unit test helpers, but not assertions, into Scarpe-Components
  • Loading branch information
noahgibbs authored Aug 16, 2023
2 parents 1e93120 + d4638ea commit db44220
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
88 changes: 88 additions & 0 deletions lib/scarpe/evented_assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

require "tempfile"
require "json"
require "fileutils"

module Scarpe::Test; end

# We need a separate assertion system for the kind of Scarpe
# testing where we start a subprocess, perform the assertion
# logic in the subprocess, and then run Minitest in the parent.
# It's an unusual setup.
module Scarpe::Test::EventedAssertions
# Assert that `text` includes `subtext`.
#
# @param text [String] the longer text
# @param subtext [String] the text that is asserted to be included
# @param msg [String,nil] if supplied, the failure message for the assertion
# @return [void]
def assert_include(text, subtext, msg = nil)
msg ||= "Expected #{text.inspect} to include #{subtext.inspect}"
assert text.include?(subtext), msg
end

# Assert that `text` does not include `subtext`.
#
# @param text [String] the longer text
# @param subtext [String] the text that is asserted to not be included
# @param msg [String,nil] if supplied, the failure message for the assertion
# @return [void]
def assert_not_include(text, subtext, msg = nil)
msg ||= "Expected #{text.inspect} not to include #{subtext.inspect}"
assert !text.include?(subtext), msg
end

# Assert that `actual_html` is the same as `expected_tag` with `opts`.
# This uses Scarpe's HTML tag-based renderer to render the tag and options
# into text, and valides that the text is the same.
#
# @see Scarpe::HTML.render
#
# @param actual_html [String] the html to compare to
# @param expected_tag [String,Symbol] the HTML tag, used to send a method call
# @param opts keyword options passed to the tag method call
# @yield block passed to the tag method call.
# @return [void]
def assert_html(actual_html, expected_tag, **opts, &block)
expected_html = Scarpe::HTML.render do |h|
h.public_send(expected_tag, opts, &block)
end

assert_equal expected_html, actual_html
end

# This does a final return of results. If it gets called
# multiple times, the test fails because that's not allowed.
#
# @param result_bool [Boolean] true if the results are success, false if failure
# @param msg [String] the message included with the results
# @param data [Hash] any additional data to pass with the results
# @return void
def return_results(result_bool, msg, data = {})
result_file = ENV["SCARPE_TEST_RESULTS"] || "./scarpe_results.txt"

result_structs = [result_bool, msg, data.merge(test_metadata)]
if File.exist?(result_file)
results_returned = JSON.parse File.read(result_file)
end

# Multiple different sets of results is bad, even if both are passing.
if results_returned && results_returned[0..1] != result_structs[0..1]
# Just raising here doesn't reliably fail the test.
# See: https://github.com/scarpe-team/scarpe/issues/212
Shoes::Log.logger("Test Results").error("Writing multi-result failure file to #{result_file.inspect}!")

new_res_data = { first_result: results_returned, second_result: result_structs }.merge(test_metadata)
bad_result = [false, "Returned two sets of results!", new_res_data]
File.write(result_file, JSON.pretty_generate(bad_result))

return
elsif results_returned
Shoes::Log.logger("Test Results").warn "Returning identical results twice: #{results_returned.inspect}"
end

Shoes::Log.logger("Test Results").debug("Writing results file #{result_file.inspect} to disk!")
File.write(result_file, JSON.pretty_generate(result_structs))
end
end
File renamed without changes.
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "scarpe"
require "scarpe/evented_assertions"
require "scarpe/unit_test_helpers"

require "json"
Expand All @@ -22,6 +23,7 @@

class ScarpeWebviewTest < Minitest::Test
include Scarpe::Test::Helpers
include Scarpe::Test::EventedAssertions

SCARPE_EXE = File.expand_path("../exe/scarpe", __dir__)

Expand Down

0 comments on commit db44220

Please sign in to comment.