Class: CachedNestedFileReaderTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/cached_nested_file_reader.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/cached_nested_file_reader.rb', line 85

def setup
  @file2 = Tempfile.new('test2.txt')
  @file2.write("ImportedLine1\nImportedLine2")
  @file2.rewind

  @file1 = Tempfile.new('test1.txt')
  @file1.write("Line1\nLine2\n #insert #{@file2.path}\nLine3")
  @file1.rewind
  @reader = CachedNestedFileReader.new(import_pattern: /^ *#insert (?'name'.+)$/)
end

#teardownObject



96
97
98
99
100
101
102
# File 'lib/cached_nested_file_reader.rb', line 96

def teardown
  @file1.close
  @file1.unlink

  @file2.close
  @file2.unlink
end

#test_caching_functionalityObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cached_nested_file_reader.rb', line 115

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).map(&:to_s)

  assert_equal result1, result2
  assert_equal %w[ImportedLine1 ImportedLine2], result2
end

#test_readlines_with_importsObject



109
110
111
112
113
# File 'lib/cached_nested_file_reader.rb', line 109

def test_readlines_with_imports
  result = @reader.readlines(@file1.path).map(&:to_s)
  assert_equal %w[Line1 Line2 ImportedLine1 ImportedLine2 Line3],
               result
end

#test_readlines_without_importsObject



104
105
106
107
# File 'lib/cached_nested_file_reader.rb', line 104

def test_readlines_without_imports
  result = @reader.readlines(@file2.path).map(&:to_s)
  assert_equal %w[ImportedLine1 ImportedLine2], result
end