From 9e7614a4a1ae085fb708af4c62c74b32f890affa Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 21 Oct 2013 22:11:32 -0700 Subject: [PATCH] Add Port class functionality and tests from Artoo --- src/port.coffee | 33 +++++++++++--- test/src/specs/port.spec.coffee | 77 ++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/port.coffee b/src/port.coffee index 9921e45..343b9f6 100644 --- a/src/port.coffee +++ b/src/port.coffee @@ -10,13 +10,36 @@ module.exports = class Port constructor: (data) -> - [@isTcp, @isSerial, @isPortless] = [false, false, false] + @isTcp = @isSerial = @isPortless = false @parse(data) parse: (data) -> - @host = "localhost" - @port = "4567" - # TODO: actual parsing of the incoming data string + if data is undefined + @port = undefined + @isPortless = true + + # is TCP host/port? + else if match = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})/.exec(data) + @port = match[2] + @host = match[1] + @isTcp = true + + # is it a numeric port for localhost tcp? + else if /^[0-9]{1,5}$/.exec(data) + @port = data + @host = "localhost" + @isTcp = true + + # must be a serial port + else + @port = data + @host = undefined + @isSerial = true toString: -> - "#{@host}:#{@port}" + if @isPortless + "none" + else if @isSerial + @port + else + "#{@host}:#{@port}" diff --git a/test/src/specs/port.spec.coffee b/test/src/specs/port.spec.coffee index c652a14..b0a4616 100644 --- a/test/src/specs/port.spec.coffee +++ b/test/src/specs/port.spec.coffee @@ -2,11 +2,76 @@ Port = source("port") -describe "tcp port", -> - p = new Port("locahost:4567") +describe "Port", -> + describe "remote TCP port", -> + port = new Port("192.168.0.1:8080") - it "should have a host", -> - p.host.should.be.equal 'localhost' + it "#port", -> + port.port.should.be.equal "8080" - it "should have a port", -> - p.port.should.be.equal '4567' + it "#isTcp", -> + port.isTcp.should.be.true + + it "#isSerial", -> + port.isSerial.should.be.false + + it "#isPortless", -> + port.isPortless.should.be.false + + it "#toString", -> + port.toString().should.be.equal "192.168.0.1:8080" + + describe "local TCP port", -> + port = new Port("5678") + + it "#port", -> + port.port.should.be.equal "5678" + + it "#isTcp", -> + port.isTcp.should.be.true + + it "#isSerial", -> + port.isSerial.should.be.false + + it "#isPortless", -> + port.isPortless.should.be.false + + it "#toString", -> + port.toString().should.be.equal "localhost:5678" + + describe "serial port", -> + port = new Port("/dev/tty.usb12345") + + it "#port", -> + port.port.should.be.equal "/dev/tty.usb12345" + + it "#isTcp", -> + port.isTcp.should.be.false + + it "#isSerial", -> + port.isSerial.should.be.true + + it "#isPortless", -> + port.isPortless.should.be.false + + it "#toString", -> + port.toString().should.be.equal "/dev/tty.usb12345" + + describe "portless", -> + port = new Port + + it "#port", -> + # cannot call .should on undefined(void 0) + assert port.port is undefined + + it "#isTcp", -> + port.isTcp.should.be.false + + it "#isSerial", -> + port.isSerial.should.be.false + + it "#isPortless", -> + port.isPortless.should.be.true + + it "#toString", -> + port.toString().should.be.equal "none"