-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathintegration_spec.rb
More file actions
502 lines (410 loc) · 14.9 KB
/
integration_spec.rb
File metadata and controls
502 lines (410 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# frozen_string_literal: true
require "spec_helper"
require "tempfile"
require "fileutils"
RSpec.describe "T-Ruby E2E Integration" do
let(:tmpdir) { Dir.mktmpdir("trb_e2e") }
after do
FileUtils.rm_rf(tmpdir)
end
describe "Full compilation pipeline" do
it "compiles a complete T-Ruby project" do
# Create a mini project structure
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
# Create trbconfig.yml config to set output dir
File.write(File.join(tmpdir, "trbconfig.yml"), <<~YAML)
emit:
rb: true
rbs: false
dtrb: false
paths:
src: "#{lib_dir}"
out: "#{lib_dir}"
YAML
# Main application file
File.write(File.join(lib_dir, "app.trb"), <<~TRB)
# Application entry point
type AppConfig = {
name: String,
version: String,
debug: Boolean
}
interface Logger
info: void
warn: void
error: void
end
def initialize_app(config: AppConfig): Boolean
puts "Initializing \#{config[:name]}"
true
end
def run(args: Array<String>): Integer
0
end
TRB
# Model file
File.write(File.join(lib_dir, "user.trb"), <<~TRB)
type UserId = Integer
type Email = String
interface User
id: UserId
email: Email
name: String
active?: Boolean
end
def find_user(id: UserId): User | nil
nil
end
def create_user(email: Email, name: String): User
{ id: 1, email: email, name: name, active?: true }
end
TRB
# Compile all files with custom config (disable type checking for this test)
config = TRuby::Config.new(File.join(tmpdir, "trbconfig.yml"))
allow(config).to receive(:type_check?).and_return(false)
compiler = TRuby::Compiler.new(config)
trb_files = Dir.glob(File.join(lib_dir, "*.trb"))
expect(trb_files.size).to eq(2)
trb_files.each do |file|
expect { compiler.compile(file) }.not_to raise_error
output_file = file.sub(".trb", ".rb")
expect(File.exist?(output_file)).to be true
end
end
it "handles incremental compilation correctly" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
# Create trbconfig.yml config to set output dir
File.write(File.join(tmpdir, "trbconfig.yml"), <<~YAML)
emit:
rb: true
rbs: false
dtrb: false
paths:
src: "#{lib_dir}"
out: "#{lib_dir}"
YAML
# Create initial file
file1 = File.join(lib_dir, "file1.trb")
File.write(file1, "def hello(name: String): String\n \"Hello, \#{name}\"\nend")
config = TRuby::Config.new(File.join(tmpdir, "trbconfig.yml"))
compiler = TRuby::Compiler.new(config)
ic = TRuby::IncrementalCompiler.new(compiler)
# Initial compile
ic.compile_all([file1])
expect(File.exist?(file1.sub(".trb", ".rb"))).to be true
# Modify file
sleep(0.1) # Ensure mtime changes
File.write(file1, "def hello(name: String): String\n \"Hi, \#{name}!\"\nend")
# Incremental compile should detect change
expect(ic.needs_compile?(file1)).to be true
ic.compile_all([file1])
end
it "performs parallel compilation" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
# Create multiple files
10.times do |i|
File.write(File.join(lib_dir, "file_#{i}.trb"), <<~TRB)
def method_#{i}(value: Integer): Integer
value * #{i}
end
TRB
end
config = TRuby::Config.new
compiler = TRuby::Compiler.new(config)
processor = TRuby::ParallelProcessor.new(thread_count: 4)
files = Dir.glob(File.join(lib_dir, "*.trb"))
results = processor.process_files(files) do |file|
compiler.compile(file)
end
expect(results.size).to eq(10)
results.each do |result|
# Each result is the output file path (string)
expect(result).to be_a(String)
expect(result).to end_with(".rb")
end
end
end
describe "Type checking pipeline" do
it "validates type annotations across files" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
# File with type definitions
File.write(File.join(lib_dir, "types.trb"), <<~TRB)
type Status = "pending" | "active" | "completed"
interface Task
id: Integer
title: String
status: Status
end
TRB
# File using the types
File.write(File.join(lib_dir, "tasks.trb"), <<~TRB)
def get_task(id: Integer): Task | nil
nil
end
def update_status(task: Task, status: Status): Task
task
end
TRB
config = TRuby::Config.new
compiler = TRuby::Compiler.new(config)
files = Dir.glob(File.join(lib_dir, "*.trb"))
files.each do |file|
expect { compiler.compile(file) }.not_to raise_error
end
end
it "detects type errors with SMT solver" do
content = <<~TRB
def process(value: String): Integer
value.to_i
end
TRB
parser = TRuby::Parser.new(content, use_combinator: true)
parser.parse
ir_program = parser.ir_program
expect(ir_program).not_to be_nil
type_checker = TRuby::TypeChecker.new
result = type_checker.check_program(ir_program)
expect(result).to be_a(Hash)
end
end
describe "Watch mode" do
it "detects file changes and recompiles" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
file = File.join(lib_dir, "watched.trb")
File.write(file, "def original(x: Integer): Integer\n x\nend")
config = TRuby::Config.new
watcher = TRuby::Watcher.new(paths: [lib_dir], config: config)
# Verify watcher can be created
expect(watcher).to be_a(TRuby::Watcher)
expect(watcher.incremental_compiler).not_to be_nil
end
end
describe "Package management" do
it "initializes a new package" do
pm = TRuby::PackageManager.new(project_dir: tmpdir)
manifest = pm.init(name: "test-package")
expect(manifest.name).to eq("test-package")
expect(manifest.version).to eq("0.1.0")
expect(File.exist?(File.join(tmpdir, ".trb-manifest.json"))).to be true
end
it "manages dependencies" do
pm = TRuby::PackageManager.new(project_dir: tmpdir)
pm.init(name: "test-project")
# Register a mock package in registry
pm.registry.register(TRuby::PackageManifest.new(
name: "test-types",
version: "1.0.0"
))
pm.add("test-types", "^1.0.0")
manifest = TRuby::PackageManifest.load(File.join(tmpdir, ".trb-manifest.json"))
expect(manifest.dependencies).to have_key("test-types")
end
it "resolves version constraints" do
registry = TRuby::PackageRegistry.new
# Register multiple versions
%w[1.0.0 1.1.0 1.2.0 2.0.0].each do |version|
registry.register(TRuby::PackageManifest.new(
name: "lib",
version: version
))
end
resolver = TRuby::DependencyResolver.new(registry)
# Create manifest with constraint
manifest = TRuby::PackageManifest.new(
name: "app",
version: "1.0.0",
dependencies: { "lib" => "^1.0.0" }
)
result = resolver.resolve(manifest)
expect(result[:conflicts]).to be_empty
expect(result[:resolved]["lib"]).to eq("1.2.0") # Latest 1.x
end
end
describe "LSP server" do
it "handles initialization" do
server = TRuby::LSPServer.new
init_result = server.handle_message({
"id" => 1,
"method" => "initialize",
"params" => {
"processId" => Process.pid,
"rootUri" => "file://#{tmpdir}",
"capabilities" => {},
},
})
expect(init_result["result"]["capabilities"]).to be_a(Hash)
expect(init_result["result"]["capabilities"]["textDocumentSync"]).not_to be_nil
expect(init_result["result"]["capabilities"]["completionProvider"]).not_to be_nil
end
it "provides hover information" do
server = TRuby::LSPServer.new
server.handle_message({
"id" => 1,
"method" => "initialize",
"params" => {
"processId" => Process.pid,
"rootUri" => "file://#{tmpdir}",
"capabilities" => {},
},
})
# Open a document (notification - no id)
server.handle_message({
"method" => "textDocument/didOpen",
"params" => {
"textDocument" => {
"uri" => "file://#{tmpdir}/test.trb",
"languageId" => "t-ruby",
"version" => 1,
"text" => "def hello(name: String): String\n \"Hello\"\nend",
},
},
})
hover_result = server.handle_message({
"id" => 2,
"method" => "textDocument/hover",
"params" => {
"textDocument" => { "uri" => "file://#{tmpdir}/test.trb" },
"position" => { "line" => 0, "character" => 4 },
},
})
expect(hover_result).to be_a(Hash)
end
end
describe "Documentation generation" do
it "generates API documentation" do
lib_dir = File.join(tmpdir, "lib")
docs_dir = File.join(tmpdir, "docs")
FileUtils.mkdir_p(lib_dir)
File.write(File.join(lib_dir, "api.trb"), <<~TRB)
# User type representing a system user
type UserId = Integer
# User interface
interface User
id: UserId
name: String
end
# Find a user by ID
def find_user(id: UserId): User | nil
nil
end
TRB
doc_gen = TRuby::DocGenerator.new
doc_gen.generate([lib_dir], output_dir: docs_dir)
expect(File.exist?(File.join(docs_dir, "index.html"))).to be true
expect(File.exist?(File.join(docs_dir, "search-index.json"))).to be true
end
it "generates markdown documentation" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
File.write(File.join(lib_dir, "lib.trb"), <<~TRB)
type Config = Hash<String, String>
def process(config: Config): Boolean
true
end
TRB
doc_gen = TRuby::DocGenerator.new
output_path = File.join(tmpdir, "API.md")
doc_gen.generate_markdown([lib_dir], output_path: output_path)
expect(File.exist?(output_path)).to be true
content = File.read(output_path)
expect(content).to include("Config")
expect(content).to include("process")
end
end
describe "Benchmarking" do
it "runs benchmark suite" do
benchmark = TRuby::BenchmarkSuite.new
# Run just parsing benchmarks with minimal iterations
benchmark.run_category(:parsing, iterations: 2, warmup: 1)
results = benchmark.results[:parsing]
expect(results).to be_a(Hash)
expect(results.keys).to include(:small_file, :medium_file)
results.each_value do |stats|
expect(stats[:avg_time]).to be >= 0
expect(stats[:min_time]).to be >= 0
expect(stats[:max_time]).to be >= 0
end
end
it "exports benchmark results" do
benchmark = TRuby::BenchmarkSuite.new
benchmark.run_category(:parsing, iterations: 2, warmup: 1)
json_path = File.join(tmpdir, "benchmarks.json")
benchmark.export_json(json_path)
expect(File.exist?(json_path)).to be true
data = JSON.parse(File.read(json_path))
expect(data["results"]).to be_a(Hash)
end
end
describe "Bundler integration" do
it "generates gem type stubs" do
gemfile_path = File.join(tmpdir, "Gemfile")
File.write(gemfile_path, <<~GEMFILE)
source 'https://rubygems.org'
gem 'json'
GEMFILE
integration = TRuby::BundlerIntegration.new(project_dir: tmpdir)
# Basic functionality test
expect(integration).to be_a(TRuby::BundlerIntegration)
end
end
describe "CLI interface" do
it "parses command line arguments" do
# Test various CLI commands
expect { TRuby::CLI.new(["--help"]) }.not_to raise_error
end
end
describe "Error handling" do
it "provides helpful error messages for syntax errors" do
# Parser is lenient and doesn't raise errors for incomplete constructs
# Instead, it returns success with empty results for unparseable content
content = "def broken(x: String" # Missing closing paren
parser = TRuby::Parser.new(content)
result = parser.parse
# Parser returns success but with no parsed functions
expect(result[:type]).to eq(:success)
expect(result[:functions]).to be_empty
end
it "handles file not found gracefully" do
config = TRuby::Config.new
compiler = TRuby::Compiler.new(config)
expect do
compiler.compile("/nonexistent/file.trb")
end.to raise_error(ArgumentError, /File not found/)
end
end
describe "Cross-file type checking" do
it "validates types across multiple files" do
lib_dir = File.join(tmpdir, "lib")
FileUtils.mkdir_p(lib_dir)
File.write(File.join(lib_dir, "base.trb"), <<~TRB)
interface Entity
id: Integer
created_at: Time
end
TRB
File.write(File.join(lib_dir, "user.trb"), <<~TRB)
interface User
# extends Entity
id: Integer
created_at: Time
name: String
end
TRB
config = TRuby::Config.new
compiler = TRuby::Compiler.new(config)
checker = TRuby::CrossFileTypeChecker.new
files = Dir.glob(File.join(lib_dir, "*.trb"))
files.each do |file|
ir = compiler.compile_to_ir(file)
checker.register_file(file, ir)
end
result = checker.check_all
expect(result[:errors]).to be_empty
end
end
end