Class: Quickdraw::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/quickdraw/context.rb

Constant Summary collapse

DEFAULT_MATCHERS =
[
	Quickdraw::Matchers::Boolean,
	Quickdraw::Matchers::CaseEquality,
	Quickdraw::Matchers::Change,
	Quickdraw::Matchers::Equality,
	Quickdraw::Matchers::Include,
	Quickdraw::Matchers::Predicate,
	Quickdraw::Matchers::RespondTo,
	Quickdraw::Matchers::ToBeA,
	Quickdraw::Matchers::ToHaveAttributes,
	Quickdraw::Matchers::ToRaise,
	Quickdraw::Matchers::ToReceive,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run, path) ⇒ Context

Returns a new instance of Context.



60
61
62
63
64
65
66
67
68
# File 'lib/quickdraw/context.rb', line 60

def initialize(run, path)
	@run = run
	@path = path
	@expectations = []
	@matchers = self.class.matchers

	@name = nil
	@skip = false
end

Class Method Details

.describe(description, &block) ⇒ Object Also known as: context



41
42
43
44
45
46
47
# File 'lib/quickdraw/context.rb', line 41

def describe(description, &block)
	unless defined?(@sub_contexts)
		@sub_contexts = []
	end

	@sub_contexts << [Class.new(self, &block), description]
end

.matchersObject



33
34
35
36
37
38
39
# File 'lib/quickdraw/context.rb', line 33

def matchers
	@matchers ||= if superclass < Quickdraw::Context
		superclass.matchers.dup
	else
		Set.new(DEFAULT_MATCHERS)
	end
end

.run(result = Quickdraw::Runner.new, path = []) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/quickdraw/context.rb', line 19

def run(result = Quickdraw::Runner.new, path = [])
	new(result, path).run(@tests) if @tests

	if defined?(@sub_contexts)
		@sub_contexts.each do |(context, desc)|
			context.run(result, [*path, desc])
		end
	end
end

.test(name = nil, skip: false, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/quickdraw/context.rb', line 51

def test(name = nil, skip: false, &block)
	unless defined?(@tests)
		@tests = []
	end

	@tests << [name, skip, block]
end

.use(*new_matchers) ⇒ Object



29
30
31
# File 'lib/quickdraw/context.rb', line 29

def use(*new_matchers)
	new_matchers.each { |m| matchers << m }
end

Instance Method Details

#assert(value) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/quickdraw/context.rb', line 99

def assert(value)
	if value
		success!
	elsif block_given?
		failure! { yield(value) }
	else
		failure! { "expected #{value.inspect} to be truthy" }
	end
end

#expect(value = Quickdraw::Null, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/quickdraw/context.rb', line 81

def expect(value = Quickdraw::Null, &block)
	type = Quickdraw::Null == value ? block : value

	expectation_class = Quickdraw::Config.registry.expectation_for(
		type, matchers: @matchers
	)

	expectation = expectation_class.new(self, value, &block)
	@expectations << expectation
	expectation
end

#failure!Object



127
128
129
130
131
132
133
# File 'lib/quickdraw/context.rb', line 127

def failure!(&)
	if @skip
		@run.success!(@name)
	else
		@run.failure!(full_path, &)
	end
end

#full_pathObject



135
136
137
# File 'lib/quickdraw/context.rb', line 135

def full_path
	[*@path, @name]
end

#refute(value) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/quickdraw/context.rb', line 109

def refute(value)
	if !value
		success!
	elsif block_given?
		failure! { yield(value) }
	else
		failure! { "expected #{value.inspect} to be falsy" }
	end
end

#resolveObject



93
94
95
96
97
# File 'lib/quickdraw/context.rb', line 93

def resolve
	@expectations.each(&:resolve)
ensure
	@expectations.clear
end

#run(tests) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/quickdraw/context.rb', line 70

def run(tests)
	tests.each do |(name, skip, block)|
		@name = name
		@skip = skip

		instance_exec(&block)

		resolve
	end
end

#success!Object



119
120
121
122
123
124
125
# File 'lib/quickdraw/context.rb', line 119

def success!
	if @skip
		@run.failure!(full_path) { "The skipped test `#{@name}` started passing." }
	else
		@run.success!(@name)
	end
end