graphomaton 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +74 -8
  3. data/README.md +426 -44
  4. data/SECURITY.md +47 -0
  5. data/docs/architecture.md +30 -0
  6. data/docs/cli.md +27 -0
  7. data/docs/custom-exporters.md +36 -0
  8. data/docs/exporters.md +17 -0
  9. data/docs/input-schema.md +26 -0
  10. data/docs/migration-1.1.md +19 -0
  11. data/docs/performance.md +19 -0
  12. data/docs/releasing.md +19 -0
  13. data/exe/graphomaton +9 -0
  14. data/lib/graphomaton/atomic_file.rb +26 -0
  15. data/lib/graphomaton/cli/config.rb +102 -0
  16. data/lib/graphomaton/cli.rb +841 -0
  17. data/lib/graphomaton/errors.rb +11 -0
  18. data/lib/graphomaton/exporter_registry.rb +127 -0
  19. data/lib/graphomaton/exporters/dot.rb +255 -18
  20. data/lib/graphomaton/exporters/mermaid.rb +705 -25
  21. data/lib/graphomaton/exporters/pdf.rb +131 -0
  22. data/lib/graphomaton/exporters/plantuml.rb +250 -13
  23. data/lib/graphomaton/exporters/png.rb +172 -0
  24. data/lib/graphomaton/exporters/svg.rb +2775 -231
  25. data/lib/graphomaton/exporters/webp.rb +185 -0
  26. data/lib/graphomaton/exporters.rb +11 -4
  27. data/lib/graphomaton/identifier_allocator.rb +33 -0
  28. data/lib/graphomaton/input_policy.rb +82 -0
  29. data/lib/graphomaton/layout/force_tree.rb +127 -0
  30. data/lib/graphomaton/model.rb +218 -0
  31. data/lib/graphomaton/process_runner.rb +154 -0
  32. data/lib/graphomaton/url_policy.rb +40 -0
  33. data/lib/graphomaton/version.rb +1 -1
  34. data/lib/graphomaton.rb +2869 -54
  35. data/sig/graphomaton.rbs +127 -0
  36. metadata +34 -24
  37. data/.codespellignore +0 -0
  38. data/.rspec +0 -1
  39. data/CODE_OF_CONDUCT.md +0 -132
  40. data/Rakefile +0 -8
  41. data/sample/basic.rb +0 -30
  42. data/sample/complex.rb +0 -32
  43. data/sample/long_names.rb +0 -20
  44. data/sample/nfa.rb +0 -28
  45. data/sample/skip_states.rb +0 -23
  46. data/spec/exporters/dot_spec.rb +0 -146
  47. data/spec/exporters/mermaid_spec.rb +0 -154
  48. data/spec/exporters/plantuml_spec.rb +0 -144
  49. data/spec/exporters/svg_spec.rb +0 -314
  50. data/spec/graphomaton_edge_cases_spec.rb +0 -322
  51. data/spec/graphomaton_spec.rb +0 -371
  52. data/spec/spec_helper.rb +0 -13
@@ -0,0 +1,127 @@
1
+ class Graphomaton
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ class ValidationError < Error
8
+ end
9
+
10
+ class ParseError < Error
11
+ end
12
+
13
+ class LayoutError < Error
14
+ end
15
+
16
+ class ExportError < Error
17
+ end
18
+
19
+ class ConversionError < ExportError
20
+ end
21
+
22
+ class SecurityError < Error
23
+ end
24
+
25
+ class Label < Data
26
+ attr_reader kind: Symbol
27
+ attr_reader value: untyped
28
+ def self.text: (untyped value) -> Label
29
+ def self.symbols: (*untyped values) -> Label
30
+ def self.epsilon: (?untyped display) -> Label
31
+ def self.uml: (event: untyped, ?guard: untyped, ?action: untyped) -> Label
32
+ def to_s: () -> String
33
+ def to_h: () -> Hash[Symbol, untyped]
34
+ end
35
+
36
+ class Diagnostic < Data
37
+ attr_reader code: String
38
+ attr_reader severity: Symbol
39
+ attr_reader path: Array[untyped]
40
+ attr_reader message: String
41
+ attr_reader hint: String?
42
+ def to_h: () -> Hash[Symbol, untyped]
43
+ end
44
+
45
+ class RenderOptions < Data
46
+ attr_reader format: Symbol
47
+ attr_reader width: Numeric
48
+ attr_reader height: Numeric
49
+ attr_reader options: Hash[Symbol, untyped]
50
+ end
51
+
52
+ class SvgOptions < Data
53
+ attr_reader width: Numeric
54
+ attr_reader height: Numeric
55
+ attr_reader options: Hash[Symbol, untyped]
56
+ end
57
+
58
+ class RenderResult < Data
59
+ attr_reader output: String
60
+ attr_reader diagnostics: Array[Diagnostic]
61
+ attr_reader bounds: Hash[Symbol, Numeric]?
62
+ attr_reader layout: Hash[untyped, Hash[Symbol, Numeric]]?
63
+ end
64
+
65
+ class State < Data
66
+ attr_reader id: untyped
67
+ attr_reader x: Numeric?
68
+ attr_reader y: Numeric?
69
+ attr_reader label: untyped
70
+ attr_reader style: Hash[untyped, untyped]?
71
+ attr_reader metadata: Hash[untyped, untyped]?
72
+ attr_reader shape: untyped
73
+ attr_reader kind: Symbol?
74
+ end
75
+
76
+ class Transition < Data
77
+ attr_reader id: Integer
78
+ attr_reader from: untyped
79
+ attr_reader to: untyped
80
+ attr_reader label: untyped
81
+ attr_reader style: Hash[untyped, untyped]?
82
+ attr_reader metadata: Hash[untyped, untyped]?
83
+ attr_reader line_style: untyped
84
+ end
85
+
86
+ def initialize: (?validation: Symbol) -> void
87
+ def states: () -> Hash[untyped, Hash[Symbol, untyped]]
88
+ def transitions: () -> Array[Hash[Symbol, untyped]]
89
+ def initial_state: () -> untyped
90
+ def final_states: () -> Array[untyped]
91
+ def revision: () -> Integer
92
+ def add_state: (untyped name, ?Numeric? x, ?Numeric? y, **untyped) -> self
93
+ def upsert_state: (untyped name, ?Numeric? x, ?Numeric? y, **untyped) -> self
94
+ def update_state: (untyped name, **untyped) -> self
95
+ def remove_state: (untyped name, ?cascade: bool) -> self
96
+ def add_transition: (untyped from, untyped to, untyped label, **untyped) -> self
97
+ def update_transition: (untyped identifier, **untyped) -> self
98
+ def remove_transition: (untyped identifier) -> self
99
+ def set_initial: (untyped state) -> self
100
+ def clear_initial: () -> self
101
+ def add_final: (untyped state) -> self
102
+ def remove_final: (untyped state) -> self
103
+ def validation_errors: (?profile: Symbol | Array[Symbol]) -> Array[String]
104
+ def validation_diagnostics: (?profile: Symbol | Array[Symbol]) -> Array[Diagnostic]
105
+ def validate!: (?profile: Symbol | Array[Symbol]) -> true
106
+ def valid?: (?profile: Symbol | Array[Symbol]) -> bool
107
+ def to_h: () -> Hash[Symbol, untyped]
108
+ def to_json: (*untyped) -> String
109
+ def to_yaml: (**untyped) -> String
110
+ def render: (?format: Symbol | String, ?width: Numeric, ?height: Numeric, ?strict_semantics: bool, **untyped) -> String
111
+ def render_result: (?format: Symbol | String, ?width: Numeric, ?height: Numeric, ?strict_semantics: bool, **untyped) -> RenderResult
112
+ def render_with: (RenderOptions options) -> String
113
+ def reachable_states: () -> Array[untyped]
114
+ def reachable_from: (untyped state) -> Array[untyped]
115
+ def graph_roots: () -> Array[untyped]
116
+ def weakly_connected_components: () -> Array[Array[untyped]]
117
+ def sink_states: () -> Array[untyped]
118
+ def self_loop_traps: () -> Array[untyped]
119
+ def bottom_sccs: () -> Array[Array[untyped]]
120
+ def write: (_Writer io, ?format: Symbol | String, ?width: Numeric, ?height: Numeric, **untyped) -> Integer
121
+
122
+ def self.from_hash: (Hash[untyped, untyped] data, **untyped) -> Graphomaton
123
+ def self.from_json: (String | _Reader source, **untyped) -> Graphomaton
124
+ def self.from_yaml: (String | _Reader source, **untyped) -> Graphomaton
125
+ def self.register_exporter: (Symbol | String name, **untyped) -> untyped
126
+ | (Symbol | String name, **untyped) { () -> untyped } -> untyped
127
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphomaton
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yudai Takada
@@ -23,48 +23,58 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '3.4'
26
- description: Graphomaton is a lightweight Ruby library for creating and visualizing
27
- finite state machines. It supports multiple output formats including SVG, Mermaid.js,
28
- GraphViz DOT, and PlantUML, making it easy to generate professional automaton diagrams.
26
+ description: Graphomaton creates, analyzes, lays out, and exports finite state machines
27
+ as SVG, PNG, PDF, WebP, Mermaid.js, GraphViz DOT, PlantUML, and standalone HTML.
29
28
  email:
30
29
  - t.yudai92@gmail.com
31
- executables: []
30
+ executables:
31
+ - graphomaton
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
- - ".codespellignore"
36
- - ".rspec"
37
35
  - CHANGELOG.md
38
- - CODE_OF_CONDUCT.md
39
36
  - LICENSE.txt
40
37
  - README.md
41
- - Rakefile
38
+ - SECURITY.md
39
+ - docs/architecture.md
40
+ - docs/cli.md
41
+ - docs/custom-exporters.md
42
+ - docs/exporters.md
43
+ - docs/input-schema.md
44
+ - docs/migration-1.1.md
45
+ - docs/performance.md
46
+ - docs/releasing.md
47
+ - exe/graphomaton
42
48
  - lib/graphomaton.rb
49
+ - lib/graphomaton/atomic_file.rb
50
+ - lib/graphomaton/cli.rb
51
+ - lib/graphomaton/cli/config.rb
52
+ - lib/graphomaton/errors.rb
53
+ - lib/graphomaton/exporter_registry.rb
43
54
  - lib/graphomaton/exporters.rb
44
55
  - lib/graphomaton/exporters/dot.rb
45
56
  - lib/graphomaton/exporters/mermaid.rb
57
+ - lib/graphomaton/exporters/pdf.rb
46
58
  - lib/graphomaton/exporters/plantuml.rb
59
+ - lib/graphomaton/exporters/png.rb
47
60
  - lib/graphomaton/exporters/svg.rb
61
+ - lib/graphomaton/exporters/webp.rb
62
+ - lib/graphomaton/identifier_allocator.rb
63
+ - lib/graphomaton/input_policy.rb
64
+ - lib/graphomaton/layout/force_tree.rb
65
+ - lib/graphomaton/model.rb
66
+ - lib/graphomaton/process_runner.rb
67
+ - lib/graphomaton/url_policy.rb
48
68
  - lib/graphomaton/version.rb
49
- - sample/basic.rb
50
- - sample/complex.rb
51
- - sample/long_names.rb
52
- - sample/nfa.rb
53
- - sample/skip_states.rb
54
- - spec/exporters/dot_spec.rb
55
- - spec/exporters/mermaid_spec.rb
56
- - spec/exporters/plantuml_spec.rb
57
- - spec/exporters/svg_spec.rb
58
- - spec/graphomaton_edge_cases_spec.rb
59
- - spec/graphomaton_spec.rb
60
- - spec/spec_helper.rb
69
+ - sig/graphomaton.rbs
61
70
  homepage: https://github.com/ydah/graphomaton
62
71
  licenses:
63
72
  - MIT
64
73
  metadata:
65
- homepage_uri: https://github.com/ydah/graphomaton
66
74
  source_code_uri: https://github.com/ydah/graphomaton
67
75
  changelog_uri: https://github.com/ydah/graphomaton/blob/main/CHANGELOG.md
76
+ documentation_uri: https://rubydoc.info/gems/graphomaton
77
+ bug_tracker_uri: https://github.com/ydah/graphomaton/issues
68
78
  rubygems_mfa_required: 'true'
69
79
  rdoc_options: []
70
80
  require_paths:
@@ -80,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
90
  - !ruby/object:Gem::Version
81
91
  version: '0'
82
92
  requirements: []
83
- rubygems_version: 3.6.9
93
+ rubygems_version: 4.0.6
84
94
  specification_version: 4
85
- summary: A tiny Ruby library for generating finite state machine (automaton) diagrams.
95
+ summary: Generate and analyze finite state machine diagrams in multiple formats.
86
96
  test_files: []
data/.codespellignore DELETED
File without changes
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --require spec_helper
data/CODE_OF_CONDUCT.md DELETED
@@ -1,132 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, caste, color, religion, or sexual
10
- identity and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the overall
26
- community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or advances of
31
- any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email address,
35
- without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official email address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- [INSERT CONTACT METHOD].
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series of
86
- actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or permanent
93
- ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within the
113
- community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.1, available at
119
- [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
-
121
- Community Impact Guidelines were inspired by
122
- [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
-
124
- For answers to common questions about this code of conduct, see the FAQ at
125
- [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
- [https://www.contributor-covenant.org/translations][translations].
127
-
128
- [homepage]: https://www.contributor-covenant.org
129
- [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
- [Mozilla CoC]: https://github.com/mozilla/diversity
131
- [FAQ]: https://www.contributor-covenant.org/faq
132
- [translations]: https://www.contributor-covenant.org/translations
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/sample/basic.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/graphomaton'
4
-
5
- automaton = Graphomaton.new
6
-
7
- automaton.add_state('待機中')
8
- automaton.add_state('お金投入済み')
9
- automaton.add_state('商品選択済み')
10
-
11
- automaton.set_initial('待機中')
12
- automaton.add_final('待機中')
13
-
14
- automaton.add_transition('待機中', 'お金投入済み', 'お金を投入する')
15
- automaton.add_transition('お金投入済み', '商品選択済み', '商品のボタンを押す')
16
- automaton.add_transition('商品選択済み', '待機中', '商品を排出する')
17
-
18
- automaton.save_svg('automaton.svg')
19
- puts 'Generated automaton.svg'
20
-
21
- automaton.save_html('automaton.html')
22
- puts 'Generated automaton.html (Mermaid.js - requires internet connection)'
23
-
24
- automaton.save_dot('automaton.dot')
25
- puts 'Generated automaton.dot (GraphViz format)'
26
- puts 'To convert to PNG: dot -Tpng automaton.dot -o automaton.png'
27
-
28
- automaton.save_plantuml('automaton.puml')
29
- puts 'Generated automaton.puml (PlantUML format)'
30
- puts 'To convert to PNG: Use PlantUML server or jar file'
data/sample/complex.rb DELETED
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/graphomaton'
4
-
5
- complex = Graphomaton.new
6
-
7
- complex.add_state('A')
8
- complex.add_state('B')
9
- complex.add_state('C')
10
-
11
- complex.set_initial('A')
12
- complex.add_final('C')
13
-
14
- complex.add_transition('A', 'B', 'x')
15
- complex.add_transition('A', 'B', 'y')
16
-
17
- complex.add_transition('B', 'C', 'a')
18
- complex.add_transition('C', 'B', 'b')
19
-
20
- complex.add_transition('B', 'B', 'loop')
21
-
22
- complex.save_svg('complex_transitions.svg')
23
- puts 'Generated complex_transitions.svg'
24
-
25
- complex.save_html('complex_transitions.html')
26
- puts 'Generated complex_transitions.html'
27
-
28
- complex.save_dot('complex_transitions.dot')
29
- puts 'Generated complex_transitions.dot'
30
-
31
- complex.save_plantuml('complex_transitions.puml')
32
- puts 'Generated complex_transitions.puml'
data/sample/long_names.rb DELETED
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/graphomaton'
4
-
5
- automaton = Graphomaton.new
6
-
7
- automaton.add_state('A')
8
- automaton.add_state('Short')
9
- automaton.add_state('Medium Name')
10
- automaton.add_state('VeryLongStateName')
11
-
12
- automaton.set_initial('A')
13
- automaton.add_final('VeryLongStateName')
14
-
15
- automaton.add_transition('A', 'Short', 'go')
16
- automaton.add_transition('Short', 'Medium Name', 'next')
17
- automaton.add_transition('Medium Name', 'VeryLongStateName', 'final')
18
-
19
- automaton.save_svg('long_names.svg')
20
- puts 'Generated long_names.svg'
data/sample/nfa.rb DELETED
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/graphomaton'
4
-
5
- nfa = Graphomaton.new
6
-
7
- nfa.add_state('q0')
8
- nfa.add_state('q1')
9
- nfa.add_state('q2')
10
-
11
- nfa.set_initial('q0')
12
- nfa.add_final('q2')
13
-
14
- nfa.add_transition('q0', 'q0', 'a,b')
15
- nfa.add_transition('q0', 'q1', 'a')
16
- nfa.add_transition('q1', 'q2', 'b')
17
-
18
- nfa.save_svg('nfa_example.svg', 600, 400)
19
- puts 'Generated nfa_example.svg'
20
-
21
- nfa.save_html('nfa_example.html')
22
- puts 'Generated nfa_example.html'
23
-
24
- nfa.save_dot('nfa_example.dot')
25
- puts 'Generated nfa_example.dot'
26
-
27
- nfa.save_plantuml('nfa_example.puml')
28
- puts 'Generated nfa_example.puml'
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/graphomaton'
4
-
5
- automaton = Graphomaton.new
6
-
7
- automaton.add_state('A')
8
- automaton.add_state('B')
9
- automaton.add_state('C')
10
- automaton.add_state('D')
11
-
12
- automaton.set_initial('A')
13
- automaton.add_final('D')
14
-
15
- automaton.add_transition('A', 'B', '1 step')
16
- automaton.add_transition('A', 'C', 'skip 1 state')
17
- automaton.add_transition('A', 'D', 'skip 2 states')
18
- automaton.add_transition('B', 'C', '1 step')
19
- automaton.add_transition('C', 'D', '1 step')
20
- automaton.add_transition('D', 'A', 'back to start')
21
-
22
- automaton.save_svg('skip_states.svg')
23
- puts 'Generated skip_states.svg'
@@ -1,146 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'graphomaton'
4
-
5
- RSpec.describe Graphomaton::Exporters::Dot do
6
- let(:automaton) { Graphomaton.new }
7
- let(:dot_exporter) { described_class.new(automaton) }
8
-
9
- describe '#export' do
10
- context 'with empty automaton' do
11
- it 'generates valid DOT syntax' do
12
- dot_output = dot_exporter.export
13
- expect(dot_output).to include('digraph')
14
- expect(dot_output).to include('rankdir=LR')
15
- end
16
-
17
- it 'includes basic graph attributes' do
18
- dot_output = dot_exporter.export
19
- expect(dot_output).to match(/node\s+\[shape\s+=\s+circle\]/)
20
- end
21
- end
22
-
23
- context 'with states' do
24
- before do
25
- automaton.add_state('A')
26
- automaton.add_state('B')
27
- automaton.add_state('C')
28
- automaton.add_transition('A', 'B', 'x')
29
- end
30
-
31
- it 'includes all states in transitions' do
32
- dot_output = dot_exporter.export
33
- expect(dot_output).to include('"A"')
34
- expect(dot_output).to include('"B"')
35
- end
36
-
37
- it 'marks final states with double circle' do
38
- automaton.add_final('C')
39
- dot_output = dot_exporter.export
40
- expect(dot_output).to match(/node\s+\[shape\s+=\s+doublecircle\];\s+"C"/)
41
- end
42
- end
43
-
44
- context 'with initial state' do
45
- before do
46
- automaton.add_state('Start')
47
- automaton.set_initial('Start')
48
- end
49
-
50
- it 'creates invisible initial node' do
51
- dot_output = dot_exporter.export
52
- expect(dot_output).to include('__start__')
53
- expect(dot_output).to match(/__start__\s+\[shape=point\]/)
54
- end
55
-
56
- it 'creates arrow from invisible node to initial state' do
57
- dot_output = dot_exporter.export
58
- expect(dot_output).to include('__start__ -> "Start"')
59
- end
60
- end
61
-
62
- context 'with transitions' do
63
- before do
64
- automaton.add_state('A')
65
- automaton.add_state('B')
66
- automaton.add_state('C')
67
- automaton.add_transition('A', 'B', 'input_a')
68
- automaton.add_transition('B', 'C', 'input_b')
69
- end
70
-
71
- it 'includes all transitions' do
72
- dot_output = dot_exporter.export
73
- expect(dot_output).to include('"A" -> "B"')
74
- expect(dot_output).to include('"B" -> "C"')
75
- end
76
-
77
- it 'includes transition labels' do
78
- dot_output = dot_exporter.export
79
- expect(dot_output).to match(/"A"\s+->\s+"B"\s+\[label="input_a"\]/)
80
- expect(dot_output).to match(/"B"\s+->\s+"C"\s+\[label="input_b"\]/)
81
- end
82
-
83
- it 'handles self-loops' do
84
- automaton.add_transition('B', 'B', 'loop')
85
- dot_output = dot_exporter.export
86
- expect(dot_output).to match(/"B"\s+->\s+"B"\s+\[label="loop"\]/)
87
- end
88
- end
89
-
90
- context 'with special characters in labels' do
91
- before do
92
- automaton.add_state('State 1')
93
- automaton.add_state('State-2')
94
- automaton.add_transition('State 1', 'State-2', 'a/b')
95
- end
96
-
97
- it 'handles spaces in state names' do
98
- dot_output = dot_exporter.export
99
- expect(dot_output).to include('"State 1"')
100
- expect(dot_output).to include('"State-2"')
101
- end
102
-
103
- it 'handles special characters in labels' do
104
- dot_output = dot_exporter.export
105
- expect(dot_output).to match(/label="a\/b"/)
106
- end
107
- end
108
-
109
- context 'with complete automaton' do
110
- before do
111
- automaton.add_state('q0')
112
- automaton.add_state('q1')
113
- automaton.add_state('q2')
114
- automaton.set_initial('q0')
115
- automaton.add_final('q2')
116
- automaton.add_transition('q0', 'q1', 'a')
117
- automaton.add_transition('q1', 'q2', 'b')
118
- automaton.add_transition('q2', 'q0', 'c')
119
- end
120
-
121
- it 'generates complete valid DOT graph' do
122
- dot_output = dot_exporter.export
123
-
124
- # Should contain digraph wrapper
125
- expect(dot_output).to start_with('digraph')
126
- expect(dot_output).to end_with('}')
127
-
128
- # Should contain all states in transitions
129
- expect(dot_output).to include('"q0"')
130
- expect(dot_output).to include('"q1"')
131
- expect(dot_output).to include('"q2"')
132
-
133
- # Should mark final state
134
- expect(dot_output).to match(/node\s+\[shape\s+=\s+doublecircle\];\s+"q2"/)
135
-
136
- # Should have initial arrow
137
- expect(dot_output).to include('__start__ -> "q0"')
138
-
139
- # Should have all transitions
140
- expect(dot_output).to include('"q0" -> "q1"')
141
- expect(dot_output).to include('"q1" -> "q2"')
142
- expect(dot_output).to include('"q2" -> "q0"')
143
- end
144
- end
145
- end
146
- end