-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/functional/regressions/test_issue205_skipws_propagation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from __future__ import unicode_literals | ||
from textx.metamodel import metamodel_from_str | ||
|
||
|
||
def test_issue205_skipws_propagation1(): | ||
""" | ||
Here, we show how the ```skipws``` rule modifier is propagated | ||
to all rules used by the rule with the modifier. | ||
""" | ||
mm = metamodel_from_str(''' | ||
SGroup[skipws]: '<s>' Sentence '</s>'; // skipws active | ||
Sentence: /(?i) */ words*=Word /(?i) */; // skipws active <- SGroup | ||
Word: /(?i) */ ('bar' '.' | 'bar' | '.'); // skipws active <- SGroup | ||
''', skipws=False, debug=False) | ||
|
||
m = mm.model_from_str('''<s> bar . </s>''', debug=False) | ||
assert 1 == len(m.words) | ||
assert 'bar.' == m.words[0] | ||
|
||
|
||
def test_issue205_skipws_propagation2(): | ||
""" | ||
Here, we show how the default ```skipws``` rule modifier is used | ||
as default (because no modifier is explicitly specified). | ||
""" | ||
mm = metamodel_from_str(''' | ||
SGroup: '<s>' Sentence '</s>'; | ||
Sentence: /(?i) */ words*=Word /(?i) */; | ||
Word: /(?i) */ ('bar' '.' | 'bar' | '.'); | ||
''', | ||
skipws=False, # default skipws value | ||
debug=False) | ||
|
||
assert 2 == len(mm.model_from_str('''<s> bar . </s>''', debug=False).words) | ||
assert 1 == len(mm.model_from_str('''<s> bar. </s>''', debug=False).words) | ||
|
||
|
||
def test_issue205_skipws_propagation3(): | ||
""" | ||
Here, we show how a given ```skipws``` rule modifier is overridden | ||
by another explicitly specified rule modifier, which stops the | ||
propagation: the ```skipws``` rule modifier in ```SGroup``` is | ||
overridden by the ```noskipws``` rule modifier of the | ||
rule ```Sentence``` (used in ```SGroup```). | ||
""" | ||
mm = metamodel_from_str(''' | ||
SGroup[skipws]: '<s>' Sentence '</s>'; | ||
Sentence[noskipws]: /(?i) */ words*=Word /(?i) */; | ||
Word: /(?i) */ ('bar' '.' | 'bar' | '.'); | ||
''', skipws=False, debug=False) | ||
|
||
assert 2 == len(mm.model_from_str('''<s> bar . </s>''', debug=False).words) | ||
assert 1 == len(mm.model_from_str('''<s> bar. </s>''', debug=False).words) | ||
assert 1 == len(mm.model_from_str('''<s> bar </s>''', debug=False).words) | ||
assert 1 == len(mm.model_from_str('''<s> . </s>''', debug=False).words) | ||
|
||
|
||
def test_issue205_skipws_propagation4(): | ||
""" | ||
Here, we show that a rule with the ```noskipws``` rule modifier | ||
is able to consume spaces explicitly. Match suppression (-) is | ||
employed to exclude these spaces from the target value (this means the | ||
```Word``` does not include the spaces in its value). | ||
Note: ```skipws``` of Sentence does not consume the spaces between | ||
the words, because the [rule modifiers] | ||
(http://textx.github.io/textX/stable/grammar/#rule-modifiers) | ||
apply immediately. Thus, we need to consume the extra spaces before a word. | ||
""" | ||
mm = metamodel_from_str(r''' | ||
Sentence[skipws]: words*=Word; | ||
Word[noskipws]: /\s*/- ((ID '.') | ID | '.'); | ||
''', skipws=True) | ||
|
||
m = mm.model_from_str('''foo bar .''') | ||
assert "foo" == m.words[0] | ||
assert "bar" == m.words[1] | ||
assert 3 == len(m.words) | ||
assert "." == m.words[2] | ||
|
||
m = mm.model_from_str('''foo bar.''') | ||
assert "foo" == m.words[0] | ||
assert "bar." == m.words[1] | ||
assert 2 == len(m.words) |