Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing code. #21

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ void function () {
'</ul>'
].join('\n')

//compiledTemplate
var fn1 = _.template(templateCode1)
var fn2 = _.template(templateCode2)

//template data
var templateData1 = {text: HELLO}
var templateData2 = [
Expand All @@ -42,25 +38,14 @@ void function () {
].join('\n')

describe('Util', function () {
var _trim
var _include
var _startsWith
var _endsWith
var _toTemplateId
var _toElementId
var _isTemplateCode
var _stripCommentTag

before(function () {
_trim = template.__trim
_include = template.__include
_startsWith = template.__startsWith
_endsWith = template.__endsWith
_toTemplateId = template.__toTemplateId
_toElementId = template.__toElementId
_isTemplateCode = template.__isTemplateCode
_stripCommentTag = template.__stripCommentTag
})
var _trim = template.__trim
var _include = template.__include
var _startsWith = template.__startsWith
var _endsWith = template.__endsWith
var _toTemplateId = template.__toTemplateId
var _toElementId = template.__toElementId
var _isTemplateCode = template.__isTemplateCode
var _stripCommentTag = template.__stripCommentTag

describe('_trim()', function () {
it('removes all spaces from the beginning and end of the supplied string', function () {
Expand Down Expand Up @@ -270,7 +255,8 @@ void function () {

//test data
var data, html1, html2
var _cacheTemplate, _cacheCompiledTemplate
var _cacheTemplate = template.__cacheTemplate
var _cacheCompiledTemplate = template.__cacheCompiledTemplate

//string
function clean(str) {
Expand All @@ -282,7 +268,7 @@ void function () {
delete _cacheTemplate[TEMPLATE_ID_1]
delete _cacheTemplate[TEMPLATE_ID_2]
}
function clearCompileCache() {
function clearCompiledCache() {
delete _cacheCompiledTemplate[TEMPLATE_ID_1]
delete _cacheCompiledTemplate[TEMPLATE_ID_2]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

精简初始化代码。

}
Expand All @@ -305,17 +291,13 @@ void function () {
$elem2.remove()
}

before(function () {
_cacheTemplate = template.__cacheTemplate
_cacheCompiledTemplate = template.__cacheCompiledTemplate
})
beforeEach(function () {
clearCodeCache()
clearCompileCache()
clearCompiledCache()
})
after(function () {
clearCodeCache()
clearCompileCache()
clearCompiledCache()
})

describe('template.add()', function () {
Expand All @@ -332,7 +314,7 @@ void function () {
expect(_cacheTemplate).to.deep.equal(data)
expect(_cacheCompiledTemplate).to.deep.equal({})
})
it('overwrites template while adding existed template id', function () {
it('updates cache while adding existed template id', function () {
expect(_cacheTemplate).to.deep.equal({})
data = {}

Expand All @@ -344,13 +326,15 @@ void function () {
data[TEMPLATE_ID_1] = templateCode2
expect(_cacheTemplate).to.deep.equal(data)
})
it('overwrites compiledTemplate while adding existed template id', function () {
it('updates compiled cache while adding existed template id', function () {
expect(_cacheCompiledTemplate).to.deep.equal({})

var fn1 = _.template(templateCode1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn1fn2赋值可以再晚点执行,用的时候再执行。不过没什么太大差异啦。

template.add(TEMPLATE_ID_1, templateCode1)
template.render(TEMPLATE_ID_1, templateData1)
expect(_cacheCompiledTemplate[TEMPLATE_ID_1].source).to.equal(fn1.source)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把测试用例的名字改得更准确一些。


var fn2 = _.template(templateCode2)
template.add(TEMPLATE_ID_1, templateCode2)
expect(_cacheCompiledTemplate[TEMPLATE_ID_1]).to.equal(null)
template.render(TEMPLATE_ID_1, templateData2)
Expand Down