PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/neatx/test/python/neatx.protocol_test.py

http://neatx.googlecode.com/
Python | 102 lines | 69 code | 13 blank | 20 comment | 0 complexity | 814329ea6838f596ba263907beab7277 MD5 | raw file
  1#!/usr/bin/python
  2#
  3
  4# Copyright (C) 2009 Google Inc.
  5#
  6# This program is free software; you can redistribute it and/or modify
  7# it under the terms of the GNU General Public License as published by
  8# the Free Software Foundation; either version 2 of the License, or
  9# (at your option) any later version.
 10#
 11# This program is distributed in the hope that it will be useful, but
 12# WITHOUT ANY WARRANTY; without even the implied warranty of
 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14# General Public License for more details.
 15#
 16# You should have received a copy of the GNU General Public License
 17# along with this program; if not, write to the Free Software
 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19# 02110-1301, USA.
 20
 21
 22"""Script for unittesting the protocol module"""
 23
 24
 25import unittest
 26
 27from neatx import constants
 28from neatx import errors
 29from neatx import protocol
 30from neatx import utils
 31
 32import mocks
 33
 34
 35class TestParseParameters(unittest.TestCase):
 36  """Tests for ParseParameters"""
 37
 38  def setUp(self):
 39    self._fake_logging = mocks.FakeLogging()
 40
 41  def _DoTest(self, params, expected):
 42    result = protocol.ParseParameters(params, _logging=self._fake_logging)
 43    self.failUnlessEqual(result, expected)
 44
 45  def _DoFailTest(self, params):
 46    self.failUnlessRaises(protocol.NxParameterParsingError,
 47                          protocol.ParseParameters, params,
 48                          _logging=self._fake_logging)
 49
 50  def test(self):
 51    self._DoTest("", [])
 52    self._DoTest(" ", [])
 53    self._DoTest("\t", [])
 54    self._DoTest("--session=\"\"", [("session", "")])
 55    self._DoTest("--session=\"\" --name=\"\"", [("session", ""), ("name", "")])
 56    self._DoTest("--session=\"123\"", [("session", "123")])
 57    self._DoTest(" --session=\"123\"", [("session", "123")])
 58    self._DoTest("--session=\"123\" ", [("session", "123")])
 59    self._DoTest(" --session=\"123\" ", [("session", "123")])
 60
 61    self._DoTest("--session=\"123\" --name=\"dummy\"",
 62                 [("session", "123"), ("name", "dummy")])
 63    self._DoTest(" --session=\"123\" --name=\"dummy\" ",
 64                 [("session", "123"), ("name", "dummy")])
 65    self._DoTest("\t--session=\"123\"\t--name=\"dummy\"\n",
 66                 [("session", "123"), ("name", "dummy")])
 67    self._DoTest("--session=\" value with spaces \" --name=\" a b\tc \"\n",
 68                 [("session", " value with spaces "), ("name", " a b\tc ")])
 69
 70    self._DoTest("--name=\"x\" --name=\"y\"", [("name", "x"), ("name", "y")])
 71
 72    self._DoFailTest(",")
 73    self._DoFailTest("-")
 74    self._DoFailTest("--x")
 75    self._DoFailTest("--xyz=")
 76    self._DoFailTest("--xyz=\"")
 77    self._DoFailTest("--xyz=\"\"\"")
 78    self._DoFailTest("--xyz=\"\"a")
 79    self._DoFailTest("--xyz=\"\" --name")
 80    self._DoFailTest("--xyz=\"\" --name=\"")
 81    self._DoFailTest("--xyz=\"\",--name=\"\"")
 82    self._DoFailTest("--xyz=\"\", --name=\"\"")
 83    self._DoFailTest("--xyz=\"\" , --name=\"\"")
 84
 85
 86class TestUnquoteParameterValue(unittest.TestCase):
 87  """Tests for UnquoteParameterValue"""
 88
 89  def _DoTest(self, value, expected):
 90    unquoted = protocol.UnquoteParameterValue(value)
 91    self.failUnlessEqual(unquoted, expected)
 92
 93  def test(self):
 94    self._DoTest("abc", "abc")
 95    self._DoTest("x%3D%22abc%20+%20%22%3B%3A%3B%20/usr/bin/xterm",
 96                 """x="abc + ";:; /usr/bin/xterm""")
 97    self._DoTest("%26%20-%20_%20/%20+%20%27%20%22%20(%20)%20%5B%20%5D%20%7C",
 98                 """& - _ / + ' " ( ) [ ] |""")
 99
100
101if __name__ == '__main__':
102  unittest.main()