Unit tests for exception handling in MuranoPL

Change-Id: Idc7e83f3d8ce7c1e3dd763b751c9310f683ea465
This commit is contained in:
Stan Lagun 2014-07-04 04:33:45 +04:00
parent 2611893c20
commit a6d377d992
3 changed files with 108 additions and 1 deletions

View File

@ -20,7 +20,7 @@ import murano.dsl.yaql_functions as yaql_functions
class MuranoPlException(Exception):
def __init__(self, names, message, stacktrace, extra=None, cause=None):
super(MuranoPlException, self).__init__(
'{0}: {1}'.format(names, message))
'[{0}]: {1}'.format(', '.join(names), message))
if not isinstance(names, list):
names = [names]
self._names = names

View File

@ -0,0 +1,51 @@
Name: ExceptionHandling
Methods:
testThrow:
Arguments:
- enum:
Contract: $.int().notNull()
Body:
Try:
- trace('enter try')
- $.doThrow($enum)
- trace('exit try')
Catch:
- With: exceptionName
As: e
Do:
- trace($e.message)
- With: anotherExceptionName
As: e
Do:
- trace($e.message)
- trace(rethrow)
- Rethrow:
- As: e
Do:
- trace('catch all')
- trace($e.message)
Else:
- trace('else section')
Finally:
- trace('finally section')
doThrow:
Arguments:
- enum:
Contract: $.int().notNull()
Body:
- Match:
1:
- Throw: exceptionName
Message: exception message
2:
- Throw: anotherExceptionName
Message: exception message 2
3:
- Throw: thirdExceptionName
Message: exception message 3
4:
- Return:
Value: $enum

View File

@ -0,0 +1,56 @@
# Copyright (c) 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from murano.dsl import dsl_exception
from murano.tests.dsl.foundation import object_model as om
from murano.tests.dsl.foundation import test_case
class TestExceptions(test_case.DslTestCase):
def setUp(self):
super(TestExceptions, self).setUp()
self._runner = self.new_runner(om.Object('ExceptionHandling'))
def test_throw_catch(self):
self._runner.testThrow(1)
self.assertEqual(
['enter try', u'exception message', 'finally section'],
self.traces)
def test_rethrow(self):
e = self.assertRaises(
dsl_exception.MuranoPlException,
self._runner.testThrow, 2)
self.assertEqual(['anotherExceptionName'], e.names)
self.assertEqual('exception message 2', e.message)
self.assertEqual('[anotherExceptionName]: exception message 2', str(e))
self.assertEqual(
['enter try', 'exception message 2',
'rethrow', 'finally section'],
self.traces)
def test_catch_all_catch(self):
self._runner.testThrow(3)
self.assertEqual(
['enter try', 'catch all',
'exception message 3', 'finally section'],
self.traces)
def test_no_throw(self):
self._runner.testThrow(4)
self.assertEqual(
['enter try', 'exit try', 'else section', 'finally section'],
self.traces)