diff --git a/CHANGELOG.md b/CHANGELOG.md index 955e152..3b22104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Simplify and reduce defined types and generics. Should speed up compilation times. - `around_each` hooks wrap `before_all` and `after_all` hooks. [#12](https://github.com/icy-arctic-fox/spectator/issues/12) +- `given` (now `provided`) blocks changed to produce a single example. `it` can no longer be nested in a `provided` block. - The "should" syntax no longer reports the source as inside Spectator. - Short-hand "should" syntax must be included by using `require "spectator/should"` - `it { should eq("foo") }` - Overhaul example creation and handling. @@ -31,9 +32,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated - `pending` blocks will behave differently in v0.11.0. They will mimic RSpec in that they _compile and run_ the block expecting it to fail. Use a `skip` (or `xit`) block instead to prevent compiling the example. +- `given` has been renamed to `provided`. The `given` keyword may be reused later for memoization. ### Removed -- Removed one-liner it syntax without braces (block). +- Removed one-liner `it`-syntax without braces (block). ## [0.9.38] - 2021-05-27 ### Fixed diff --git a/src/spectator/dsl/concise.cr b/src/spectator/dsl/concise.cr new file mode 100644 index 0000000..0fb13d2 --- /dev/null +++ b/src/spectator/dsl/concise.cr @@ -0,0 +1,41 @@ +require "./examples" +require "./groups" +require "./memoize" + +module Spectator::DSL + # DSL methods and macros for shorter syntax. + module Concise + # Defines an example and input values in a shorter syntax. + # The only arguments given to this macro are one or more assignments. + # The names in the assigments will be available in the example code. + # + # If the code block is omitted, then the example is skipped (marked as not implemented). + # + # Tags and metadata cannot be used with this macro. + # + # ``` + # given x = 42 do + # expect(x).to eq(42) + # end + # ``` + macro provided(*assignments, &block) + class Given%given < {{@type.id}} + {% for assignment in assignments %} + let({{assignment.target}}) { {{assignment.value}} } + {% end %} + + {% if block %} + example {{block}} + {% else %} + example {{assignments.splat.stringify}} + {% end %} + end + end + + # :ditto: + @[Deprecated("Use `provided` instead.")] + macro given(*assignments, &block) + provided({{assignments.splat}}) {{block}} + end + end +end diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 4eee462..d1ec249 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -116,6 +116,6 @@ module Spectator::DSL define_example_group :xcontext, skip: "Temporarily skipped with xcontext" - # TODO: sample, random_sample, and given + # TODO: sample, random_sample end end diff --git a/src/spectator/test_context.cr b/src/spectator/test_context.cr index 5559473..f02f334 100644 --- a/src/spectator/test_context.cr +++ b/src/spectator/test_context.cr @@ -8,6 +8,7 @@ require "./tags" # This type is intentionally outside the `Spectator` module. # The reason for this is to prevent name collision when using the DSL to define a spec. class SpectatorTestContext < SpectatorContext + include ::Spectator::DSL::Concise include ::Spectator::DSL::Examples include ::Spectator::DSL::Expectations include ::Spectator::DSL::Groups