rich-ruby 1.0.1 → 1.0.2

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.
data/examples/verify.rb CHANGED
@@ -1,215 +1,216 @@
1
- # frozen_string_literal: true
2
-
3
- # Full verification test for the Rich library
4
-
5
- require_relative "../lib/rich"
6
-
7
- puts "=" * 60
8
- puts "Rich Library Verification Test"
9
- puts "=" * 60
10
- puts ""
11
-
12
- # Track results
13
- results = []
14
-
15
- def test(name)
16
- print "Testing #{name}... "
17
- begin
18
- yield
19
- puts "✓ PASS"
20
- true
21
- rescue StandardError => e
22
- puts "✗ FAIL: #{e.message}"
23
- false
24
- end
25
- end
26
-
27
- # Test 1: Basic imports
28
- results << test("Module loading") do
29
- raise "Console not defined" unless defined?(Rich::Console)
30
- raise "Color not defined" unless defined?(Rich::Color)
31
- raise "Style not defined" unless defined?(Rich::Style)
32
- raise "Text not defined" unless defined?(Rich::Text)
33
- raise "Panel not defined" unless defined?(Rich::Panel)
34
- raise "Table not defined" unless defined?(Rich::Table)
35
- raise "Tree not defined" unless defined?(Rich::Tree)
36
- raise "Progress not defined" unless defined?(Rich::ProgressBar)
37
- end
38
-
39
- # Test 2: ColorTriplet
40
- results << test("ColorTriplet") do
41
- triplet = Rich::ColorTriplet.new(255, 128, 64)
42
- raise "Hex wrong" unless triplet.hex == "#ff8040"
43
- raise "RGB wrong" unless triplet.red == 255 && triplet.green == 128
44
-
45
- from_hex = Rich::ColorTriplet.from_hex("#00ff00")
46
- raise "From hex wrong" unless from_hex.green == 255
47
- end
48
-
49
- # Test 3: Color parsing
50
- results << test("Color parsing") do
51
- red = Rich::Color.parse("red")
52
- raise "Red not parsed" unless red.type == Rich::ColorType::STANDARD
53
-
54
- hex = Rich::Color.parse("#ff5500")
55
- raise "Hex not parsed" unless hex.type == Rich::ColorType::TRUECOLOR
56
-
57
- named = Rich::Color.parse("bright_blue")
58
- raise "Named not parsed" unless named.number == 12
59
- end
60
-
61
- # Test 4: Style parsing
62
- results << test("Style parsing") do
63
- style = Rich::Style.parse("bold red on white")
64
- raise "Bold not set" unless style.bold?
65
- raise "Color wrong" unless style.color.name == "red"
66
- raise "Bgcolor wrong" unless style.bgcolor.name == "white"
67
- end
68
-
69
- # Test 5: Style combination
70
- results << test("Style combination") do
71
- s1 = Rich::Style.parse("bold")
72
- s2 = Rich::Style.parse("red")
73
- combined = s1 + s2
74
- raise "Combination failed" unless combined.bold? && combined.color.name == "red"
75
- end
76
-
77
- # Test 6: Console creation
78
- results << test("Console creation") do
79
- console = Rich::Console.new
80
- raise "Width missing" unless console.width > 0
81
- raise "Height missing" unless console.height > 0
82
- raise "Color system missing" unless Rich::ColorSystem::ALL.include?(console.color_system)
83
- end
84
-
85
- # Test 7: Cell width calculation
86
- results << test("Cell width calculation") do
87
- raise "ASCII wrong" unless Rich::Cells.cell_len("Hello") == 5
88
- raise "CJK wrong" unless Rich::Cells.cell_len("你好") == 4 # 2 chars × 2 width
89
- raise "Empty wrong" unless Rich::Cells.cell_len("") == 0
90
- end
91
-
92
- # Test 8: Segment creation
93
- results << test("Segment creation") do
94
- seg = Rich::Segment.new("Hello", style: Rich::Style.parse("bold"))
95
- raise "Text wrong" unless seg.text == "Hello"
96
- raise "Style missing" unless seg.style.bold?
97
- end
98
-
99
- # Test 9: Text with spans
100
- results << test("Text with spans") do
101
- text = Rich::Text.new("Hello ")
102
- text.append("World", style: "red")
103
- raise "Plain wrong" unless text.plain == "Hello World"
104
- raise "Spans wrong" unless text.spans.length == 1
105
- end
106
-
107
- # Test 10: Markup parsing
108
- results << test("Markup parsing") do
109
- text = Rich::Markup.parse("[bold]Hello[/bold]")
110
- raise "Parse failed" unless text.plain == "Hello"
111
- raise "Style not applied" unless text.spans.any? { |s| s.style.bold? }
112
- end
113
-
114
- # Test 11: Panel rendering
115
- results << test("Panel rendering") do
116
- panel = Rich::Panel.new("Content", title: "Title")
117
- output = panel.render(max_width: 40)
118
- raise "No border" unless output.include?("╭") || output.include?("+")
119
- raise "No content" unless output.include?("Content")
120
- end
121
-
122
- # Test 12: Table rendering
123
- results << test("Table rendering") do
124
- table = Rich::Table.new
125
- table.add_column("Name")
126
- table.add_column("Value")
127
- table.add_row("Key", "123")
128
- output = table.render(max_width: 40)
129
- raise "No header" unless output.include?("Name")
130
- raise "No data" unless output.include?("123")
131
- end
132
-
133
- # Test 13: Tree rendering
134
- results << test("Tree rendering") do
135
- tree = Rich::Tree.new("Root")
136
- tree.add("Child 1")
137
- tree.add("Child 2")
138
- output = tree.render
139
- raise "No root" unless output.include?("Root")
140
- raise "No children" unless output.include?("Child")
141
- end
142
-
143
- # Test 14: Progress bar
144
- results << test("Progress bar") do
145
- bar = Rich::ProgressBar.new(total: 100, completed: 50)
146
- raise "Progress wrong" unless bar.progress == 0.5
147
- raise "Percentage wrong" unless bar.percentage == 50
148
-
149
- bar.advance(25)
150
- raise "Advance failed" unless bar.percentage == 75
151
- end
152
-
153
- # Test 15: Spinner
154
- results << test("Spinner") do
155
- spinner = Rich::Spinner.new
156
- frame1 = spinner.frame
157
- spinner.advance
158
- frame2 = spinner.frame
159
- raise "Spinner not advancing" if frame1 == frame2 && spinner.frames.length > 1
160
- end
161
-
162
- # Test 16: Box styles
163
- results << test("Box styles") do
164
- boxes = [Rich::Box::ASCII, Rich::Box::ROUNDED, Rich::Box::HEAVY, Rich::Box::DOUBLE]
165
- boxes.each do |box|
166
- raise "Missing top_left" if box.top_left.nil?
167
- raise "Missing horizontal" if box.horizontal.nil?
168
- end
169
- end
170
-
171
- # Test 17: JSON highlighting
172
- results << test("JSON highlighting") do
173
- data = { "name" => "test", "value" => 123 }
174
- output = Rich::JSON.to_s(data)
175
- raise "No output" if output.empty?
176
- end
177
-
178
- # Test 18: Columns layout
179
- results << test("Columns layout") do
180
- cols = Rich::Columns.new(%w[one two three four])
181
- output = cols.render(max_width: 40)
182
- raise "No content" unless output.include?("one")
183
- end
184
-
185
- # Test 19: Windows Console API (on Windows)
186
- if Gem.win_platform?
187
- results << test("Windows Console API") do
188
- raise "Win32Console not loaded" unless defined?(Rich::Win32Console)
189
- raise "supports_ansi? missing" unless Rich::Win32Console.respond_to?(:supports_ansi?)
190
-
191
- # Get console size
192
- size = Rich::Win32Console.get_size
193
- raise "Size detection failed" unless size && size[0] > 0
194
- end
195
- end
196
-
197
- # Test 20: Control codes
198
- results << test("Control codes") do
199
- raise "Clear missing" if Rich::Control.clear_screen.empty?
200
- raise "Cursor up missing" if Rich::Control.cursor_up(5).empty?
201
- raise "Reset missing" if Rich::Control.reset.empty?
202
- end
203
-
204
- puts ""
205
- puts "=" * 60
206
- passed = results.count(true)
207
- total = results.length
208
- puts "Results: #{passed}/#{total} tests passed"
209
- puts "=" * 60
210
-
211
- if passed == total
212
- puts "\n🎉 All tests passed! Rich library is fully functional."
213
- else
214
- puts "\n⚠️ Some tests failed. Please review the output above."
215
- end
1
+ # frozen_string_literal: true
2
+
3
+ # Full verification test for the Rich library
4
+
5
+ require_relative "../lib/rich"
6
+
7
+ puts "=" * 60
8
+ puts "Rich Library Verification Test"
9
+ puts "=" * 60
10
+ puts ""
11
+
12
+ # Track results
13
+ results = []
14
+
15
+ def test(name)
16
+ print "Testing #{name}... "
17
+ begin
18
+ yield
19
+ puts "✓ PASS"
20
+ true
21
+ rescue StandardError => e
22
+ puts "✗ FAIL: #{e.message}"
23
+ false
24
+ end
25
+ end
26
+
27
+ # Test 1: Basic imports
28
+ results << test("Module loading") do
29
+ raise "Console not defined" unless defined?(Rich::Console)
30
+ raise "Color not defined" unless defined?(Rich::Color)
31
+ raise "Style not defined" unless defined?(Rich::Style)
32
+ raise "Text not defined" unless defined?(Rich::Text)
33
+ raise "Panel not defined" unless defined?(Rich::Panel)
34
+ raise "Table not defined" unless defined?(Rich::Table)
35
+ raise "Tree not defined" unless defined?(Rich::Tree)
36
+ raise "Progress not defined" unless defined?(Rich::ProgressBar)
37
+ end
38
+
39
+ # Test 2: ColorTriplet
40
+ results << test("ColorTriplet") do
41
+ triplet = Rich::ColorTriplet.new(255, 128, 64)
42
+ raise "Hex wrong" unless triplet.hex == "ff8040"
43
+ raise "Hex-with-hash wrong" unless triplet.hex_with_hash == "#ff8040"
44
+ raise "RGB wrong" unless triplet.red == 255 && triplet.green == 128
45
+
46
+ from_hex = Rich::ColorTriplet.from_hex("#00ff00")
47
+ raise "From hex wrong" unless from_hex.green == 255
48
+ end
49
+
50
+ # Test 3: Color parsing
51
+ results << test("Color parsing") do
52
+ red = Rich::Color.parse("red")
53
+ raise "Red not parsed" unless red.type == Rich::ColorType::STANDARD
54
+
55
+ hex = Rich::Color.parse("#ff5500")
56
+ raise "Hex not parsed" unless hex.type == Rich::ColorType::TRUECOLOR
57
+
58
+ named = Rich::Color.parse("bright_blue")
59
+ raise "Named not parsed" unless named.number == 12
60
+ end
61
+
62
+ # Test 4: Style parsing
63
+ results << test("Style parsing") do
64
+ style = Rich::Style.parse("bold red on white")
65
+ raise "Bold not set" unless style.bold?
66
+ raise "Color wrong" unless style.color.name == "red"
67
+ raise "Bgcolor wrong" unless style.bgcolor.name == "white"
68
+ end
69
+
70
+ # Test 5: Style combination
71
+ results << test("Style combination") do
72
+ s1 = Rich::Style.parse("bold")
73
+ s2 = Rich::Style.parse("red")
74
+ combined = s1 + s2
75
+ raise "Combination failed" unless combined.bold? && combined.color.name == "red"
76
+ end
77
+
78
+ # Test 6: Console creation
79
+ results << test("Console creation") do
80
+ console = Rich::Console.new
81
+ raise "Width missing" unless console.width > 0
82
+ raise "Height missing" unless console.height > 0
83
+ raise "Color system missing" unless Rich::ColorSystem::ALL.include?(console.color_system)
84
+ end
85
+
86
+ # Test 7: Cell width calculation
87
+ results << test("Cell width calculation") do
88
+ raise "ASCII wrong" unless Rich::Cells.cell_len("Hello") == 5
89
+ raise "CJK wrong" unless Rich::Cells.cell_len("你好") == 4 # 2 chars × 2 width
90
+ raise "Empty wrong" unless Rich::Cells.cell_len("") == 0
91
+ end
92
+
93
+ # Test 8: Segment creation
94
+ results << test("Segment creation") do
95
+ seg = Rich::Segment.new("Hello", style: Rich::Style.parse("bold"))
96
+ raise "Text wrong" unless seg.text == "Hello"
97
+ raise "Style missing" unless seg.style.bold?
98
+ end
99
+
100
+ # Test 9: Text with spans
101
+ results << test("Text with spans") do
102
+ text = Rich::Text.new("Hello ")
103
+ text.append("World", style: "red")
104
+ raise "Plain wrong" unless text.plain == "Hello World"
105
+ raise "Spans wrong" unless text.spans.length == 1
106
+ end
107
+
108
+ # Test 10: Markup parsing
109
+ results << test("Markup parsing") do
110
+ text = Rich::Markup.parse("[bold]Hello[/bold]")
111
+ raise "Parse failed" unless text.plain == "Hello"
112
+ raise "Style not applied" unless text.spans.any? { |s| s.style.bold? }
113
+ end
114
+
115
+ # Test 11: Panel rendering
116
+ results << test("Panel rendering") do
117
+ panel = Rich::Panel.new("Content", title: "Title")
118
+ output = panel.render(max_width: 40)
119
+ raise "No border" unless output.include?("") || output.include?("+")
120
+ raise "No content" unless output.include?("Content")
121
+ end
122
+
123
+ # Test 12: Table rendering
124
+ results << test("Table rendering") do
125
+ table = Rich::Table.new
126
+ table.add_column("Name")
127
+ table.add_column("Value")
128
+ table.add_row("Key", "123")
129
+ output = table.render(max_width: 40)
130
+ raise "No header" unless output.include?("Name")
131
+ raise "No data" unless output.include?("123")
132
+ end
133
+
134
+ # Test 13: Tree rendering
135
+ results << test("Tree rendering") do
136
+ tree = Rich::Tree.new("Root")
137
+ tree.add("Child 1")
138
+ tree.add("Child 2")
139
+ output = tree.render
140
+ raise "No root" unless output.include?("Root")
141
+ raise "No children" unless output.include?("Child")
142
+ end
143
+
144
+ # Test 14: Progress bar
145
+ results << test("Progress bar") do
146
+ bar = Rich::ProgressBar.new(total: 100, completed: 50)
147
+ raise "Progress wrong" unless bar.progress == 0.5
148
+ raise "Percentage wrong" unless bar.percentage == 50
149
+
150
+ bar.advance(25)
151
+ raise "Advance failed" unless bar.percentage == 75
152
+ end
153
+
154
+ # Test 15: Spinner
155
+ results << test("Spinner") do
156
+ spinner = Rich::Spinner.new
157
+ frame1 = spinner.frame
158
+ spinner.advance
159
+ frame2 = spinner.frame
160
+ raise "Spinner not advancing" if frame1 == frame2 && spinner.frames.length > 1
161
+ end
162
+
163
+ # Test 16: Box styles
164
+ results << test("Box styles") do
165
+ boxes = [Rich::Box::ASCII, Rich::Box::ROUNDED, Rich::Box::HEAVY, Rich::Box::DOUBLE]
166
+ boxes.each do |box|
167
+ raise "Missing top_left" if box.top_left.nil?
168
+ raise "Missing horizontal" if box.horizontal.nil?
169
+ end
170
+ end
171
+
172
+ # Test 17: JSON highlighting
173
+ results << test("JSON highlighting") do
174
+ data = { "name" => "test", "value" => 123 }
175
+ output = Rich::JSON.to_s(data)
176
+ raise "No output" if output.empty?
177
+ end
178
+
179
+ # Test 18: Columns layout
180
+ results << test("Columns layout") do
181
+ cols = Rich::Columns.new(%w[one two three four])
182
+ output = cols.render(max_width: 40)
183
+ raise "No content" unless output.include?("one")
184
+ end
185
+
186
+ # Test 19: Windows Console API (on Windows)
187
+ if Gem.win_platform?
188
+ results << test("Windows Console API") do
189
+ raise "Win32Console not loaded" unless defined?(Rich::Win32Console)
190
+ raise "supports_ansi? missing" unless Rich::Win32Console.respond_to?(:supports_ansi?)
191
+
192
+ # Get console size
193
+ size = Rich::Win32Console.get_size
194
+ raise "Size detection failed" unless size && size[0] > 0
195
+ end
196
+ end
197
+
198
+ # Test 20: Control codes
199
+ results << test("Control codes") do
200
+ raise "Clear missing" if Rich::Control.clear_screen.empty?
201
+ raise "Cursor up missing" if Rich::Control.cursor_up(5).empty?
202
+ raise "Reset missing" if Rich::Control.reset.empty?
203
+ end
204
+
205
+ puts ""
206
+ puts "=" * 60
207
+ passed = results.count(true)
208
+ total = results.length
209
+ puts "Results: #{passed}/#{total} tests passed"
210
+ puts "=" * 60
211
+
212
+ if passed == total
213
+ puts "\n🎉 All tests passed! Rich library is fully functional."
214
+ else
215
+ puts "\n⚠️ Some tests failed. Please review the output above."
216
+ end