• R/O
  • SSH

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2


Commit MetaInfo

Revisioncb6ed189fcdc464830d380c22479d2a9224d3f4e (tree)
Zeit2021-12-09 21:49:50
AutorAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Log Message

more unit-tests & fixes

Ändern Zusammenfassung

Diff

diff -r 4f710df67cec -r cb6ed189fcdc AST/castle/_base.py
--- a/AST/castle/_base.py Wed Dec 08 23:58:06 2021 +0100
+++ b/AST/castle/_base.py Thu Dec 09 13:49:50 2021 +0100
@@ -9,4 +9,19 @@
99 @property
1010 def position_end(self): return self.parse_tree.position_end
1111
12-class ID(str): pass
12+
13+class IDError(ValueError):
14+ "The given ID is not valid as an ID"
15+
16+import re
17+
18+class ID(str):
19+ _pattern = re.compile(r'[A-Za-z_][A-Za-z0-9_]*')
20+
21+ @staticmethod
22+ def validate_or_raise(value):
23+ if not isinstance(value, str):
24+ raise IDError("not a str of ID")
25+ if ID._pattern.fullmatch(value) is None:
26+ raise IDError("not a valid pattern")
27+
diff -r 4f710df67cec -r cb6ed189fcdc AST/castle/peg.py
--- a/AST/castle/peg.py Wed Dec 08 23:58:06 2021 +0100
+++ b/AST/castle/peg.py Thu Dec 09 13:49:50 2021 +0100
@@ -25,8 +25,9 @@
2525 class Setting(PEG):
2626 def __init__(self, *,
2727 name: ID,
28- value=None,
28+ value,
2929 **kwargs):
30+ ID.validate_or_raise(name)
3031 super().__init__(**kwargs)
3132 self.name = name
3233 self.value = value
@@ -38,6 +39,7 @@
3839 name: ID,
3940 value :Expression,
4041 **kwargs):
42+ ID.validate_or_raise(name)
4143 super().__init__(**kwargs)
4244 self.name = name
4345 self.value = value
diff -r 4f710df67cec -r cb6ed189fcdc AST/pytst/test_1_Settings.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AST/pytst/test_1_Settings.py Thu Dec 09 13:49:50 2021 +0100
@@ -0,0 +1,21 @@
1+import pytest
2+
3+from castle.peg import Setting
4+
5+def test_a_ID():
6+ a_name, a_val = 'aName', 42
7+ s=Setting(name=a_name, value=a_val)
8+ assert s.name == a_name, "Remember the ID"
9+ assert s.value == a_val, "Remember the value"
10+
11+
12+def test_needID():
13+ with pytest.raises(ValueError):
14+ Setting(name=42, value="the name should be an ID, or string")
15+ with pytest.raises(ValueError):
16+ Setting(name='a b', value="a space in not allowed in an ID")
17+
18+def test_needID():
19+ with pytest.raises(TypeError):
20+ Setting(name='Forgot_a_Value')
21+