-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestinit.js
138 lines (116 loc) · 3.76 KB
/
testinit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Utilities */
function renderColors(colors, target, label) {
target = target || 'body';
$(function () {
var i,
$swatches = $('<div>').addClass('test-colorFactory-swatches');
for (i = 0; i < colors.length; i += 1) {
$('<div class="test-colorFactory-swatch"></div>')
.css('backgroundColor', colors[colors.length - i - 1])
.appendTo($swatches);
}
$('<div class="test-colorFactor-holder"></div>').append(
$swatches,
$('<div>').text(label).addClass('test-colorFactory-label')
).appendTo(target);
});
}
/* Custom tests */
QUnit.colorTest = function (title, actualColors, callback) {
return QUnit.test(title, function () {
this.renderColors = function (colors, label) {
renderColors(colors, '#qunit-test-output-' + QUnit.config.current.testId, label);
};
this.renderColors(actualColors, 'actual');
this.colors = actualColors;
return callback.apply(this, arguments);
});
};
QUnit.colorTest.closeMatch = function (title, actualColors, expectedColors, threshold) {
return QUnit.colorTest(title, actualColors, function (assert) {
this.renderColors(expectedColors, 'match');
assert.strictEqual(actualColors.length, expectedColors.length, 'validate array size');
for (var i = 0, len = expectedColors.length; i < len; i += 1) {
assert.colorIsVisuallyClose(actualColors[i], expectedColors[i], threshold);
}
});
};
QUnit.colorTest.complementary = function (input, expected) {
return QUnit.colorTest.closeMatch('complementaryColors() for ' + input,
ColorFactory.complementary(input),
[expected, input]
);
};
// The test for binary() is basically the same as for complementary(),
// both take one value and add one to it. Except that
// complementary() sets the input last, and binary() sets it first.
QUnit.colorTest.binary = function (message, input, expected, threshold) {
return QUnit.colorTest.closeMatch(message,
ColorFactory.binary(input),
[input, expected],
threshold
);
};
QUnit.colorTest.distinguishable = function (title, colors, callback) {
return QUnit.colorTest(title, colors, function (assert) {
assert.colorIsDistinguishable(colors);
if (callback) {
callback.apply(this, arguments);
}
});
};
/* Custom assertions */
QUnit.assert.match = function (actual, regex, message) {
this.pushResult( {
result: regex.test(actual),
actual: actual,
expected: regex,
message: message
} );
};
QUnit.assert.colorIsDistinguishable = function (colors) {
var i, c0, c1, hsl0, hsl1, diff;
if (colors.length < 2) {
this.pushFailure('assert.beDistinguishable expects two or more colors.');
return;
}
for (i = 0; i < (colors.length - 1); i++) {
c0 = colors[i];
c1 = colors[i + 1];
hsl0 = ColorHelper.rgbToHSL(c0);
hsl1 = ColorHelper.rgbToHSL(c1);
diff = Math.abs(hsl0[0] - hsl1[0]) + Math.abs(hsl0[1] - hsl1[1]) + Math.abs(hsl0[2] - hsl1[2]);
this.pushResult( {
result: diff > 30,
actual: diff,
expected: '> 30',
message: 'color #' + i + ' ' + c0 +
' (' + hsl0.join(',') + ') is distinguishable from ' +
'color #' + (i + 1) + ' ' + c1 + ' (' + hsl1.join(',') + ') ' +
'in saturation and lightness'
} );
}
};
QUnit.assert.colorIsVisuallyClose = function (actual, expected, threshold) {
threshold = threshold || 8;
var d,
expectedHsl = ColorHelper.rgbToHSL(expected),
actualHsl = ColorHelper.rgbToHSL(actual),
diffTotal = 0,
diff = [];
for (d in actualHsl) {
diff.push(Math.abs(expectedHsl[d] - actualHsl[d]));
}
for (d in diff) {
diffTotal += diff[d];
if (d === 0) {
diffTotal /= 3.6;
}
}
this.pushResult( {
result: diffTotal < (threshold * 3),
actual: diffTotal + ' (differed HSV: [' + diff.join(',') + '])',
expected: diffTotal + ' < (threshold * 3)',
message: 'color ' + actual + ' is visually close to ' + expected + ' (within a threshold of: ' + threshold + ')'
} );
};