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

Context based definition of an example to make easier to write examples that expect empty lines #250

Open
eldipa opened this issue Jun 1, 2022 · 0 comments
Labels
enhancement something nice to have but it is not neither critical nor urgent far in the future something that it could be cool but it is too hard to implement request for comments a draft idea that needs more brainstorming, comments are welcome!

Comments

@eldipa
Copy link
Collaborator

eldipa commented Jun 1, 2022

Describe the feature you'd like
When an example's expected output has an empty line, the user must (1) not write an empty line and use +norm-ws or (2) write a dummy character like ~ in the empty line and delete it before the comparison with the obtained output with +rm=~

byexample defines an example's expected output from the first line after the example's code/snippet and until the line where another prompt for the same language begins or until an empty line.

So the following works (byexample -l python,shell):

>>> print("Hello world")
Hello world

$  echo -e "Hello world"
Hello world

However the following don't

>>> print("Hello\n\nworld")
Hello

world

$  echo -e "Hello\n\nworld"
Hello

world

In order to make it work we can use +norm-ws or +rm

>>> print("Hello\n\nworld")   # byexample: +rm=~
Hello
~
world

$  echo -e "Hello\n\nworld"   # byexample: +norm-ws
Hello world

The proposed feature is to use the context of the example to hint byexample to where the example really ends.

First, if another runner is enabled, the prompt of it should mark as the end of the former example:

For example the following does not work because the $ echo..... is interpreted as part of the python example's expected output:

>>> print("Hello world")
Hello world
$  echo -e "Hello world"
Hello world

With the proposed feature, the example above should work as $ works as the end of the python example.

For a fenced-code block based zones, the end of the zone is clearly another end-marker. The following example will work

>>> print("Hello\n\nworld")
Hello

world

$  echo -e "Hello\n\nworld"
Hello

world

Here the python example will expect an empty new line after the world line but because byexample currently removes it, it should not bring any trouble (however this depends on +term).

The shell example ends at the line where the zone ends (the end of the fenced-code block)

For non-fenced-code blocks, a zone finder could use indentation

Imagine some human explanation here...
  >>> print("Hello\n\nworld") 
  Hello

  world

An another human paragraph here...
  $  echo -e "Hello\n\nworld"
  Hello

  world
And this line is another paragraph...

Notice that the indentation of the snippet defines the indentation of the output. Also note that empty lines like the above between "Hello" and "world" may not be indented but they should be part of the output.

Note also the cases where the line is not really empty but it is semantically empty like here (Ruby example):

# Ruby example bla bla bla
#     >> puts "Hello\n\nworld"
#     Hello
#
#     world
# Another paragraph here

Describe alternatives you've considered (optional)

And EOF marker could be used but it looks weird.

Additional context (optional)

This feature breaks assumptions that are deeply inside byexample since its root. An ExampleFinder uses a regex to find an example and it uses an empty line and its own prompt as end-markers.

Changing that will require that the regex knows about others' prompts so this is much complicated. If we don't stop at the begin of another prompt, a fenced-code based zone where two or more languages are mixed will break:

>>> print("Hello world")
Hello world

$  echo -e "Hello world"
Hello world

In the above case, the python example would span until the end of the zone.

Because this feature could potentially break a lot of examples, it should be enabled optionally. Moreover, this requires supports from each ExampleFinder (regex) and ZonerFinder (to decide if it is fenced-code or indentation based) and probably from ExampleParser (to strip possibly the indentation)

@eldipa eldipa added enhancement something nice to have but it is not neither critical nor urgent request for comments a draft idea that needs more brainstorming, comments are welcome! far in the future something that it could be cool but it is too hard to implement labels Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement something nice to have but it is not neither critical nor urgent far in the future something that it could be cool but it is too hard to implement request for comments a draft idea that needs more brainstorming, comments are welcome!
Projects
None yet
Development

No branches or pull requests

1 participant