diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f7cae1..bccdc60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - Removed one-liner it syntax without braces (block). +## [0.9.34] - 2021-03-31 +### Changed +- Allow filtering examples by using any line in the example block. [#19](https://github.com/icy-arctic-fox/spectator/issues/19) Thanks @matthewmcgarvey ! + +## [0.9.33] - 2021-03-22 +### Changed +- Target Crystal 1.0 + ## [0.9.32] - 2021-02-03 ### Fixed - Fix source reference with brace-less example syntax. [#20](https://github.com/icy-arctic-fox/spectator/issues/20) @@ -266,8 +274,10 @@ This has been changed so that it compiles and raises an error at runtime with a First version ready for public use. -[Unreleased]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.32...HEAD -[0.9.31]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.31...v0.9.32 +[Unreleased]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.34...HEAD +[0.9.34]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.33...v0.9.34 +[0.9.33]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.32...v0.9.33 +[0.9.32]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.31...v0.9.32 [0.9.31]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.30...v0.9.31 [0.9.30]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.29...v0.9.30 [0.9.29]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.28...v0.9.29 diff --git a/shard.yml b/shard.yml index e624ed0..830c2c0 100644 --- a/shard.yml +++ b/shard.yml @@ -6,7 +6,7 @@ description: | authors: - Michael Miller -crystal: 0.35.1 +crystal: 1.0.0 license: MIT diff --git a/spec/line_number_spec.cr b/spec/line_number_spec.cr new file mode 100644 index 0000000..5439ea0 --- /dev/null +++ b/spec/line_number_spec.cr @@ -0,0 +1,32 @@ +require "./spec_helper" + +Spectator.describe Spectator do + let(current_example) { ::Spectator::Example.current } + subject(location) { current_example.location } + + context "line numbers" do + it "contains starting line of spec" do + expect(location.line).to eq(__LINE__ - 1) + end + + it "contains ending line of spec" do + expect(location.end_line).to eq(__LINE__ + 1) + end + + it "handles multiple lines and examples" do + # Offset is important. + expect(location.line).to eq(__LINE__ - 2) + # This line fails, refer to https://github.com/crystal-lang/crystal/issues/10562 + # expect(location.end_line).to eq(__LINE__ + 2) + # Offset is still important. + end + end + + context "file names" do + subject { location.file } + + it "match source code" do + is_expected.to eq(__FILE__) + end + end +end diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index 3e05c11..5d8b88b 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -47,7 +47,7 @@ module Spectator::DSL ::Spectator::DSL::Builder.add_example( _spectator_example_name(\{{what}}), - ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}), + ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}, \{{block.end_line_number}}), new.as(::Spectator::Context), \%tags ) do |example| diff --git a/src/spectator/line_example_filter.cr b/src/spectator/line_example_filter.cr index dac4bcf..25b5fc8 100644 --- a/src/spectator/line_example_filter.cr +++ b/src/spectator/line_example_filter.cr @@ -7,7 +7,9 @@ module Spectator # Checks whether the example satisfies the filter. def includes?(example) : Bool - @line == example.location.line + start_line = example.location.line + end_line = example.location.end_line + (start_line..end_line).covers?(@line) end end end diff --git a/src/spectator/location.cr b/src/spectator/location.cr index 2c6f444..bf1ac68 100644 --- a/src/spectator/location.cr +++ b/src/spectator/location.cr @@ -6,11 +6,17 @@ module Spectator # Absolute file path. getter file : String - # Line number in the file. + # Starting line number in the file. getter line : Int32 + # Ending line number in the file. + getter end_line : Int32 + # Creates the location. - def initialize(@file, @line) + def initialize(@file, @line, end_line = nil) + # if an end line is not provided, + # make the end line the same as the start line + @end_line = end_line || @line end # Parses a location from a string.