From 482c6f634074ace9b66c1be22b089b08fc041774 Mon Sep 17 00:00:00 2001 From: FelisCatus Date: Thu, 17 Mar 2016 18:44:14 +0800 Subject: [PATCH] Add test to WeekdayCondition and TimeCondition --- omega-pac/package.json | 1 + omega-pac/src/conditions.coffee | 7 +- omega-pac/test/conditions.coffee | 123 ++++++++++++++++++++++++++++++- 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/omega-pac/package.json b/omega-pac/package.json index 078ccc8..b772d4b 100644 --- a/omega-pac/package.json +++ b/omega-pac/package.json @@ -15,6 +15,7 @@ "grunt-contrib-watch": "^0.6.1", "grunt-mocha-test": "~0.11.0", "load-grunt-config": "^0.13.1", + "lolex": "^1.4.0", "minifyify": "^4.1.1" }, "dependencies": { diff --git a/omega-pac/src/conditions.coffee b/omega-pac/src/conditions.coffee index d9a2b07..cf75359 100644 --- a/omega-pac/src/conditions.coffee +++ b/omega-pac/src/conditions.coffee @@ -84,7 +84,7 @@ module.exports = exports = return exports._abbrs[abbr.toUpperCase()] comment: (comment, node) -> - return unless comment + return node unless comment node.start ?= {} # This hack is needed to allow dumping comments in repeated print call. Object.defineProperty node.start, '_comments_dumped', @@ -123,8 +123,10 @@ module.exports = exports = return exports.comment comment, new U2.AST_Binary( left: val operator: '===' - right: new U2.AST_Number value: min + right: min ) + if min > max + return exports.comment comment, new U2.AST_False if exports.isInt(min) and exports.isInt(max) and max - min < 32 comment ||= "#{min} <= value && value <= #{max}" tmpl = "0123456789abcdefghijklmnopqrstuvwxyz" @@ -609,6 +611,7 @@ module.exports = exports = condition.startDay = 0 unless 0 <= condition.startDay <= 6 condition.endDay = 0 unless 0 <= condition.endDay <= 6 condition + 'TimeCondition': abbrs: ['T', 'Time', 'Hour'] analyze: (condition) -> null diff --git a/omega-pac/test/conditions.coffee b/omega-pac/test/conditions.coffee index cc91bb8..7f2eaaa 100644 --- a/omega-pac/test/conditions.coffee +++ b/omega-pac/test/conditions.coffee @@ -1,5 +1,6 @@ chai = require 'chai' should = chai.should() +lolex = require 'lolex'; describe 'Conditions', -> Conditions = require '../src/conditions' @@ -11,7 +12,7 @@ describe 'Conditions', -> request = Conditions.requestFromUrl(request) matchResult = Conditions.match(condition, request) - condExpr = Conditions.compile(condition, request) + condExpr = Conditions.compile(condition) testFunc = new U2.AST_Function( argnames: [ new U2.AST_SymbolFunarg name: 'url' @@ -31,7 +32,6 @@ describe 'Conditions', -> printCond = JSON.stringify(condition) printCompiled = if compiled then 'COMPILED ' else '' printMatch = if should_match then 'to match' else 'not to match' - console.log(request) msg = ("expect #{printCompiled}condition #{printCond} " + "#{printMatch} request #{o_request}") chai.assert(false, msg) @@ -349,6 +349,124 @@ describe 'Conditions', -> testCond(cond, 'https://example.com/', not 'match') testCond(cond, 'https://example.net/', not 'match') + describe 'WeekdayCondition', -> + clock = null + before -> + clock = lolex.install 0, ['Date'] + after -> + clock.uninstall() + + testCondDay = (cond, day, match) -> + # Feb 2016 Calendar for testing: + # Su Mo Tu We Th Fr Sa + # .. 01 02 03 04 05 06 + # 07 08 09 10 11 12 13 + # (...) + date = if day > 0 then day else 7 + clock.setSystemTime(new Date("2016-02-0#{date}T00:00:00Z").getTime()) + testCond(cond, "http://weekday-#{day}/", match) + + it 'should match requests based on date range', -> + cond = + conditionType: 'WeekdayCondition' + startDay: 3 + endDay: 5 + + testCondDay(cond, 0, not 'match') + testCondDay(cond, 1, not 'match') + testCondDay(cond, 2, not 'match') + testCondDay(cond, 3, 'match') + testCondDay(cond, 4, 'match') + testCondDay(cond, 5, 'match') + testCondDay(cond, 6, not 'match') + + it 'should match the day if startDay == endDay', -> + cond = + conditionType: 'WeekdayCondition' + startDay: 3 + endDay: 3 + + testCondDay(cond, 0, not 'match') + testCondDay(cond, 1, not 'match') + testCondDay(cond, 2, not 'match') + testCondDay(cond, 3, 'match') + testCondDay(cond, 4, not 'match') + testCondDay(cond, 5, not 'match') + testCondDay(cond, 6, not 'match') + + it 'should not match anything if startDay > endDay', -> + cond = + conditionType: 'WeekdayCondition' + startDay: 4 + endDay: 3 + + testCondDay(cond, 0, not 'match') + testCondDay(cond, 1, not 'match') + testCondDay(cond, 2, not 'match') + testCondDay(cond, 3, not 'match') + testCondDay(cond, 4, not 'match') + testCondDay(cond, 5, not 'match') + testCondDay(cond, 6, not 'match') + + describe 'TimeCondition', -> + clock = null + before -> + clock = lolex.install 0, ['Date'] + after -> + clock.uninstall() + + testCondTime = (cond, time, match) -> + # This uses RFC2822 format to make it in local time zone. + # ISO-8601 should be avoided because ES5 says it assumes UTC. + clock.setSystemTime(new Date("01 Feb 2016 #{time}").getTime()) + testCond(cond, "http://time-#{time}/", match) + + it 'should match requests based on hour range', -> + cond = + conditionType: 'TimeCondition' + startHour: 7 + endHour: 9 + + testCondTime(cond, '00:00:00', not 'match') + testCondTime(cond, '06:00:00', not 'match') + testCondTime(cond, '07:00:00', 'match') + testCondTime(cond, '08:00:00', 'match') + testCondTime(cond, '09:00:00', 'match') + testCondTime(cond, '09:59:59', 'match') + testCondTime(cond, '10:00:00', not 'match') + testCondTime(cond, '19:00:00', not 'match') + testCondTime(cond, '23:00:00', not 'match') + + it 'should match the hour if startHour == endHour', -> + cond = + conditionType: 'TimeCondition' + startHour: 7 + endHour: 7 + + testCondTime(cond, '00:00:00', not 'match') + testCondTime(cond, '06:00:00', not 'match') + testCondTime(cond, '07:00:00', 'match') + testCondTime(cond, '07:00:01', 'match') + testCondTime(cond, '07:59:59', 'match') + testCondTime(cond, '08:00:00', not 'match') + testCondTime(cond, '19:00:00', not 'match') + + it 'should not match anything if startHour > endHour', -> + cond = + conditionType: 'TimeCondition' + startHour: 7 + endHour: 6 + + testCondTime(cond, '00:00:00', not 'match') + testCondTime(cond, '06:00:00', not 'match') + testCondTime(cond, '06:59:59', not 'match') + testCondTime(cond, '07:00:00', not 'match') + testCondTime(cond, '08:00:00', not 'match') + testCondTime(cond, '09:00:00', not 'match') + testCondTime(cond, '10:00:00', not 'match') + testCondTime(cond, '19:00:00', not 'match') + testCondTime(cond, '23:00:00', not 'match') + describe '#typeFromAbbr', -> it 'should get condition types by abbrs', -> Conditions.typeFromAbbr('True').should.equal('TrueCondition') @@ -409,7 +527,6 @@ describe 'Conditions', -> result = Conditions.str(condition) result.should.equal('Ip: 127.0.0.1/16') cond = Conditions.fromStr(result) - console.log typeof cond.prefixLength cond.should.eql(condition) it 'should provide sensible fallbacks for invalid IpCondition', -> cond = Conditions.fromStr('Ip: foo/-233')