test_helper.rb

  1# frozen_string_literal: true
  2
  3require "simplecov"
  4SimpleCov.start do
  5	add_filter "/test/"
  6	enable_coverage :branch
  7end
  8
  9require "em_promise"
 10require "fiber"
 11require "minitest/autorun"
 12require "rantly/minitest_extensions"
 13require "webmock/minitest"
 14begin
 15	require "pry-rescue/minitest"
 16	require "pry-reload"
 17
 18	module Minitest
 19		class Test
 20			alias old_capture_exceptions capture_exceptions
 21			def capture_exceptions
 22				old_capture_exceptions do
 23					yield
 24				rescue Minitest::Skip => e
 25					failures << e
 26				end
 27			end
 28		end
 29	end
 30rescue LoadError
 31	# Just helpers for dev, no big deal if missing
 32	nil
 33end
 34
 35require "backend_sgx"
 36
 37CONFIG = {
 38	sgx: "sgx",
 39	component: {
 40		jid: "component"
 41	},
 42	creds: {
 43		account: "test_bw_account",
 44		username: "test_bw_user",
 45		password: "test_bw_password"
 46	},
 47	activation_amount: 1,
 48	plans: [
 49		{
 50			name: "test_usd",
 51			currency: :USD,
 52			monthly_price: 1000
 53		},
 54		{
 55			name: "test_bad_currency",
 56			currency: :BAD
 57		}
 58	],
 59	braintree: {
 60		merchant_accounts: {
 61			USD: "merchant_usd"
 62		}
 63	},
 64	credit_card_url: ->(*) { "http://creditcard.example.com" }
 65}.freeze
 66
 67BACKEND_SGX = Minitest::Mock.new(BackendSgx.new)
 68
 69BLATHER = Class.new {
 70	def <<(*); end
 71}.new.freeze
 72
 73class Matching
 74	def initialize(&block)
 75		@block = block
 76	end
 77
 78	def ===(other)
 79		@block.call(other)
 80	end
 81end
 82
 83class PromiseMock < Minitest::Mock
 84	def then(succ=nil, _=nil)
 85		if succ
 86			succ.call(self)
 87		else
 88			yield self
 89		end
 90	end
 91end
 92
 93module EventMachine
 94	class << self
 95		# Patch EM.add_timer to be instant in tests
 96		alias old_add_timer add_timer
 97		def add_timer(*args, &block)
 98			args[0] = 0
 99			old_add_timer(*args, &block)
100		end
101	end
102end
103
104module Minitest
105	class Test
106		def self.property(m, &block)
107			define_method("test_#{m}") do
108				property_of(&block).check { |args| send(m, *args) }
109			end
110		end
111
112		def self.em(m)
113			alias_method "raw_#{m}", m
114			define_method(m) do
115				EM.run do
116					Fiber.new {
117						begin
118							send("raw_#{m}")
119						ensure
120							EM.stop
121						end
122					}.resume
123				end
124			end
125		end
126	end
127end