Class: LoggedStructTest
- Defined in:
- lib/logged_struct.rb
Instance Method Summary collapse
- #setup ⇒ Object
- #test_array_values ⇒ Object
- #test_boolean_values ⇒ Object
- #test_custom_log_level ⇒ Object
- #test_debug_logging ⇒ Object
- #test_hash_style_getter ⇒ Object
- #test_hash_style_setter ⇒ Object
- #test_initialization_with_hash ⇒ Object
- #test_initialization_without_hash ⇒ Object
- #test_method_missing_for_undefined_method ⇒ Object
- #test_method_style_getter ⇒ Object
- #test_method_style_setter ⇒ Object
- #test_nested_values ⇒ Object
- #test_nil_values ⇒ Object
- #test_numeric_values ⇒ Object
- #test_to_h_method ⇒ Object
Instance Method Details
#setup ⇒ Object
148 149 150 151 152 153 |
# File 'lib/logged_struct.rb', line 148 def setup @log_output = StringIO.new @logger = Logger.new(@log_output) @logger.level = Logger::DEBUG @logger.formatter = proc { |_, _, _, msg| "#{msg}\n" } end |
#test_array_values ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/logged_struct.rb', line 261 def test_array_values struct = LoggedStruct.new({ tags: ['ruby', 'testing'] }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct. assert_equal(['ruby', 'testing'], result) assert_includes struct.log_content, 'GET: tags => ["ruby", "testing"]' end |
#test_boolean_values ⇒ Object
272 273 274 275 276 277 278 279 280 281 |
# File 'lib/logged_struct.rb', line 272 def test_boolean_values struct = LoggedStruct.new({ active: true }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct.active assert_equal true, result assert_includes struct.log_content, 'GET: active => true' end |
#test_custom_log_level ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/logged_struct.rb', line 229 def test_custom_log_level logger = Logger.new(@log_output) logger.formatter = proc { |severity, _, _, msg| "#{severity}: #{msg}\n" } struct = LoggedStruct.new({ name: 'John' }, logger: logger, log_level: :info) @log_output.truncate(0) @log_output.rewind struct.name assert_includes struct.log_content, 'INFO: GET: name => "John"' end |
#test_debug_logging ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/logged_struct.rb', line 308 def test_debug_logging # Create a struct with direct logging to STDOUT for visibility stdout_logger = Logger.new($stdout) stdout_logger.level = Logger::DEBUG stdout_logger.formatter = proc { |_, _, _, msg| "STDOUT_LOG: #{msg}\n" } struct = LoggedStruct.new({ value: 'test' }, logger: stdout_logger) # Check the initial state of the log buffer puts "Initial log buffer content: #{struct.log_buffer.string.inspect}" # Get a value to trigger logging value = struct.value puts "After get - log buffer: #{struct.log_buffer.string.inspect}" # Set a value to trigger logging struct.new_value = 'hello world' puts "After set - log buffer: #{struct.log_buffer.string.inspect}" # Make a direct assertion we can verify assert_includes struct.log_buffer.string, "GET: value => \"test\"" assert_includes struct.log_buffer.string, "SET: new_value = \"hello world\"" end |
#test_hash_style_getter ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/logged_struct.rb', line 194 def test_hash_style_getter struct = LoggedStruct.new({ name: 'John' }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct[:name] assert_equal 'John', result assert_includes struct.log_content, 'GET: name => "John"' end |
#test_hash_style_setter ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/logged_struct.rb', line 205 def test_hash_style_setter struct = LoggedStruct.new(logger: @logger) @log_output.truncate(0) @log_output.rewind struct[:name] = 'John' assert_equal 'John', struct[:name] assert_includes struct.log_content, 'SET: name = "John"' # The second line should be from the getter assertion assert_includes struct.log_content, 'GET: name => "John"' end |
#test_initialization_with_hash ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/logged_struct.rb', line 155 def test_initialization_with_hash struct = LoggedStruct.new({ name: 'John', age: 30 }, logger: @logger) name = struct.name assert_equal 'John', name assert_includes struct.log_content, 'GET: name => "John"' age = struct.age assert_equal 30, age assert_includes struct.log_content, 'GET: age => 30' end |
#test_initialization_without_hash ⇒ Object
167 168 169 170 171 172 |
# File 'lib/logged_struct.rb', line 167 def test_initialization_without_hash struct = LoggedStruct.new(logger: @logger) assert_nil struct.name assert_includes struct.log_content, 'GET: name => nil' end |
#test_method_missing_for_undefined_method ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/logged_struct.rb', line 242 def test_method_missing_for_undefined_method struct = LoggedStruct.new(logger: @logger) assert_raises(NoMethodError) do struct.undefined_method('argument') end end |
#test_method_style_getter ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/logged_struct.rb', line 174 def test_method_style_getter struct = LoggedStruct.new({ name: 'John' }, logger: @logger) result = struct.name assert_equal 'John', result assert_includes struct.log_content, 'GET: name => "John"' end |
#test_method_style_setter ⇒ Object
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/logged_struct.rb', line 183 def test_method_style_setter struct = LoggedStruct.new(logger: @logger) struct.name = 'John' assert_includes struct.log_content, 'SET: name = "John"' result = struct.name assert_equal 'John', result assert_includes struct.log_content, 'GET: name => "John"' end |
#test_nested_values ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/logged_struct.rb', line 250 def test_nested_values struct = LoggedStruct.new({ user: { name: 'John', age: 30 } }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct.user assert_equal({ name: 'John', age: 30 }, result) assert_includes struct.log_content, 'GET: user => {:name=>"John", :age=>30}' end |
#test_nil_values ⇒ Object
283 284 285 286 287 288 289 290 291 292 |
# File 'lib/logged_struct.rb', line 283 def test_nil_values struct = LoggedStruct.new({ data: nil }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct.data assert_nil result assert_includes struct.log_content, 'GET: data => nil' end |
#test_numeric_values ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/logged_struct.rb', line 294 def test_numeric_values struct = LoggedStruct.new({ count: 42, price: 99.99 }, logger: @logger) @log_output.truncate(0) @log_output.rewind count = struct.count price = struct.price assert_equal 42, count assert_equal 99.99, price assert_includes struct.log_content, 'GET: count => 42' assert_includes struct.log_content, 'GET: price => 99.99' end |
#test_to_h_method ⇒ Object
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/logged_struct.rb', line 218 def test_to_h_method struct = LoggedStruct.new({ name: 'John', age: 30 }, logger: @logger) @log_output.truncate(0) @log_output.rewind result = struct.to_h assert_equal({ name: 'John', age: 30 }, result) assert_includes struct.log_content, 'Converting LoggedStruct to Hash: {:name=>"John", :age=>30}' end |