Skip to content

Commit

Permalink
repr for QgsDefaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 11, 2021
1 parent 3a4867d commit 66bd635
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsdefaultvalue.sip.in
Expand Up @@ -47,6 +47,15 @@ Create a new default value with the given ``expression`` and ``applyOnUpdate`` f

bool operator==( const QgsDefaultValue &other ) const;

SIP_PYOBJECT __repr__();
%MethodCode
const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsDefaultValue: %1>" ).arg(
sipCpp->expression().length() > 1000 ? sipCpp->expression().left( 1000 ) + QStringLiteral( "..." )
: sipCpp->expression() )
: QStringLiteral( "<QgsDefaultValue: invalid>" );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

QString expression() const;
%Docstring
The expression will be evaluated whenever a default value needs
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsdefaultvalue.h
Expand Up @@ -63,6 +63,17 @@ class CORE_EXPORT QgsDefaultValue
// TODO c++20 - replace with = default
bool operator==( const QgsDefaultValue &other ) const;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsDefaultValue: %1>" ).arg(
sipCpp->expression().length() > 1000 ? sipCpp->expression().left( 1000 ) + QStringLiteral( "..." )
: sipCpp->expression() )
: QStringLiteral( "<QgsDefaultValue: invalid>" );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

/**
* The expression will be evaluated whenever a default value needs
* to be calculated for a field.
Expand Down
7 changes: 6 additions & 1 deletion tests/src/python/test_python_repr.py
Expand Up @@ -59,7 +59,8 @@
QgsMeshLayer,
QgsDataSourceUri,
QgsDoubleRange,
QgsIntRange
QgsIntRange,
QgsDefaultValue
)

start_app()
Expand Down Expand Up @@ -331,6 +332,10 @@ def testIntRange(self):
self.assertEqual(QgsIntRange(1, 10, True, False).__repr__(),
"<QgsIntRange: [1, 10)>")

def testDefaultValue(self):
self.assertEqual(QgsDefaultValue().__repr__(), '<QgsDefaultValue: invalid>')
self.assertEqual(QgsDefaultValue('1+3').__repr__(), '<QgsDefaultValue: 1+3>')


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

0 comments on commit 66bd635

Please sign in to comment.