dot_helper_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require 'rails_helper'
  2. describe DotHelper do
  3. describe "with example Agents" do
  4. class Agents::DotFoo < Agent
  5. default_schedule "2pm"
  6. def check
  7. create_event :payload => {}
  8. end
  9. end
  10. class Agents::DotBar < Agent
  11. cannot_be_scheduled!
  12. def check
  13. create_event :payload => {}
  14. end
  15. end
  16. before do
  17. stub(Agents::DotFoo).valid_type?("Agents::DotFoo") { true }
  18. stub(Agents::DotBar).valid_type?("Agents::DotBar") { true }
  19. end
  20. describe "#agents_dot" do
  21. before do
  22. @agents = [
  23. @foo = Agents::DotFoo.new(name: "foo").tap { |agent|
  24. agent.user = users(:bob)
  25. agent.save!
  26. },
  27. @bar1 = Agents::DotBar.new(name: "bar1").tap { |agent|
  28. agent.user = users(:bob)
  29. agent.sources << @foo
  30. agent.save!
  31. },
  32. @bar2 = Agents::DotBar.new(name: "bar2").tap { |agent|
  33. agent.user = users(:bob)
  34. agent.sources << @foo
  35. agent.propagate_immediately = true
  36. agent.disabled = true
  37. agent.save!
  38. },
  39. @bar3 = Agents::DotBar.new(name: "bar3").tap { |agent|
  40. agent.user = users(:bob)
  41. agent.sources << @bar2
  42. agent.save!
  43. },
  44. ]
  45. @foo.reload
  46. @bar2.reload
  47. # Fix the order of receivers
  48. @agents.each do |agent|
  49. stub.proxy(agent).receivers { |orig| orig.order(:id) }
  50. end
  51. end
  52. it "generates a DOT script" do
  53. expect(agents_dot(@agents)).to match(%r{
  54. \A
  55. digraph \x20 "Agent \x20 Event \x20 Flow" \{
  56. node \[ [^\]]+ \];
  57. edge \[ [^\]]+ \];
  58. (?<foo>\w+) \[label=foo\];
  59. \k<foo> -> (?<bar1>\w+) \[style=dashed\];
  60. \k<foo> -> (?<bar2>\w+) \[color="\#999999"\];
  61. \k<bar1> \[label=bar1\];
  62. \k<bar2> \[label=bar2,style="rounded,dashed",color="\#999999",fontcolor="\#999999"\];
  63. \k<bar2> -> (?<bar3>\w+) \[style=dashed,color="\#999999"\];
  64. \k<bar3> \[label=bar3\];
  65. \}
  66. \z
  67. }x)
  68. end
  69. it "generates a richer DOT script" do
  70. expect(agents_dot(@agents, rich: true)).to match(%r{
  71. \A
  72. digraph \x20 "Agent \x20 Event \x20 Flow" \{
  73. (graph \[ [^\]]+ \];)?
  74. node \[ [^\]]+ \];
  75. edge \[ [^\]]+ \];
  76. (?<foo>\w+) \[label=foo,tooltip="Dot \x20 Foo",URL="#{Regexp.quote(agent_path(@foo))}"\];
  77. \k<foo> -> (?<bar1>\w+) \[style=dashed\];
  78. \k<foo> -> (?<bar2>\w+) \[color="\#999999"\];
  79. \k<bar1> \[label=bar1,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar1))}"\];
  80. \k<bar2> \[label=bar2,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar2))}",style="rounded,dashed",color="\#999999",fontcolor="\#999999"\];
  81. \k<bar2> -> (?<bar3>\w+) \[style=dashed,color="\#999999"\];
  82. \k<bar3> \[label=bar3,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar3))}"\];
  83. \}
  84. \z
  85. }x)
  86. end
  87. end
  88. end
  89. describe "DotHelper::DotDrawer" do
  90. describe "#id" do
  91. it "properly escapes double quotaion and backslash" do
  92. expect(DotHelper::DotDrawer.draw(foo: "") {
  93. id('hello\\"')
  94. }).to eq('"hello\\\\\\""')
  95. end
  96. end
  97. end
  98. end