PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ruby/test/test_following.rb

https://bitbucket.org/shibu/berrymq
Ruby | 154 lines | 130 code | 24 blank | 0 comment | 0 complexity | a8f4f427cb126f410b7c9399b6feaf83 MD5 | raw file
Possible License(s): MIT
  1. $LOAD_PATH.unshift(File::expand_path(File::dirname(__FILE__)) + '/../lib')
  2. require 'berrymq'
  3. require 'test/unit'
  4. class Sample1
  5. include BerryMQ::Follower
  6. following("test1@test_ns1:entry")
  7. def test_follow_method(message)
  8. end
  9. end
  10. class TestFollowerRegister < Test::Unit::TestCase
  11. def test_regist
  12. sample = Sample1.new
  13. assert_equal(1, BerryMQ::RootTransporter["test_ns1"].size)
  14. end
  15. end
  16. class Sample2
  17. include BerryMQ::Follower
  18. attr_reader :called
  19. def initialize
  20. @called = 0
  21. super
  22. end
  23. following("sample2:entry")
  24. def test_follow_method(message)
  25. @called += 1
  26. end
  27. following("sample2:original")
  28. def test_original_action(message)
  29. @called += 1
  30. end
  31. following("sample3:entry")
  32. def test_other_name(message)
  33. @called +=1
  34. end
  35. end
  36. class TestMessageSending < Test::Unit::TestCase
  37. def test_regist
  38. sample = Sample2.new
  39. BerryMQ::twitter("sample2:entry")
  40. assert_equal 1, sample.called
  41. end
  42. def test_original_action
  43. sample = Sample2.new
  44. BerryMQ::twitter("sample2:original")
  45. assert_equal 1, sample.called
  46. end
  47. def test_wildcard_action_1
  48. sample = Sample2.new
  49. BerryMQ::twitter("sample2:*")
  50. assert_equal 2, sample.called
  51. end
  52. def test_wildcard_action_2
  53. sample = Sample2.new
  54. BerryMQ::twitter("sample2")
  55. assert_equal 2, sample.called
  56. end
  57. def test_wildcard_name
  58. sample = Sample2.new
  59. BerryMQ::twitter("*:entry")
  60. assert_equal 2, sample.called
  61. end
  62. def test_wildcard_all_1
  63. sample = Sample2.new
  64. BerryMQ::twitter("*:*")
  65. assert_equal 3, sample.called
  66. end
  67. def test_wildcard_all_2
  68. sample = Sample2.new
  69. BerryMQ::twitter("*")
  70. assert_equal 3, sample.called
  71. end
  72. end
  73. class Sample4
  74. include BerryMQ::Follower
  75. def initialize(history)
  76. @call_history = history
  77. super()
  78. end
  79. auto_twitter("sample4:entry")
  80. def twitt_entry_method
  81. @call_history.push("target")
  82. end
  83. auto_twitter("sample4:exit")
  84. def twitt_exit_method
  85. @call_history.push("target")
  86. end
  87. auto_twitter("sample4")
  88. def twitter_entry_and_exit_method
  89. @call_history.push("target")
  90. end
  91. following("sample4:entry")
  92. def follow_entry(message)
  93. @call_history.push("entry")
  94. end
  95. following("sample4:exit")
  96. def follow_exit(message)
  97. @call_history.push("exit")
  98. end
  99. end
  100. class TestAutoTwitter < Test::Unit::TestCase
  101. def test_auto_twitter_enter
  102. call_history = []
  103. sample = Sample4.new(call_history)
  104. sample.twitt_entry_method
  105. assert_equal ["entry", "target"], call_history
  106. end
  107. def test_auto_twitter_exit
  108. call_history = []
  109. sample = Sample4.new(call_history)
  110. sample.twitt_exit_method
  111. assert_equal ["target", "exit"], call_history
  112. end
  113. def test_auto_twitter_both
  114. call_history = []
  115. sample = Sample4.new(call_history)
  116. sample.twitter_entry_and_exit_method
  117. assert_equal ["entry", "target", "exit"], call_history
  118. end
  119. end
  120. class TestUtil < Test::Unit::TestCase
  121. def test_iter_chain
  122. a = [1, 2, 3]
  123. b = [4, 5, 6]
  124. result = []
  125. BerryMQ::Util::iter_chain(a, b) { |v|
  126. result.push(v)
  127. }
  128. assert_equal [1, 2, 3, 4, 5, 6], result
  129. end
  130. end