Rule 1. You can’t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don’t try to second guess and put in a speed hack until you’ve proven that’s where the bottleneck is. Rule 2. Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest. Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don’t get fancy. (Even if n does get big, use Rule 2 first.) Rule 4. Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures. Rule 5. Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming. [3] Rule 6. There is no Rule 6
I’m gonna use this post to continuesly show all those design defection that I’ve been through. The God Design is like this: I think maybe I do this, it can have some effect in my mind, and i tried it, and yay! it works!
Switching input method in OSX in
Sougou iOS custom input method
In iOS, u can register some quick input combo. like me, if i type “zzzg”, it should have “zhengzhongzhao###gmail”
in the Sougou custom input method, we don’t have it.
iOS, Wechat, Renren sharing
It’s been a long time since iOS8 releases, but wechat(the most likely one to be the FB in CHINA) haven’t come with the extension of sharing.
it('should return error trying to save duplicate username', function(done){
var profile = {
username: 'vgheri',
password: 'test',
firstName: 'Valerio',
lastName: 'Gheri'
};
// once we have specified the info we want to send to the server via POST verb,
// we need to actually perform the action on the resource, in this case we want to
// POST on /api/profiles and we want to send some info
// We do this using the request object, requiring supertest!
request(url)
.post('/api/profiles')
.send(profile)
// end handles the response
.end(function(err, res){
if (err) {
throw err;
}
// this is should.js syntax, very clear
res.should.have.status(400);
done();
});
});
it('should correctly update an existing account', function(done){
var body = {
firstName: 'JP',
lastName: 'Berd'
};
request(url)
.put('/api/profiles/vgheri')
.send(body)
.expect('Content-Type', /json/)
.expect(200) //Status code
.end(function(err,res){
if (err) {
throw err;
}
// Should.js fluent syntax applied
res.body.should.have.property('_id');
res.body.firstName.should.equal('JP');
res.body.lastName.should.equal('Berd');
res.body.creationDate.should.not.equal(null);
done();
});
});
});
});
1
2
3
4
5
6
7
8
9
describe('#index', function(){
it('should get /topic/:tid 200', function(done){
request.get('/topic/' + support.testTopic._id)
.expect(200, function(err, res){
res.text.should.containEql('test topic content');
res.text.should.containEql('alsotang');
done(err);
});
});
涉及到插入的时候,之前写rspec也遇到类似的 的情况,可以通过测试count是否增加1。
Fun
mocha -R nyan and whole lots of other stuff.
1
2
3
4
5
6
1 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__| /\_/\
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_~|_( ^ .^)
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_""""
1 passing (141ms)
Chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
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
Should
chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors')
.with.length(3);
Expect
var expect = chai.expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(tea).to.have.property('flavors')
.with.length(3);
Assert
var assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);
SuperTest
Miscs
Be kind and don’t make developers hunt around in your docs to figure out how to run the tests, add a make test target to your Makefile - TJ
Nock
Nock is an HTTP mocking library that can record HTTP calls and play them back, so that you can either mock an API by hand, or just record actual HTTP calls and use their responses in future test runs. Our orchestrate.js library does the former, but I’ll show you how to do the latter.