Add Port class functionality and tests from Artoo

This commit is contained in:
Andrew Stewart 2013-10-21 22:11:32 -07:00
parent 846161c004
commit 9e7614a4a1
2 changed files with 99 additions and 11 deletions

View File

@ -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}"

View File

@ -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"