Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Jul 19, 2015
1 parent 7b04e1c commit 2f8338e
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/common/I18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define(
this.store = appendLanguage({}, languageList);
this.setLanguage(
defaultLanguage
|| navigator && navigator.language.toLowerCase()
|| typeof navigator !== 'undefined' && navigator.language.toLowerCase()
|| 'en-us'
);
}
Expand Down
26 changes: 15 additions & 11 deletions src/common/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
define(
function (require) {


var is = {};
var toString = toString || Object.prototype.toString;

// 生成 isXXX方法
['String', 'Array', 'Function', 'Date', 'Object'].forEach(function (type) {
is['is' + type] = function (obj) {
return obj != null && toString.call(obj).slice(8, -1) === type;
};
});
function isArray(obj) {
return obj != null && toString.call(obj).slice(8, -1) === 'Array';
}

function isObject(obj) {
return obj != null && toString.call(obj).slice(8, -1) === 'Object';
}

/**
* 为函数提前绑定前置参数(柯里化)
Expand Down Expand Up @@ -164,10 +163,10 @@ define(

var cloned = source;

if (is.isArray(source)) {
if (isArray(source)) {
cloned = source.slice().map(clone);
}
else if (is.isObject(source) && 'isPrototypeOf' in source) {
else if (isObject(source) && 'isPrototypeOf' in source) {
cloned = {};
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
Expand Down Expand Up @@ -306,7 +305,12 @@ define(
debounce: debounce
};

extend(exports, is);
// 生成 isXXX方法
['String', 'Function', 'Date'].forEach(function (type) {
exports['is' + type] = function (obj) {
return obj != null && toString.call(obj).slice(8, -1) === type;
};
});

return exports;
}
Expand Down
163 changes: 163 additions & 0 deletions test/spec/graphics/computeBoundingBox.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
define(
function (require) {

var computeBoundingBox = require('graphics/computeBoundingBox');

var p0 = {
x: 50,
y: 50,
onCurve: true
};

var c0 = {
x: 80,
y: 60
};

var c1 = {
x: 200,
y: 200
};

var p1 = {
x: 100,
y: 100,
onCurve: true
};

describe('计算包围盒', function () {



it('test computeBounding', function () {

var result = computeBoundingBox.computeBounding([]);
expect(result).toBe(false);

var result = computeBoundingBox.computeBounding([p0]);
expect(result).toEqual({
x: 50,
y: 50,
width: 0,
height: 0
});

var result = computeBoundingBox.computeBounding([p0, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 50,
height: 50
});


var result = computeBoundingBox.computeBounding([p0, c1, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 150,
height: 150
});
});


it('test quadraticBezier', function () {

var result = computeBoundingBox.quadraticBezier(p0, p0, p0);
expect(result).toEqual({
x: 50,
y: 50,
width: 0,
height: 0
});

var result = computeBoundingBox.quadraticBezier(p0, p0, p1);
expect(result).toEqual({
x: 50,
y: 50,
width: 50,
height: 50
});

var result = computeBoundingBox.quadraticBezier(p0, c0, p1);
expect(result).toEqual({
x: 50,
y: 50,
width: 50,
height: 50
});

var result = computeBoundingBox.quadraticBezier(p0, c1, p1);
expect(result).toEqual({
x: 50,
y: 50,
width: 90,
height: 90
});
});

it('test computePath', function () {
var result = computeBoundingBox.computePath([p0]);
expect(result).toEqual({
x: 50,
y: 50,
width: 0,
height: 0
});

var result = computeBoundingBox.computePath([p0, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 50,
height: 50
});


var result = computeBoundingBox.computePath([p0, c1, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 90,
height: 90
});
});


it('test computePathBox', function () {

var result = computeBoundingBox.computePathBox([]);
expect(result).toBe(false);

var result = computeBoundingBox.computePathBox([p0]);
expect(result).toEqual({
x: 50,
y: 50,
width: 0,
height: 0
});

var result = computeBoundingBox.computePathBox([p0, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 50,
height: 50
});


var result = computeBoundingBox.computePathBox([p0, c1, p1]);
expect(result).toEqual({
x: 50,
y: 50,
width: 150,
height: 150
});
});



});

}
);
128 changes: 128 additions & 0 deletions test/spec/graphics/pathAdjust.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
define(
function (require) {
var lang = require('common/lang');
var pathAdjust = require('graphics/pathAdjust');

var path = [
{
x: 50,
y: 50
},
{
x: 100,
y: 100
}
];

describe('调整路径', function () {

it('test default', function () {

var result = pathAdjust(lang.clone(path));
expect(result).toEqual([
{
x: 50,
y: 50
},
{
x: 100,
y: 100
}
]);

var result = pathAdjust(lang.clone(path), 1, 1);
expect(result).toEqual([
{
x: 50,
y: 50
},
{
x: 100,
y: 100
}
]);

var result = pathAdjust(lang.clone(path), 1, 1, 0, 0);
expect(result).toEqual([
{
x: 50,
y: 50
},
{
x: 100,
y: 100
}
]);
});

it('test scale', function () {

var result = pathAdjust(lang.clone(path), 2, 2);
expect(result).toEqual([
{
x: 100,
y: 100
},
{
x: 200,
y: 200
}
]);

var result = pathAdjust(lang.clone(path), 0.5, 1);
expect(result).toEqual([
{
x: 25,
y: 50
},
{
x: 50,
y: 100
}
]);

var result = pathAdjust(lang.clone(path), 1, 2, 0, 0);
expect(result).toEqual([
{
x: 50,
y: 100
},
{
x: 100,
y: 200
}
]);
});


it('test offset', function () {

var result = pathAdjust(lang.clone(path), 1, 1, 10, 10);
expect(result).toEqual([
{
x: 60,
y: 60
},
{
x: 110,
y: 110
}
]);

var result = pathAdjust(lang.clone(path), 2, 2, 10, 10);
expect(result).toEqual([
{
x: 120,
y: 120
},
{
x: 220,
y: 220
}
]);
});

});

}
);
7 changes: 7 additions & 0 deletions test/spec/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ define(
// common
require('./common/lang.spec');

// glyf
require('./graphics/computeBoundingBox.spec');
require('./graphics/pathAdjust.spec');

// math
require('./math/bezierCubic2Q2.spec');

// ttf
require('./ttf/reader.spec');
require('./ttf/writer.spec');
Expand Down
Loading

0 comments on commit 2f8338e

Please sign in to comment.