test_context.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from nose.tools import with_setup, assert_equals, assert_not_equals, assert_true
  2. from gixy.core.context import get_context, pop_context, push_context, purge_context, CONTEXTS, Context
  3. from gixy.directives.block import Root
  4. from gixy.core.variable import Variable
  5. from gixy.core.regexp import Regexp
  6. def setup():
  7. assert_equals(len(CONTEXTS), 0)
  8. def tear_down():
  9. purge_context()
  10. @with_setup(setup, tear_down)
  11. def test_push_pop_context():
  12. root_a = Root()
  13. push_context(root_a)
  14. assert_equals(len(CONTEXTS), 1)
  15. root_b = Root()
  16. push_context(root_b)
  17. assert_equals(len(CONTEXTS), 2)
  18. poped = pop_context()
  19. assert_equals(len(CONTEXTS), 1)
  20. assert_equals(poped.block, root_b)
  21. poped = pop_context()
  22. assert_equals(len(CONTEXTS), 0)
  23. assert_equals(poped.block, root_a)
  24. @with_setup(setup, tear_down)
  25. def test_push_get_purge_context():
  26. root = Root()
  27. push_context(root)
  28. assert_equals(len(CONTEXTS), 1)
  29. assert_equals(get_context().block, root)
  30. root = Root()
  31. push_context(root)
  32. assert_equals(len(CONTEXTS), 2)
  33. assert_equals(get_context().block, root)
  34. purge_context()
  35. assert_equals(len(CONTEXTS), 0)
  36. @with_setup(setup, tear_down)
  37. def test_add_variables():
  38. context = push_context(Root())
  39. assert_equals(len(context.variables['index']), 0)
  40. assert_equals(len(context.variables['name']), 0)
  41. one_str_var = Variable('1')
  42. context.add_var('1', one_str_var)
  43. one_int_var = Variable(1)
  44. context.add_var(1, one_int_var)
  45. some_var = Variable('some')
  46. context.add_var('some', some_var)
  47. assert_equals(len(context.variables['index']), 1)
  48. assert_equals(context.variables['index'][1], one_int_var)
  49. assert_equals(len(context.variables['name']), 1)
  50. assert_equals(context.variables['name']['some'], some_var)
  51. context.clear_index_vars()
  52. assert_equals(len(context.variables['index']), 0)
  53. assert_equals(len(context.variables['name']), 1)
  54. assert_equals(context.variables['name']['some'], some_var)
  55. @with_setup(setup, tear_down)
  56. def test_get_variables():
  57. context = push_context(Root())
  58. assert_equals(len(context.variables['index']), 0)
  59. assert_equals(len(context.variables['name']), 0)
  60. one_var = Variable(1)
  61. context.add_var(1, one_var)
  62. some_var = Variable('some')
  63. context.add_var('some', some_var)
  64. assert_equals(context.get_var(1), one_var)
  65. assert_equals(context.get_var('some'), some_var)
  66. # Checks not existed variables, for now context may return None
  67. assert_equals(context.get_var(0), None)
  68. assert_equals(context.get_var('not_existed'), None)
  69. # Checks builtins variables
  70. assert_true(context.get_var('uri'))
  71. assert_true(context.get_var('document_uri'))
  72. assert_true(context.get_var('arg_asdsadasd'))
  73. assert_true(context.get_var('args'))
  74. @with_setup(setup, tear_down)
  75. def test_context_depend_variables():
  76. push_context(Root())
  77. assert_equals(len(get_context().variables['index']), 0)
  78. assert_equals(len(get_context().variables['name']), 0)
  79. get_context().add_var(1, Variable(1, value='one'))
  80. get_context().add_var('some', Variable('some', value='some'))
  81. assert_equals(get_context().get_var(1).value, 'one')
  82. assert_equals(get_context().get_var('some').value, 'some')
  83. # Checks top context variables are still exists
  84. push_context(Root())
  85. assert_equals(get_context().get_var(1).value, 'one')
  86. assert_equals(get_context().get_var('some').value, 'some')
  87. # Checks variable overriding
  88. get_context().add_var('some', Variable('some', value='some_new'))
  89. get_context().add_var('foo', Variable('foo', value='foo'))
  90. assert_not_equals(get_context().get_var('some').value, 'some')
  91. assert_equals(get_context().get_var('some').value, 'some_new')
  92. assert_equals(get_context().get_var('foo').value, 'foo')
  93. assert_equals(get_context().get_var(1).value, 'one')
  94. # Checks variables after restore previous context
  95. pop_context()
  96. assert_not_equals(get_context().get_var('some').value, 'some_new')
  97. assert_equals(get_context().get_var('some').value, 'some')
  98. assert_equals(get_context().get_var('foo'), None)
  99. assert_equals(get_context().get_var(1).value, 'one')
  100. @with_setup(setup, tear_down)
  101. def test_push_failed_with_regexp_py35_gixy_10():
  102. push_context(Root())
  103. assert_equals(len(get_context().variables['index']), 0)
  104. assert_equals(len(get_context().variables['name']), 0)
  105. regexp = Regexp('^/some/(.*?)')
  106. for name, group in regexp.groups.items():
  107. get_context().add_var(name, Variable(name=name, value=group))
  108. push_context(Root())