]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Add a bunch of tests
authorDaniel Imms <daimms@microsoft.com>
Sat, 11 Jun 2016 01:53:32 +0000 (18:53 -0700)
committerDaniel Imms <daimms@microsoft.com>
Sat, 11 Jun 2016 01:53:32 +0000 (18:53 -0700)
addons/linkify/linkify.js
package.json
test/addons/linkify-test.js [new file with mode: 0644]

index 344bc71124bd06a7a107136f241e07b05eeeb5c5..f02c2023c2aaed2a757f7bf3d5333aeb3810c2a9 100644 (file)
         continue;
       }
 
+      var url = exports.findLinkMatch(node.data, lenient);
 
-      if (lenient) {
-        match = node.data.match(lenientUrlRegex);
-      } else {
-        match = node.data.match(strictUrlRegex);
-      }
-
-      /**
-       * If no URL was found in the current text, return.
-       */
-      if (!match) {
+      if (!url) {
         continue;
       }
 
-      var url = match[1],
-          startsWithProtocol = new RegExp('^' + protocolClause),
+      var startsWithProtocol = new RegExp('^' + protocolClause),
           urlHasProtocol = url.match(startsWithProtocol),
           href = (urlHasProtocol) ? url : 'http://' + url,
           link = '<a href="' +  href + '" >' + url + '</a>',
     terminal.emit('linkify:line', line);
   };
 
+  /**
+   * Finds a link within a block of text.
+   *
+   * @param {string} text - The text to search .
+   * @param {boolean} lenient - Whether to use the lenient search.
+   * @return {string} A URL.
+   */
+  exports.findLinkMatch = function (text, lenient) {
+    var match = text.match(lenient ? lenientUrlRegex : strictUrlRegex);
+    if (!match || match.length === 0) {
+      return null;
+    }
+    return match[1];
+  }
 
   /**
    * Converts all valid URLs found in the terminal view into hyperlinks.
index 73fde3c7c3dd96f908e8abb97f83c1d15989252a..f1d3e57741d9aad7a54956d43d2ca4bbf84c3fa9 100644 (file)
@@ -14,6 +14,6 @@
   },
   "scripts": {
     "start": "bash bin/server",
-    "test": "bash bin/test"
+    "test": "bash bin/test --recursive"
   }
 }
diff --git a/test/addons/linkify-test.js b/test/addons/linkify-test.js
new file mode 100644 (file)
index 0000000..7cb2318
--- /dev/null
@@ -0,0 +1,48 @@
+var assert = require('chai').assert;
+var Terminal = require('../../src/xterm');
+var linkify = require('../../addons/linkify/linkify');
+
+describe('linkify addon', function () {
+  var xterm;
+
+  describe('API', function () {
+    it('should define Terminal.prototype.linkify', function () {
+      assert.isDefined(Terminal.prototype.linkify);
+    });
+    it('should define Terminal.prototype.linkifyTerminalLine', function () {
+      assert.isDefined(Terminal.prototype.linkifyTerminalLine);
+    });
+  });
+
+  describe('findUrlMatchOnLine', function () {
+    describe('strict regex', function () {
+      it('should match when the entire text is a match', function () {
+        assert.equal(linkify.findLinkMatch('http://github.com', false), 'http://github.com');
+        assert.equal(linkify.findLinkMatch('http://127.0.0.1', false), 'http://127.0.0.1');
+      });
+      it('should match simple domains', function () {
+        assert.equal(linkify.findLinkMatch('foo http://github.com bar', false), 'http://github.com');
+        assert.equal(linkify.findLinkMatch('foo http://www.github.com bar', false), 'http://www.github.com');
+        assert.equal(linkify.findLinkMatch('foo https://github.com bar', false), 'https://github.com');
+        assert.equal(linkify.findLinkMatch('foo https://www.github.com bar', false), 'https://www.github.com');
+      });
+      it('should match web addresses with alpha paths', function () {
+        assert.equal(linkify.findLinkMatch('foo http://github.com/a/b/c bar', false), 'http://github.com/a/b/c');
+        assert.equal(linkify.findLinkMatch('foo http://www.github.com/a/b/c bar', false), 'http://www.github.com/a/b/c');
+      });
+      it('should not include whitespace surrounding a match', function () {
+        assert.equal(linkify.findLinkMatch(' http://github.com', false), 'http://github.com');
+        assert.equal(linkify.findLinkMatch('http://github.com ', false), 'http://github.com');
+        assert.equal(linkify.findLinkMatch('  http://github.com  ', false), 'http://github.com');
+      });
+      it('should match IP addresses', function () {
+        assert.equal(linkify.findLinkMatch('foo http://127.0.0.1 bar', false), 'http://127.0.0.1');
+        assert.equal(linkify.findLinkMatch('foo https://127.0.0.1 bar', false), 'https://127.0.0.1');
+      });
+      it('should match ports on both domains and IP addresses', function () {
+        assert.equal(linkify.findLinkMatch('foo http://127.0.0.1:8080 bar', false), 'http://127.0.0.1:8080');
+        assert.equal(linkify.findLinkMatch('foo http://www.github.com:8080 bar', false), 'http://www.github.com:8080');
+      });
+    });
+  });
+});