diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e6215e..3a84cf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,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) @@ -264,8 +272,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..3589ce4 --- /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(source) { current_example.source } + + context "line numbers" do + it "contains starting line of spec" do + expect(source.line).to eq(__LINE__ - 1) + end + + it "contains ending line of spec" do + expect(source.end_line).to eq(__LINE__ + 1) + end + + it "handles multiple lines and examples" do + # Offset is important. + expect(source.line).to eq(__LINE__ - 2) + # This line fails, refer to https://github.com/crystal-lang/crystal/issues/10562 + # expect(source.end_line).to eq(__LINE__ + 2) + # Offset is still important. + end + end + + context "file names" do + subject { source.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 b466b02..019ab5a 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::Source.new(\{{block.filename}}, \{{block.line_number}}), + ::Spectator::Source.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 85cd739..0c65fba 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.source.line + start_line = example.source.line + end_line = example.source.end_line + (start_line..end_line).covers?(@line) end end end diff --git a/src/spectator/source.cr b/src/spectator/source.cr index b09d5df..e69f5ff 100644 --- a/src/spectator/source.cr +++ b/src/spectator/source.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 source. - 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 source from a string.