Class: CachedNestedFileReaderTest
- Defined in:
- lib/cached_nested_file_reader.rb
Instance Method Summary collapse
- #setup ⇒ Object
- #teardown ⇒ Object
- #test_caching_functionality ⇒ Object
- #test_import_caching_with_different_parameters ⇒ Object
- #test_import_caching_with_same_parameters ⇒ Object
- #test_readlines_with_imports ⇒ Object
- #test_readlines_with_imports_and_substitutions ⇒ Object
- #test_readlines_with_template_delimiters ⇒ Object
- #test_readlines_without_imports ⇒ Object
Instance Method Details
#setup ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/cached_nested_file_reader.rb', line 345 def setup @file2 = Tempfile.new('test2.txt') @file2.write("ImportedLine1\nImportedLine2") @file2.rewind @file1 = Tempfile.new('test1.txt') @file1.write("Line1\nLine2\n @import #{@file2.path}\nLine3") @file1.rewind @reader = CachedNestedFileReader.new( import_directive_line_pattern: /^(?<indention> *)@import +(?<name>\S+)(?<params>(?: +[A-Za-z_]\w*=(?:"[^"]*"|'[^']*'|\S+))*) *$/, import_directive_parameter_scan: /([A-Za-z_]\w*)(:=|\?=|!=|=)(?:"([^"]*)"|'([^']*)'|(\S+))/, import_parameter_variable_assignment: '%{key}=%{value}', shell: 'bash', shell_block_name: '(document_shell)', symbol_command_substitution: ':c=', symbol_evaluated_expression: ':e=', symbol_raw_literal: '=', symbol_force_quoted_literal: ':q=', symbol_variable_reference: ':v=' ) end |
#teardown ⇒ Object
369 370 371 372 373 374 375 |
# File 'lib/cached_nested_file_reader.rb', line 369 def teardown @file1.close @file1.unlink @file2.close @file2.unlink end |
#test_caching_functionality ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/cached_nested_file_reader.rb', line 427 def test_caching_functionality # First read result1 = @reader.readlines(@file2.path).map(&:to_s) # Simulate file content change @file2.reopen(@file2.path, 'w') { |f| f.write('ChangedLine') } # Second read (should read from cache, not the changed file) result2 = @reader.readlines(@file2.path, clear_cache: false, read_cache: true).map(&:to_s) assert_equal result1, result2 assert_equal %w[ImportedLine1 ImportedLine2], result2 end |
#test_import_caching_with_different_parameters ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/cached_nested_file_reader.rb', line 484 def test_import_caching_with_different_parameters # Create a file that will be imported with different parameters template_file = Tempfile.new('template.txt') template_file.write('Hello NAME, your ID is ID') template_file.rewind # Create a file that imports the same file with different parameters importing_file = Tempfile.new('importing_different.txt') importing_file.write("Users:\n @import #{template_file.path} NAME=Alice ID=123\n @import #{template_file.path} NAME=Bob ID=456\nEnd") importing_file.rewind # Track how many times the template file is actually read read_count = 0 original_readlines = File.method(:readlines) File.define_singleton_method(:readlines) do |filename, **opts| if filename == template_file.path read_count += 1 end original_readlines.call(filename, **opts) end result = @reader.readlines(importing_file.path).map(&:to_s) # The template file should be read twice since parameters are different assert_equal 2, read_count, 'Template file should be read twice when imported with different parameters' # Verify the content is correct expected = ['Users:', ' Hello Alice, your 123 is 123', ' Hello Bob, your 456 is 456', 'End'] assert_equal expected, result # Restore original method File.define_singleton_method(:readlines, original_readlines) template_file.close template_file.unlink importing_file.close importing_file.unlink end |
#test_import_caching_with_same_parameters ⇒ Object
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 |
# File 'lib/cached_nested_file_reader.rb', line 443 def test_import_caching_with_same_parameters # Create a file that will be imported multiple times shared_file = Tempfile.new('shared.txt') shared_file.write("Shared content line 1\nShared content line 2") shared_file.rewind # Create a file that imports the same file multiple times with same parameters importing_file = Tempfile.new('importing_multiple.txt') importing_file.write("Start\n @import #{shared_file.path} PARAM=value\nMiddle\n @import #{shared_file.path} PARAM=value\nEnd") importing_file.rewind # Track how many times the shared file is actually read read_count = 0 original_readlines = File.method(:readlines) File.define_singleton_method(:readlines) do |filename, **opts| if filename == shared_file.path read_count += 1 end original_readlines.call(filename, **opts) end result = @reader.readlines(importing_file.path).map(&:to_s) # The shared file should only be read once, not twice assert_equal 1, read_count, 'Shared file should only be read once when imported with same parameters' # Verify the content is correct expected = ['Start', ' Shared content line 1', ' Shared content line 2', 'Middle', ' Shared content line 1', ' Shared content line 2', 'End'] assert_equal expected, result # Restore original method File.define_singleton_method(:readlines, original_readlines) shared_file.close shared_file.unlink importing_file.close importing_file.unlink end |
#test_readlines_with_imports ⇒ Object
382 383 384 385 386 |
# File 'lib/cached_nested_file_reader.rb', line 382 def test_readlines_with_imports result = @reader.readlines(@file1.path).map(&:to_s) assert_equal ['Line1', 'Line2', ' ImportedLine1', ' ImportedLine2', 'Line3'], result end |
#test_readlines_with_imports_and_substitutions ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/cached_nested_file_reader.rb', line 388 def test_readlines_with_imports_and_substitutions file_with_substitution = Tempfile.new('test_substitution.txt') file_with_substitution.write("Server: HOST\nPort: PORT") file_with_substitution.rewind file_importing = Tempfile.new('test_importing.txt') file_importing.write("Config:\n @import #{file_with_substitution.path} HOST=localhost PORT=8080\nEnd") file_importing.rewind result = @reader.readlines(file_importing.path).map(&:to_s) assert_equal ['Config:', ' Server: localhost', ' Port: 8080', 'End'], result file_with_substitution.close file_with_substitution.unlink file_importing.close file_importing.unlink end |
#test_readlines_with_template_delimiters ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/cached_nested_file_reader.rb', line 407 def test_readlines_with_template_delimiters file_with_template = Tempfile.new('test_template.txt') file_with_template.write("API_URL=${API_URL}\nVERSION={{VERSION}}") file_with_template.rewind file_importing = Tempfile.new('test_importing.txt') file_importing.write("Config:\n @import #{file_with_template.path} API_URL=https://api.example.com VERSION=1.2.3\nEnd") file_importing.rewind result = @reader.readlines(file_importing.path, use_template_delimiters: true).map(&:to_s) assert_equal ['Config:', ' API_URL=https://api.example.com', ' VERSION=1.2.3', 'End'], result file_with_template.close file_with_template.unlink file_importing.close file_importing.unlink end |
#test_readlines_without_imports ⇒ Object
377 378 379 380 |
# File 'lib/cached_nested_file_reader.rb', line 377 def test_readlines_without_imports result = @reader.readlines(@file2.path).map(&:to_s) assert_equal %w[ImportedLine1 ImportedLine2], result end |