def test_super(self): @specs.parameter('string', yaqltypes.String()) @specs.inject('base', yaqltypes.Super()) def len2(string, base): return 2 * base(string) context = self.context.create_child_context() context.register_function(len2, name='len') self.assertEqual(6, self.eval('len(abc)', context=context))
def inject_method_with_yaql_expr(context, target, target_method, expr): ctx_manager = helpers.get_executor(context).context_manager original_class = target.type original_function = original_class.find_single_method(target_method) result_fd = original_function.instance_stub.clone() def payload_adapter(__super, __context, __sender, *args, **kwargs): new_context = context.create_child_context() new_context[constants.CTX_ORIGINAL_CONTEXT] = __context mock_obj = context[constants.CTX_THIS] new_context.register_function(lambda: __super(*args, **kwargs), name='originalMethod') return expr(new_context, mock_obj, *args, **kwargs) result_fd.payload = payload_adapter result_fd.insert_parameter('__super', yaqltypes.Super()) existing_mocks = ctx_manager.class_mock_ctx.setdefault( original_class.name, []) existing_mocks.append(result_fd)
@specs.parameter('schema', Schema) @specs.method def not_null(schema): """Implementation of notNull() contract that generates schema instead""" types = schema.data.get('type') if isinstance(types, list) and 'null' in types: types.remove('null') if len(types) == 1: types = types[0] schema.data['type'] = types schema.data['_notNull'] = True return schema @specs.inject('up', yaqltypes.Super()) @specs.name('#finalize') def finalize(obj, up): """Wrapper around YAQL contracts that removes temporary schema data""" res = up(obj) if isinstance(res, Schema): res = res.data if isinstance(res, dict): res.pop('_notNull', None) return res @specs.parameter('expr', yaqltypes.YaqlExpression()) @specs.parameter('schema', Schema) @specs.method def check(schema, expr, engine, context):
@specs.name('#operator_.') def obj_attribution(context, obj, property_name): return obj.get_property(property_name, context) @specs.parameter('cls', dsl_types.MuranoTypeReference) @specs.parameter('property_name', yaqltypes.Keyword()) @specs.name('#operator_.') def obj_attribution_static(context, cls, property_name): return helpers.get_executor().get_static_property(cls.type, property_name, context) @specs.parameter('receiver', dsl.MuranoObjectParameter(decorate=False)) @specs.parameter('expr', yaqltypes.Lambda(method=True)) @specs.inject('operator', yaqltypes.Super(with_context=True)) @specs.name('#operator_.') def op_dot(context, receiver, expr, operator): executor = helpers.get_executor() type_context = executor.context_manager.create_type_context(receiver.type) obj_context = executor.context_manager.create_object_context(receiver) ctx2 = helpers.link_contexts(helpers.link_contexts(context, type_context), obj_context) return operator(ctx2, receiver, expr) @specs.parameter('receiver', dsl_types.MuranoTypeReference) @specs.parameter('expr', yaqltypes.Lambda(method=True)) @specs.inject('operator', yaqltypes.Super(with_context=True)) @specs.name('#operator_.') def op_dot_static(context, receiver, expr, operator):
from yaql.language import specs from yaql.language import utils from yaql.language import yaqltypes @specs.parameter('left', yaqltypes.YaqlExpression()) @specs.name('#operator_=>') def build_tuple(left, right, context, engine): if isinstance(left, expressions.BinaryOperator) and left.operator == '=>': return left(utils.NO_VALUE, context, engine) + (right, ) else: return left(utils.NO_VALUE, context, engine), right @specs.parameter('tuples', tuple) @specs.inject('delegate', yaqltypes.Super(with_name=True)) @specs.no_kwargs @specs.extension_method def dict_(delegate, *tuples): return delegate('dict', tuples) @specs.method @specs.parameter('collection', yaqltypes.Iterable()) def to_list(collection): return list(collection) def tuple_(*args): return args