dot_helper_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. end
  48. it "generates a DOT script" do
  49. expect(agents_dot(@agents)).to match(%r{
  50. \A
  51. digraph \x20 "Agent \x20 Event \x20 Flow" \{
  52. node \[ [^\]]+ \];
  53. edge \[ [^\]]+ \];
  54. (?<foo>\w+) \[label=foo\];
  55. \k<foo> -> (?<bar1>\w+) \[style=dashed\];
  56. \k<foo> -> (?<bar2>\w+) \[color="\#999999"\];
  57. \k<bar1> \[label=bar1\];
  58. \k<bar2> \[label=bar2,style="rounded,dashed",color="\#999999",fontcolor="\#999999"\];
  59. \k<bar2> -> (?<bar3>\w+) \[style=dashed,color="\#999999"\];
  60. \k<bar3> \[label=bar3\];
  61. \}
  62. \z
  63. }x)
  64. end
  65. it "generates a richer DOT script" do
  66. expect(agents_dot(@agents, rich: true)).to match(%r{
  67. \A
  68. digraph \x20 "Agent \x20 Event \x20 Flow" \{
  69. node \[ [^\]]+ \];
  70. edge \[ [^\]]+ \];
  71. (?<foo>\w+) \[label=foo,tooltip="Dot \x20 Foo",URL="#{Regexp.quote(agent_path(@foo))}"\];
  72. \k<foo> -> (?<bar1>\w+) \[style=dashed\];
  73. \k<foo> -> (?<bar2>\w+) \[color="\#999999"\];
  74. \k<bar1> \[label=bar1,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar1))}"\];
  75. \k<bar2> \[label=bar2,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar2))}",style="rounded,dashed",color="\#999999",fontcolor="\#999999"\];
  76. \k<bar2> -> (?<bar3>\w+) \[style=dashed,color="\#999999"\];
  77. \k<bar3> \[label=bar3,tooltip="Dot \x20 Bar",URL="#{Regexp.quote(agent_path(@bar3))}"\];
  78. \}
  79. \z
  80. }x)
  81. end
  82. end
  83. end
  84. describe "DotHelper::DotDrawer" do
  85. describe "#id" do
  86. it "properly escapes double quotaion and backslash" do
  87. expect(DotHelper::DotDrawer.draw(foo: "") {
  88. id('hello\\"')
  89. }).to eq('"hello\\\\\\""')
  90. end
  91. end
  92. end
  93. end