Class: Spectre::SimpleFormatter
Instance Method Summary collapse
- #collections(engine) ⇒ Object
-
#describe(contexts, level = 0, parent: nil) ⇒ Object
Outputs all the specs for all contexts.
-
#details(specs) ⇒ Object
Formats the details of given specs.
- #environment(env) ⇒ Object
- #envs(envs) ⇒ Object
-
#initialize(config) ⇒ SimpleFormatter
constructor
A new instance of SimpleFormatter.
-
#list(specs) ⇒ Object
Formats a list of specs in short form.
- #log(level, message, status = nil, desc = nil) ⇒ Object
-
#mixins(mixins) ⇒ Object
Formats a list of mixins.
- #scope(desc, type) ⇒ Object
Constructor Details
#initialize(config) ⇒ SimpleFormatter
Returns a new instance of SimpleFormatter.
245 246 247 248 249 250 251 252 |
# File 'lib/spectre.rb', line 245 def initialize config @out = config['stdout'] || $stdout @level = 0 @width = 80 @indent = 2 @colors = [:blue, :magenta, :yellow, :green] @debug = config['debug'] end |
Instance Method Details
#collections(engine) ⇒ Object
331 332 333 |
# File 'lib/spectre.rb', line 331 def collections engine @out.puts engine.collections.pretty end |
#describe(contexts, level = 0, parent: nil) ⇒ Object
Outputs all the specs for all contexts
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/spectre.rb', line 277 def describe contexts, level = 0, parent: nil contexts.select { |x| x.parent == parent }.each do |context| @out.puts("#{' ' * level}#{context.desc.send(level.positive? ? :magenta : :blue)}") context.specs.each do |spec| @out.puts("#{' ' * (level + 1)}#{spec.desc}") end describe(contexts, level + 1, parent: context) end end |
#details(specs) ⇒ Object
Formats the details of given specs
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/spectre.rb', line 292 def details specs counter = 0 specs .group_by { |x| x.parent.root } .each_value do |spec_group| spec_group.each do |spec| spec_id = "[#{spec.name}]".send(@colors[counter % @colors.length]) spec_detail = "#{spec_id}\n" spec_detail += " subject..: #{spec.root.desc}\n" spec_detail += " context..: #{spec.parent.desc}\n" unless spec.root == spec.parent spec_detail += " desc.....: #{spec.desc}\n" spec_detail += " tags.....: #{spec..join(', ')}\n" if spec..any? spec_detail += " data.....: #{spec.data.to_json}\n" if spec.data spec_detail += " file.....: #{spec.file}\n" @out.puts("#{spec_detail}\n") end counter += 1 end end |
#environment(env) ⇒ Object
335 336 337 |
# File 'lib/spectre.rb', line 335 def environment env @out.puts env.to_h.pretty end |
#envs(envs) ⇒ Object
339 340 341 342 343 |
# File 'lib/spectre.rb', line 339 def envs envs @out.puts envs .map { |env_name, _| env_name } .join("\n") end |
#list(specs) ⇒ Object
Formats a list of specs in short form
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/spectre.rb', line 257 def list specs counter = 0 specs .group_by { |x| x.parent.root.name } .each_value do |spec_group| spec_group .sort .each do |spec| spec_id = "[#{spec.name}]".send(@colors[counter % @colors.length]) @out.puts "#{spec_id} #{spec.full_desc} #{spec..map { |x| "##{x}" }.join(' ').cyan}" end counter += 1 end end |
#log(level, message, status = nil, desc = nil) ⇒ Object
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 |
# File 'lib/spectre.rb', line 378 def log level, , status = nil, desc = nil return if @locked color = [:fatal, :debug].include?(level) ? level : nil write(, fill: true, color:) if block_given? or @debug or level != :debug error = nil if block_given? @locked = true begin level, status, desc = yield rescue StandardError => e level = :fatal status = :error desc = e.class error = e ensure @locked = false end end label = status || level return unless block_given? or @debug or level != :debug status_text = "[#{label}]" if desc.nil? @out.puts status_text.send(label) else @out.puts "#{status_text} - #{desc}".send(label) end raise error if error end |
#mixins(mixins) ⇒ Object
Formats a list of mixins
318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/spectre.rb', line 318 def mixins mixins paragraphs = [] mixins.each_value do |mixin| output = "#{mixin.desc.yellow}\n" output += " params.....: #{mixin.params.join ', '}\n" if mixin.params.any? output += " location...: #{mixin.file.sub(Dir.pwd, '.')}:#{mixin.line}" paragraphs << output end @out.puts paragraphs.join("\n\n") end |
#scope(desc, type) ⇒ Object
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 |
# File 'lib/spectre.rb', line 345 def scope desc, type if desc colored_desc = case type when :before, :after, :setup, :teardown desc.magenta when :group desc.grey when Specification desc.cyan when :mixin desc.yellow when DefinitionContext desc.blue else desc end write(colored_desc) @out.puts "\n" end return unless block_given? @level += 1 begin yield ensure @level -= 1 end end |