test_variable.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from nose.tools import assert_true, assert_false, assert_equals, with_setup
  2. from gixy.core.context import get_context, push_context, purge_context
  3. from gixy.directives.block import Root
  4. from gixy.core.regexp import Regexp
  5. from gixy.core.variable import Variable
  6. def setup():
  7. push_context(Root())
  8. def tear_down():
  9. purge_context()
  10. @with_setup(setup, tear_down)
  11. def test_literal():
  12. var = Variable(name='simple', value='$uri', have_script=False)
  13. assert_false(var.depends)
  14. assert_false(var.regexp)
  15. assert_equals(var.value, '$uri')
  16. assert_false(var.can_startswith('$'))
  17. assert_false(var.can_contain('i'))
  18. assert_true(var.must_contain('$'))
  19. assert_true(var.must_contain('u'))
  20. assert_false(var.must_contain('a'))
  21. assert_true(var.must_startswith('$'))
  22. assert_false(var.must_startswith('u'))
  23. @with_setup(setup, tear_down)
  24. def test_regexp():
  25. var = Variable(name='simple', value=Regexp('^/.*'))
  26. assert_false(var.depends)
  27. assert_true(var.regexp)
  28. assert_true(var.can_startswith('/'))
  29. assert_false(var.can_startswith('a'))
  30. assert_true(var.can_contain('a'))
  31. assert_false(var.can_contain('\n'))
  32. assert_true(var.must_contain('/'))
  33. assert_false(var.must_contain('a'))
  34. assert_true(var.must_startswith('/'))
  35. assert_false(var.must_startswith('a'))
  36. @with_setup(setup, tear_down)
  37. def test_script():
  38. get_context().add_var('foo', Variable(name='foo', value=Regexp('.*')))
  39. var = Variable(name='simple', value='/$foo')
  40. assert_true(var.depends)
  41. assert_false(var.regexp)
  42. assert_false(var.can_startswith('/'))
  43. assert_false(var.can_startswith('a'))
  44. assert_true(var.can_contain('/'))
  45. assert_true(var.can_contain('a'))
  46. assert_false(var.can_contain('\n'))
  47. assert_true(var.must_contain('/'))
  48. assert_false(var.must_contain('a'))
  49. assert_true(var.must_startswith('/'))
  50. assert_false(var.must_startswith('a'))
  51. @with_setup(setup, tear_down)
  52. def test_regexp_boundary():
  53. var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True))
  54. assert_false(var.depends)
  55. assert_true(var.regexp)
  56. assert_true(var.can_startswith('/'))
  57. assert_false(var.can_startswith('a'))
  58. assert_false(var.can_contain('/'))
  59. assert_true(var.can_contain('a'))
  60. assert_false(var.can_contain('0'))
  61. assert_false(var.can_contain('\n'))
  62. assert_true(var.must_contain('/'))
  63. assert_false(var.must_contain('a'))
  64. assert_true(var.must_startswith('/'))
  65. assert_false(var.must_startswith('a'))
  66. @with_setup(setup, tear_down)
  67. def test_script_boundary():
  68. get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'), boundary=Regexp('[a-z]', strict=True)))
  69. var = Variable(name='simple', value='/$foo', boundary=Regexp('[/a-z0-9]', strict=True))
  70. assert_true(var.depends)
  71. assert_false(var.regexp)
  72. assert_false(var.can_startswith('/'))
  73. assert_false(var.can_startswith('a'))
  74. assert_false(var.can_contain('/'))
  75. assert_true(var.can_contain('a'))
  76. assert_false(var.can_contain('\n'))
  77. assert_false(var.can_contain('0'))
  78. assert_true(var.must_contain('/'))
  79. assert_false(var.must_contain('a'))
  80. assert_true(var.must_startswith('/'))
  81. assert_false(var.must_startswith('a'))