Skip to content

Commit

Permalink
Allow parsing the empty string (as None)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Jun 8, 2020
1 parent 96372af commit a5c90a0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
7 changes: 5 additions & 2 deletions OMPython/OMTypedParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ def convertTuple(t):
recordMember = delimitedList(Group(ident + Suppress('=') + omcValue))
omcRecord << Group(Suppress('record') + Suppress(fqident) + Dict(recordMember) + Suppress('end') + Suppress(fqident) + Suppress(';')).setParseAction(convertDict)

omcGrammar = omcValue + StringEnd()
omcGrammar = Optional(omcValue) + StringEnd()

omcNumber.setParseAction(convertNumbers)


def parseString(string):
return omcGrammar.parseString(string)[0]
res = omcGrammar.parseString(string)
if len(res) == 0:
return
return res[0]


if __name__ == "__main__":
Expand Down
4 changes: 4 additions & 0 deletions tests/test_OMParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def testFloat(self):
# def testDict(self):
# self.assertEqual(type(typeCheck('{"a": "b"}')), dict)

def testIdent(self):
self.assertEqual(typeCheck('blabla2'), "blabla2")
pass

def testStr(self):
pass

Expand Down
46 changes: 46 additions & 0 deletions tests/test_typedParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from builtins import int

from OMPython import OMTypedParser

import unittest

typeCheck = OMTypedParser.parseString


class TypeCheckTester(unittest.TestCase):
def testNewlineBehaviour(self):
pass

def testBoolean(self):
self.assertEqual(typeCheck('true'), True)
self.assertEqual(typeCheck('false'), False)

def testInt(self):
self.assertEqual(typeCheck('2'), 2)
self.assertEqual(type(typeCheck('1')), int)
self.assertEqual(type(typeCheck('123123123123123123232323')), int)
self.assertEqual(type(typeCheck('9223372036854775808')), int)

def testFloat(self):
self.assertEqual(type(typeCheck('1.2e3')), float)

def testIdent(self):
self.assertEqual(typeCheck('blabla2'), "blabla2")
pass

def testEmpty(self):
self.assertEqual(typeCheck(''), None)
pass

def testStr(self):
pass

def testUnStringable(self):
pass


if __name__ == '__main__':
unittest.main()

0 comments on commit a5c90a0

Please sign in to comment.