Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Ranges of Char in macros #11261

Open
postmodern opened this issue Sep 30, 2021 · 3 comments
Open

Allow Ranges of Char in macros #11261

postmodern opened this issue Sep 30, 2021 · 3 comments

Comments

@postmodern
Copy link
Contributor

Discussion

Currently it is not possible to define a range of Chars in macros.

Example

{% alpha = ('A'..'Z').to_a + ('a'..'z').to_a %}

{% for char in alpha %}
  puts {{ char }}
{% end %}
Error: range begin must be a NumberLiteral, not CharLiteral

Workaround

One could define the range of Chars as a range of bytes. However, most people would not recognize (0x41..0x5a) or (0x61..7a) as A-Z and a-z, so the readability of the code would be decreased.

@straight-shoota
Copy link
Member

I can probably guess on your use case by now, but please provide a convincing practical example why that would be useful.

@postmodern
Copy link
Contributor Author

  {% ascii   = ('\u{00}'..'\u{255}') %}
  {% alpha   = ('a'..'z').to_a + ('A'..'Z').to_a %}
  {% numeric = ('0'..'9').to_a %}
  
  ...
  
    {% separators = ['_', '-'] %}

    {% for char in (ascii - alpha - numeric - separators) %}
      context "when the given String contains a #{ {{ char }}.inspect } character" do
        let(string) { "Foo#{ {{ char.id }} }Bar" }

        it do
          expect {
            subject.underscore(string)
          }.to raise_error(ArgumentError,"cannot convert string to underscored: #{string.inspect}")
        end
      end
    {% end %}

@straight-shoota
Copy link
Member

That's a use case, but sorry I don't think it's a very convincing one.
There is so much logic involved in building the test cases that it can't really serve the testing purpose very reliably. You have to be sure that the macro expressions work correctly as you intend them to do. But they're not validated. They could be completely wrong and run invalid test cases that wouldn't catch bugs in the tested method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants