From 680baf846ec4c4ab127c37ca0c31648e042ae92d Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 19 May 2016 02:18:31 +0900 Subject: [PATCH] add codegen tests (#2894) ref #2872 --- test/unit/modules/compiler/codegen.spec.js | 283 +++++++++++++++++++++ 1 file changed, 283 insertions(+) diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js index e69de29b..f765b8bd 100644 --- a/test/unit/modules/compiler/codegen.spec.js +++ b/test/unit/modules/compiler/codegen.spec.js @@ -0,0 +1,283 @@ +import { parse } from 'compiler/parser/index' +import { optimize } from 'compiler/optimizer' +import { generate } from 'compiler/codegen' +import directives from 'web/compiler/directives/index' +import { isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from 'web/util/index' +import { isObject } from 'shared/util' + +/* eslint-disable quotes */ +describe('codegen', () => { + const baseOptions = { + expectHTML: true, + preserveWhitespace: true, + directives, + isReservedTag, + isUnaryTag, + mustUseProp, + getTagNamespace + } + + function assertCodegen (template, generatedCode, ...args) { + let staticRenderFnCodes = [] + let generateOptions = baseOptions + let proc = null + let len = args.length + while (len--) { + const arg = args[len] + if (Array.isArray(arg)) { + staticRenderFnCodes = arg + } else if (isObject(arg)) { + generateOptions = arg + } else if (typeof arg === 'function') { + proc = arg + } + } + + const ast = parse(template, baseOptions) + optimize(ast, baseOptions) + proc && proc(ast) + const res = generate(ast, generateOptions) + expect(res.render).toBe(generatedCode) + expect(res.staticRenderFns).toEqual(staticRenderFnCodes) + } + + it('generate directive', () => { + assertCodegen( + '

', + `with (this) { return __r__(__s__('p', {directives:[{name:"custom1",value:(value1),arg:"arg1",modifiers:{"modifire":true}},{name:"custom2",arg:"arg1"}]}), undefined)}` + ) + }) + + it('generate v-pre', () => { + assertCodegen( + '

hello world

', + 'with (this) { return __m__(0)}', + [`with(this){return __r__(__s__('div', {pre:true}), [__r__(__s__('p', {}), [__t__("hello world")])])}`] + ) + }) + + it('generate v-for directive', () => { + assertCodegen( + '
  • ', + `with (this) { return (items)&&__renderList__((items), function(item,$index,$key){return __r__(__s__('li', {key:uid}), undefined)})}` + ) + }) + + it('generate v-if directive', () => { + assertCodegen( + '

    hello

    ', + `with (this) { return (show) ? __r__(__s__('p', undefined), [__t__("hello")]) : null}` + ) + }) + + it('generate v-else directive', () => { + assertCodegen( + '

    hello

    world

    ', + `with (this) { return __r__(__s__('div', undefined), [(show) ? __r__(__s__('p', undefined), [__t__("hello")]) : __r__(__s__('p', undefined), [__t__("world")])])}` + ) + }) + + it('generate v-ref directive', () => { + assertCodegen( + '

    ', + `with (this) { return __r__(__s__('p', {hook:{"insert":function(n1,n2){__registerRef__("component1", n1.child || n1.elm, false), false},"destroy":function(n1,n2){__registerRef__("component1", n1.child || n1.elm, false, true)}}}), undefined)}` + ) + }) + + it('generate v-ref directive on v-for', () => { + assertCodegen( + '', + `with (this) { return __r__(__s__('ul', undefined), [(items)&&__renderList__((items), function(item,$index,$key){return __r__(__s__('li', {hook:{"insert":function(n1,n2){__registerRef__("component1", n1.child || n1.elm, true), false},"destroy":function(n1,n2){__registerRef__("component1", n1.child || n1.elm, true, true)}}}), undefined)})])}` + ) + }) + + it('generate template tag', () => { + assertCodegen( + '', + `with (this) { return [__r__(__s__('p', undefined), [__t__("hello world")])]}` + ) + }) + + it('generate svg tag', () => { + assertCodegen( + 'hello world', + `with (this) { return __r__(__s__('svg', undefined,'svg'), function(){return [__r__(__s__('text', undefined,'svg'), function(){return [__t__("hello world")]})]})}` + ) + }) + + it('generate render tag', () => { + assertCodegen( + '', + `with (this) { return onRender(params,undefined)}` + ) + }) + + it('generate render tag that have childs', () => { + assertCodegen( + '

    hello

    ', + `with (this) { return onRender(null,[__m__(0)])}`, + [`with(this){return __r__(__s__('p', undefined), [__t__("hello")])}`] + ) + }) + + it('generate single slot', () => { + assertCodegen( + '', + `with (this) { return ($slots["default"] || undefined)}` + ) + }) + + it('generate named slot', () => { + assertCodegen( + '', + `with (this) { return ($slots["one"] || undefined)}` + ) + }) + + it('generate slot target', () => { + assertCodegen( + '

    hello world

    ', + `with (this) { return __r__(__s__('p', {slot:"one"}), [__t__("hello world")])}` + ) + }) + + it('generate class binding', () => { + // static + assertCodegen( + '

    hello world

    ', + 'with (this) { return __m__(0)}', + [`with(this){return __r__(__s__('p', {staticAttrs:{"class":"class1"}}), [__t__("hello world")])}`] + ) + // dynamic + assertCodegen( + '

    hello world

    ', + `with (this) { return __r__(__s__('p', {attrs:{"class":class1}}), [__t__("hello world")])}` + ) + }) + + it('generate style binding', () => { + assertCodegen( + '

    hello world

    ', + `with (this) { return __r__(__s__('p', {attrs:{"style":error}}), [__t__("hello world")])}` + ) + }) + + it('generate transition', () => { + assertCodegen( + '

    hello world

    ', + 'with (this) { return __m__(0)}', + [`with(this){return __r__(__s__('p', {staticAttrs:{"transition":"expand"}}), [__t__("hello world")])}`] + ) + }) + + it('generate v-show directive', () => { + assertCodegen( + '

    hello world

    ', + `with (this) { return __r__(__s__('p', {directives:[{name:"show",value:(shown)}],show:true}), [__t__("hello world")])}` + ) + }) + + it('generate props with v-bind directive', () => { + assertCodegen( + '

    ', + `with (this) { return __r__(__s__('p', {props:{"value":msg}}), undefined)}` + ) + }) + + it('generate attrs with v-bind directive', () => { + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {attrs:{"name":field1}}), undefined)}` + ) + }) + + it('generate staticAttrs', () => { + assertCodegen( + '', + `with (this) { return __m__(0)}`, + [`with(this){return __r__(__s__('input', {staticAttrs:{"name":"field1"}}), undefined)}`] + ) + }) + + it('generate events with v-on directive', () => { + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":onInput}}), undefined)}` + ) + // keycode + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}}), undefined)}` + ) + // multiple keycode + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}}), undefined)}` + ) + // inline statement + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":function($event){curent++}}}), undefined)}` + ) + // unhandled + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":function(){}}}), undefined)}`, + ast => { + ast.events.input = undefined + } + ) + // multiple handlers + assertCodegen( + '', + `with (this) { return __r__(__s__('input', {on:{"input":[function($event){curent++},onInput]}}), undefined)}` + ) + }) + + it('generate component', () => { + assertCodegen( + '', + `with (this) { return __r__(__s__('my-component', {attrs:{"msg":msg},staticAttrs:{"name":"mycomponent1"},on:{"notify":onNotify}}), undefined)}` + ) + // have "is" attribute + assertCodegen( + '', + `with (this) { return __r__(__s__("component1", {}), undefined)}` + ) + // have "inline-template'" + assertCodegen( + '

    hello world

    ', + `with (this) { return __r__(__s__('my-component', {inlineTemplate:{render:function(){with (this) { return __m__(0)}},staticRenderFns:[function(){with(this){return __r__(__s__('p', undefined), [__t__("hello world")])}}]}}), undefined)}` + ) + // "have inline-template attrs, and not have child + assertCodegen( + '

    ', + `with (this) { return __r__(__s__('my-component', {inlineTemplate:{render:function(){with (this) { return __m__(0)}},staticRenderFns:[function(){with(this){return __r__(__s__('hr', undefined), undefined)}}]}}), undefined)}` + ) + expect('Inline-template components must have exactly one child element.').toHaveBeenWarned() + }) + + it('not specified ast type', () => { + const res = generate(null, baseOptions) + expect(res.render).toBe(`with (this) { return __r__(__s__("div"))}`) + expect(res.staticRenderFns).toEqual([]) + }) + + it('not specified directives option', () => { + assertCodegen( + '

    hello world

    ', + `with (this) { return (show) ? __r__(__s__('p', undefined), [__t__("hello world")]) : null}`, + { isReservedTag } + ) + }) + + it('not specified isReservedTag option', () => { + assertCodegen( + '

    hello world

    ', + `with (this) { return __m__(0)}`, + [`with(this){return __r__(__s__('div', undefined), function(){return [__r__(__s__('p', undefined), function(){return [__t__("hello world")]})]})}`], + { directives } + ) + }) +}) +/* eslint-enable quotes */