shard-spectator/src/spectator/example_builder.cr

39 lines
1.7 KiB
Crystal
Raw Normal View History

2021-07-17 20:01:27 +00:00
require "./context"
require "./example"
require "./location"
require "./metadata"
2021-07-17 17:10:44 +00:00
require "./node_builder"
module Spectator
2021-07-17 20:01:27 +00:00
# Constructs examples.
# Call `#build` to produce an `Example`.
2021-07-17 17:10:44 +00:00
class ExampleBuilder < NodeBuilder
@name : Proc(Example, String) | String?
2021-07-17 20:01:27 +00:00
# Creates the builder.
# A proc provided by *context_builder* is used to create a unique `Context` for each example produced by `#build`.
# The *entrypoint* indicates the proc used to invoke the test code in the example.
# The *name*, *location*, and *metadata* will be applied to the `Example` produced by `#build`.
2021-07-17 17:10:44 +00:00
def initialize(@context_builder : -> Context, @entrypoint : Example ->,
2022-11-30 06:22:42 +00:00
@name : String? = nil, @location : Location? = nil, @metadata : Metadata? = nil)
2021-07-17 17:10:44 +00:00
end
# Creates the builder.
# A proc provided by *context_builder* is used to create a unique `Context` for each example produced by `#build`.
# The *entrypoint* indicates the proc used to invoke the test code in the example.
# The *name* is an interpolated string that runs in the context of the example.
# *location*, and *metadata* will be applied to the `Example` produced by `#build`.
def initialize(@context_builder : -> Context, @entrypoint : Example ->,
@name : Example -> String, @location : Location? = nil, @metadata : Metadata? = nil)
end
2021-07-17 20:01:27 +00:00
# Constructs an example with previously defined attributes and context.
# The *parent* is an already constructed example group to nest the new example under.
# It can be nil if the new example won't have a parent.
def build(parent = nil)
2021-07-17 17:10:44 +00:00
context = @context_builder.call
Example.new(context, @entrypoint, @name, @location, parent, @metadata)
end
end
end