Browse Source

Add tests

Alexander I. Zaytsev 11 năm trước cách đây
mục cha
commit
efc8b2339e
5 tập tin đã thay đổi với 128 bổ sung0 xóa
  1. 1 0
      .gitignore
  2. 8 0
      .npmignore
  3. 56 0
      Gruntfile.js
  4. 6 0
      package.json
  5. 57 0
      test/jsfuck_test.js

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
+node_modules
 .DS_Store

+ 8 - 0
.npmignore

@@ -0,0 +1,8 @@
+docs
+test
+.DS_Store
+.editorconfig
+.gitattributes
+.jshintrc
+.travis.yml
+Gruntfile.js

+ 56 - 0
Gruntfile.js

@@ -0,0 +1,56 @@
+/*global module:false*/
+module.exports = function(grunt) {
+
+  // Project configuration.
+  grunt.initConfig({
+    // Task configuration.
+    jshint: {
+      options: {
+        curly: true,
+        eqeqeq: true,
+        immed: true,
+        latedef: true,
+        newcap: true,
+        noarg: true,
+        sub: true,
+        undef: true,
+        unused: true,
+        boss: true,
+        eqnull: true,
+        node: true,
+        globals: {
+          jQuery: true,
+          window: true,
+        }
+      },
+      gruntfile: {
+        src: 'Gruntfile.js'
+      },
+      lib_test: {
+        src: ['jsfuck.js', 'lib/**/*.js', 'test/**/*.js']
+      }
+    },
+    nodeunit: {
+      files: ['test/**/*_test.js']
+    },
+    watch: {
+      gruntfile: {
+        files: '<%= jshint.gruntfile.src %>',
+        tasks: ['jshint:gruntfile']
+      },
+      lib_test: {
+        files: '<%= jshint.lib_test.src %>',
+        tasks: ['jshint:lib_test', 'nodeunit']
+      }
+    }
+  });
+
+  // These plugins provide necessary tasks.
+  grunt.loadNpmTasks('grunt-contrib-nodeunit');
+  grunt.loadNpmTasks('grunt-contrib-jshint');
+  grunt.loadNpmTasks('grunt-contrib-watch');
+
+  // Default task.
+  grunt.registerTask('default', ['jshint', 'nodeunit']);
+
+};

+ 6 - 0
package.json

@@ -24,5 +24,11 @@
   ],
   "bugs": {
     "url": "https://github.com/aemkei/jsfuck/issues"
+  },
+  "devDependencies": {
+    "grunt": "~0.4.1",
+    "grunt-contrib-nodeunit": "~0.2.0",
+    "grunt-contrib-watch": "~0.5.3",
+    "grunt-contrib-jshint": "~0.6.4"
   }
 }

+ 57 - 0
test/jsfuck_test.js

@@ -0,0 +1,57 @@
+/*jshint -W061 */
+'use strict';
+
+var JSFuck = require('../jsfuck.js').JSFuck,
+	test_encode = function (test, value) {
+		var encoded = JSFuck.encode(value),
+			unencoded = eval(encoded);
+
+		test.strictEqual(value, unencoded, 'encoding "' + value + '" failed');
+	};
+
+exports['encode_tests'] = {
+	'encode numbers': function(test) {
+		for (var i=0; i<=10; i++) {
+			test_encode(test, i+"");
+		}
+		test.done();
+	},
+	'encode "false"': function(test) {
+		test_encode(test, 'false');
+		test.done();
+	},
+	'encode "falsefalsetrue"': function(test) {
+		test_encode(test, 'falsefalsetrue');
+		test.done();
+	},
+	'encode "a"': function(test) {
+		test_encode(test, 'a');
+		test.done();
+	},
+	'encode "ABCDEFGHIJKLMNOPQRSTUVWXYZ"': function(test) {
+		test_encode(test, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
+		test.done();
+	},
+	'encode "abcdefghijklmnopqrstuvwxyz"': function(test) {
+		test_encode(test, 'abcdefghijklmnopqrstuvwxyz');
+		test.done();
+	}
+};
+
+exports['tests'] = {
+	'encode 1': function(test) {
+		var encoded = JSFuck.encode('1');
+		test.equal(encoded, '[+!+[]]+[]');
+		test.done();
+	},
+	'encode 2': function(test) {
+		var encoded = JSFuck.encode('2');
+		test.equal(encoded, '[!+[]+!+[]]+[]');
+		test.done();
+	},
+	'encode 3': function(test) {
+		var encoded = JSFuck.encode('3');
+		test.equal(encoded, '[!+[]+!+[]+!+[]]+[]');
+		test.done();
+	}
+};