Permalink
Browse files

Use should and mocha for testing.

  • Loading branch information...
1 parent 02513a5 commit 138d6fad2297fe261733e49a8b0e66b5d5d6afbc @drewish drewish committed Apr 22, 2012
Showing with 129 additions and 62 deletions.
  1. +3 −2 .gitignore
  2. +1 −1 README.md
  3. +26 −0 examples/simple.js
  4. +6 −3 package.json
  5. +2 −0 test/mocha.opts
  6. +91 −56 test/test.js
View
@@ -1,3 +1,4 @@
-
-build/
.lock-wscript
+build/
+node_modules/
+test/copy.jpg
View
@@ -41,7 +41,7 @@ Install the module with npm:
}
});
-See test.js.
+See `example/simple.js`.
email: dberesford at gmail
twitter: @dberesford
View
@@ -0,0 +1,26 @@
+var ex = require('exiv2node');
+var assert = require('assert');
+var fs = require('fs')
+
+var dir = __dirname + '../test/images';
+
+// Test basic image with exif tags
+ex.getImageTags(dir + '/books.jpg', function(err, tags) {
+ console.log(tags);
+});
+
+// Make a copy of our file so we don't polute the original.
+fs.writeFileSync(dir + '/copy.jpg', fs.readFileSync(dir + '/books.jpg'));
+
+// Set some tags on the image
+var tags = {
+ "Exif.Photo.UserComment" : "Some books..",
+ "Exif.Canon.OwnerName" : "Damo's camera"
+};
+ex.setImageTags(dir + '/copy.jpg', tags, function(err){
+ // Check our tags have been set
+ ex.getImageTags(dir + '/copy.jpg', function(err, tags) {
+ assert.equal("Some books..", tags["Exif.Photo.UserComment"]);
+ assert.equal("Damo's camera", tags["Exif.Canon.OwnerName"]);
+ });
+});
View
@@ -9,15 +9,18 @@
"url": "git://github.com/dberesford/exiv2node.git"
},
"dependencies": {},
- "devDependencies": {},
+ "devDependencies": {
+ "should": "*",
+ "mocha": "*"
+ },
"optionalDependencies": {},
"engines": {
"node": ">= 0.5.6"
},
"main": "exiv2node.node",
"scripts": {
- "install" : "node-waf install",
+ "install" : "node-waf configure build install",
"preuninstall": "rm -rf build/*",
- "test": "node test/test.js"
+ "test": "mocha"
}
}
View
@@ -0,0 +1,2 @@
+--growl
+--reporter spec
View
@@ -1,57 +1,92 @@
-var ex = require('exiv2node');
-var assert = require('assert');
-var fs = require('fs')
-
-var dir = __dirname + '/images';
-
-// Test basic image with exif tags
-ex.getImageTags(dir + '/books.jpg', function(err, tags) {
- assert.equal(null, err);
- assert.notEqual(null, tags);
-
- if (tags) {
- //console.log("All Tags:");
- //for (key in tags){
- // console.log(key + ":" + tags[key]);
- //}
- assert.equal("2008:12:16 21:28:36", tags["Exif.Image.DateTime"]);
- assert.equal("2008:12:16 21:28:36", tags["Exif.Photo.DateTimeOriginal"]);
- }
-});
-
-
-// Set image tags, first make a fresh copy of our test image
-fs.writeFileSync(dir + '/copy.jpg', fs.readFileSync(dir + '/books.jpg'));
-
-// Set some tags on the image
-ex.setImageTags(dir + '/copy.jpg', { "Exif.Photo.UserComment" : "Some books..", "Exif.Canon.OwnerName" : "Damo's camera"}, function(err){
- assert.equal(null, err);
-
- if (err) {
- // console.log(err);
- }else {
- // console.log("setImageTags complete..");
- }
-
- // Check our tags have been set
- ex.getImageTags(dir + '/copy.jpg', function(err, tags) {
- assert.equal("Some books..", tags["Exif.Photo.UserComment"]);
- assert.equal("Damo's camera", tags["Exif.Canon.OwnerName"]);
- });
-});
-
-// Test image with no tags
-ex.getImageTags(dir + '/damien.jpg', function(err, tags) {
- assert.equal(null, tags);
-});
-
-// Test non existent files
-ex.setImageTags('idontexist.jpg', { "Exif.Photo.UserComment" : "test"}, function(err){
- assert.notEqual(null, err);
- //console.log(err);
-});
-ex.getImageTags('idontexist.jpg', function(err, tags) {
- assert.notEqual(null, err);
- //console.log(err);
-});
+var exiv = require('exiv2node')
+ , fs = require('fs')
+ , should = require('should')
+ , dir = __dirname + '/images';
+describe('exiv2', function(){
+ describe('.getImageTags()', function(){
+ it("should callback with image's tags", function(done) {
+ exiv.getImageTags(dir + '/books.jpg', function(err, tags) {
+ should.not.exist(err);
+ tags.should.be.a('object');
+ tags.should.have.property('Exif.Image.DateTime', '2008:12:16 21:28:36');
+ tags.should.have.property('Exif.Photo.DateTimeOriginal', '2008:12:16 21:28:36');
+ done();
+ })
+ });
+
+ it('should callback with null on untagged file', function(done) {
+ exiv.getImageTags(dir + '/damien.jpg', function(err, tags) {
+ should.not.exist(err);
+ should.not.exist(tags);
+ done();
+ })
+ });
+
+ it('should throw if no file path is provided', function() {
+ (function(){
+ exiv.getImageTags()
+ }).should.throw();
+ });
+
+ it('should throw if no callback is provided', function() {
+ (function(){
+ exiv.getImageTags(dir + '/books.jpg')
+ }).should.throw();
+ });
+
+ it('should report an error on an invalid path', function(done) {
+ exiv.getImageTags('idontexist.jpg', function(err, tags) {
+ should.exist(err);
+ should.not.exist(tags);
+ done();
+ });
+ });
+ });
+
+ describe('.setImageTags()', function(){
+ var temp = dir + '/copy.jpg';
+
+ before(function() {
+ fs.writeFileSync(temp, fs.readFileSync(dir + '/books.jpg'));
+ });
+ it('should write tags to image files', function(done) {
+ var tags = {
+ "Exif.Photo.UserComment" : "Some books..",
+ "Exif.Canon.OwnerName" : "Damo's camera"
+ };
+ exiv.setImageTags(temp, tags, function(err){
+ should.not.exist(err);
+
+ exiv.getImageTags(temp, function(err, tags) {
+ tags.should.have.property('Exif.Photo.UserComment', "Some books..");
+ tags.should.have.property('Exif.Canon.OwnerName', "Damo's camera");
+ done();
+ });
+ });
+ })
+ after(function(done) {
+ fs.unlink(temp, done);
+ });
+
+ it('should throw if no file path is provided', function() {
+ (function(){
+ exiv.setImageTags()
+ }).should.throw();
+ });
+
+ it('should throw if no callback is provided', function() {
+ (function(){
+ exiv.setImageTags(dir + '/books.jpg')
+ }).should.throw();
+ });
+
+ it('should report an error on an invalid path', function(done) {
+ exiv.setImageTags('idontexist.jpg', {}, function(err, tags) {
+ should.exist(err);
+ should.not.exist(tags);
+ done();
+ });
+ });
+ });
+})

0 comments on commit 138d6fa

Please sign in to comment.