Sha256: 967d4be8c8f9317174c6bcccee14c8cf549b40e08cfb90696968b655f1b956b7

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true
#
# This file is part of the "timers" project and released under the MIT license.
#
# Copyright, 2018, by Samuel Williams. All rights reserved.
#

RSpec.describe Timers::Events do
	it "should register an event" do
		fired = false

		callback = proc do |_time|
			fired = true
		end

		subject.schedule(0.1, callback)

		expect(subject.size).to be == 1

		subject.fire(0.15)

		expect(subject.size).to be == 0

		expect(fired).to be true
	end

	it "should register events in order" do
		fired = []

		times = [0.95, 0.1, 0.3, 0.5, 0.4, 0.2, 0.01, 0.9]

		times.each do |requested_time|
			callback = proc do |_time|
				fired << requested_time
			end

			subject.schedule(requested_time, callback)
		end

		subject.fire(0.5)
		expect(fired).to be == times.sort.first(6)

		subject.fire(1.0)
		expect(fired).to be == times.sort
	end

	it "should fire events with the time they were fired at" do
		fired_at = :not_fired

		callback = proc do |time|
			# The time we actually were fired at:
			fired_at = time
		end

		subject.schedule(0.5, callback)

		subject.fire(1.0)

		expect(fired_at).to be == 1.0
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timers-4.3.0 spec/timers/events_spec.rb
timers-4.2.1 spec/timers/events_spec.rb
timers-4.2.0 spec/timers/events_spec.rb