CKStyle单元测试 #18

Closed
wangjeaf opened this Issue Sep 17, 2014 · 0 comments

Comments

Projects
None yet
1 participant
@wangjeaf
Owner

wangjeaf commented Sep 17, 2014

CKStyle Unit Test

每一个规则,都需要添加对应的单元测试

放置在test目录下的所有文件,都是单元测试用例文件(asserts.py和helper.py除外)

mocha 是CKStyle单元测试的运行器,负责运行 test目录下所有的 run.spec.js 文件,而run.spec.js则会将本目录下符合规则的所有测试用例运行,并给出运行结果

一个典型的 run.spec.js 文件内容如下:

describe('Parser', function(){
  require('../helper').simpleRunTests(__dirname);
})

NodeJS Unit Test

NodeJS单元测试必须:

1、在 exports 中加入doTest方法,并在doTest方法及其调用中编写断言

Nodejs Unit Test Demo

var Rule = require('./helper').Rule
var RuleSet = require('./helper').RuleSet

exports.doTest = function () {
    _rule()
    _getRuleSet()
}

function _getRuleSet() {
    ruleSet = new RuleSet('.selector', 'width:100px;', '/* aa */', null)
    rule = new Rule("", "", "", ruleSet)
    equal(rule.getRuleSet(), ruleSet, 'get rule set')
    equal(rule.getRuleSet().selector, '.selector', 'it is what I need')
}

function _rule() {
    rule = new Rule("   .test ", "  _width ", " 100px; ", null)
    equal(rule.selector, '.test', 'selector is ok')
    equal(rule.roughSelector, '   .test ', 'roughSelector is ok')
    equal(rule.roughName, '  _width ', 'roughName is ok')
    equal(rule.name, 'width', 'name is ok')
    equal(rule.roughValue, ' 100px; ', 'roughValue is ok')
    equal(rule.value, '100px', 'value is ok')
    equal(rule.strippedName, '_width', 'stripped name is ok')
    equal(rule.strippedValue, '100px;', 'strippedValue is ok')
}

Css Unit Test

CSS的单元测试,必须满足以下条件:

1、必须包含@unit-test-expecteds,并在此规则中写入单元测试断言

2、每一个规则由key-value组成,key为错误的errorLevel,value为错误消息

3、如果断言中有,而实际检查结果中没有,测试时将出现[expect but not have]

4、如果断言中没有,而实际检查结果中有,测试时将出现[unexpect but has]

5、一定要注意errorLevel是否正确

CSS Unit Test Demo

@unit-test-expecteds {
    1: zero should be removed when meet 0.xxx in ".test"
    1: zero should be removed when meet 0.xxx in ".test-another"
    1: zero should be removed when meet 0.xxx in ".test-padding"
}

.test {
    width: 0.1px;
}

.test-another {
    width: 0.001px;
}

.test-padding {
    padding: 1px 0.2px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment