Quick implementation of pending examles

This commit is contained in:
Michael Miller 2018-09-15 10:36:20 -06:00
parent 7cfa432dfd
commit dc8e651209
5 changed files with 82 additions and 1 deletions

View file

@ -95,7 +95,7 @@ In no particular order, features that have been implemented and are planned:
- [ ] Should syntax
- [X] Helper methods and modules
- [ ] Aliasing - custom example group types with preset attributes
- [ ] Pending tests - `pending`
- [X] Pending tests - `pending`
- [ ] Shared examples - `behaves_like`, `include_examples`
- [ ] Matchers
- [ ] Equality matchers - `eq`, `be`, `be_a`, `match`

View file

@ -182,6 +182,63 @@ module Spectator
{% end %}
end
macro pending(description, &block)
{%
parent_module = @type
safe_name = description.id.stringify.chars.map { |c| ::Spectator::ContextDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
class_name = (safe_name.camelcase + "Example").id
given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given]
var_names = given_vars.map { |v| v[:name] }
%}
class Example%example
include ExampleDSL
include {{parent_module}}
def %run({{ var_names.join(", ").id }})
{{block.body}}
end
end
class {{class_name.id}} < ::Spectator::PendingExample
{% for given_var, i in given_vars %}
@%var{i} : ValueWrapper
private def %var{i}
@%var{i}.unsafe_as(TypedValueWrapper(typeof({{given_var[:type_def]}}))).value
end
{% end %}
def initialize(context{% for v, i in var_names %}, %var{i}{% end %})
super(context)
{% for given_var, i in given_vars %}
@%var{i} = TypedValueWrapper(typeof({{given_var[:type_def]}})).new(%var{i})
{% end %}
end
def description
{% if description.is_a?(StringLiteral) %}
{{description}}
{% else %}
{{description.stringify}}
{% end %}
end
end
%current_context = ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}]
{% for given_var, i in given_vars %}
{%
var_name = given_var[:name]
collection = given_var[:collection]
%}
{{collection}}.each do |%var{i}|
{% end %}
%current_context.examples << {{class_name.id}}.new(%current_context {% for v, i in var_names %}, %var{i}{% end %})
{% for given_var in given_vars %}
end
{% end %}
end
def it_behaves_like
raise NotImplementedError.new("Spectator::DSL#it_behaves_like")
end

View file

@ -0,0 +1,9 @@
require "./example"
module Spectator
abstract class PendingExample < Example
def run
PendingExampleResult.new(self)
end
end
end

View file

@ -0,0 +1,13 @@
require "./example_result"
module Spectator
class PendingExampleResult < ExampleResult
def passed?
false
end
def initialize(@example)
super(@example, Time::Span.new(nanoseconds: 0))
end
end
end

View file

@ -18,6 +18,8 @@ module Spectator
print case result
when SuccessfulExampleResult
".".colorize.green
when PendingExampleResult
"P".colorize.yellow
when ErroredExampleResult
"E".colorize.magenta
when FailedExampleResult