def _parseNodeTestSX(self, parser, inputXS, expectedSX, ignoreChars=None): if ignoreChars is None: ignoreChars = self.__junk_regex__ input = DMIOReader.readFromString(inputXS) expected = DMIOReader.readFromString(expectedSX) result = parser.traceParseNode(input, ignoreChars) if not result.isValid(): print 'PARSE FAILURE while parsing', input print 'EXPECTED:' print expectedSX self._onError(result) self.assert_(result.isValid()) value = DMNode.coerce(result.value) expected = DMNode.coerce(expected) bSame = value == expected if not bSame: print 'While parsing', input print 'EXPECTED:' print expectedSX print '' print 'RESULT:' print DMIOWriter.writeAsString(value) self._onError(result) self.assert_(bSame)
def _parseNodeTest(self, parser, input, expected, ignoreChars=None): if ignoreChars is None: ignoreChars = self.__junk_regex__ result = parser.traceParseNode(input, ignoreChars) if not result.isValid(): print 'PARSE FAILURE while parsing %s' % (input, ) print 'EXPECTED:' print expected self._onError(result) self.assert_(result.isValid()) value = DMNode.coerce(result.value) expected = DMNode.coerce(expected) bSame = value == expected if not bSame: print 'While parsing', input print 'EXPECTED:' print expected print '' print 'RESULT:' print value self._onError(result) self.assert_(bSame)
def _parseRichStringTestSX(self, parser, input, expectedSX, ignoreChars=None): if ignoreChars is None: ignoreChars = self.__junk_regex__ expected = DMIOReader.readFromString(expectedSX) result = parser.traceParseListItems(input, ignoreChars) if not result.isValid(): print 'PARSE FAILURE while parsing', input print 'EXPECTED:' print expectedSX self._onError(result) self.assert_(result.isValid()) value = DMNode.coerce(result.value) expected = DMNode.coerce(expected) if result.end != len(input): print 'INCOMPLETE PARSE while parsing', input print 'Parsed %d/%d items' % (result.end, len(input)) print input[:result.end] print 'EXPECTED:' print expectedSX print 'RESULT:' print DMIOWriter.writeAsString(value) self._onError(result) bSame = value == expected if not bSame: print 'While parsing', input print 'EXPECTED:' print expectedSX print '' print 'RESULT:' print DMIOWriter.writeAsString(value) self._onError(result) self.assert_(bSame)
def test_coercion_objects(self): class MyObject(object): def __init__(self): pass q = MyObject() self._testCoerce( {1.0: q}, DictLiteral(values=[ DictKeyValuePair(key=FloatLiteral(value='1.0'), value=EmbeddedObjectExpr( embeddedValue=DMNode.embed(q))) ]))
def coerceToModel(x, coerceObj=EmbeddedObjectExpr): # Primitive values t = type(x) handler = _primitiveToMethodTypeMap.get(t) if handler is not None: return handler(x, coerceObj) # Object values if coerceObj is None: raise TypeError, 'Cannot coerce objects with an object coercion function or embedded object schema class' elif coerceObj is EmbeddedObjectExpr or coerceObj is EmbeddedObjectLiteral or coerceObj is EmbeddedObjectStmt: return coerceObj(embeddedValue=DMNode.embedIsolated(x)) else: return coerceObj(x)
def _parseRichStringTest(self, parser, input, expected, ignoreChars=None): if ignoreChars is None: ignoreChars = self.__junk_regex__ result = parser.traceParseRichStringItems(input, ignoreChars) if not result.isValid(): print 'PARSE FAILURE while parsing %s, stopped at %d: %s' % ( input, result.end, input[:result.end]) print 'EXPECTED:' print expected self._onError(result) self.assert_(result.isValid()) value = DMNode.coerce(result.value) expected = DMNode.coerce(expected) if result.end != len(input): print 'INCOMPLETE PARSE while parsing', input print 'Parsed %d/%d items' % (result.end, len(input)) print input[:result.end] print 'EXPECTED:' print expected print 'RESULT:' print value self._onError(result) bSame = value == expected if not bSame: print 'While parsing', input print 'EXPECTED:' print expected print '' print 'RESULT:' print value self._onError(result) self.assert_(bSame)
def _readInlineObjectStmt_v2(fieldValues): # Version 2 called the field 'resource', rather than 'embeddedValue' embeddedValue = fieldValues['resource'] embeddedValue = DMNode.embed(None) return EmbeddedObjectStmt(embeddedValue=embeddedValue)
def embeddedStatement(x): return Py.EmbeddedObjectStmt(embeddedValue=DMNode.embedIsolated(x))
def embeddedExpression(x): return Py.EmbeddedObjectExpr(embeddedValue=DMNode.embedIsolated(x))
def embeddedLiteral(x): return Py.EmbeddedObjectLiteral(embeddedValue=DMNode.embedIsolated(x))
def newParagraphEmbeddedObjectModel(value): embeddedValue = DMNode.embedIsolated( value ) return Schema.ParagraphEmbeddedObject( embeddedValue=embeddedValue )
def newInlineEmbeddedObjectModel(value): embeddedValue = DMNode.embedIsolated( value ) return Schema.InlineEmbeddedObject( embeddedValue=embeddedValue )