Class: Spectre::JsonFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ JsonFormatter

Returns a new instance of JsonFormatter.



529
530
531
532
533
534
# File 'lib/spectre.rb', line 529

def initialize config
  @config = config
  @out = config['stdout'] || $stdout
  @out.sync = true
  @curr_scope = nil
end

Instance Method Details

#collections(engine) ⇒ Object



584
585
586
587
588
589
590
591
592
# File 'lib/spectre.rb', line 584

def collections engine
  @out.puts(engine.collections.map do |name, config|
    {
      name:,
      config:,
      specs: engine.list(config).map(&:name),
    }
  end.to_json)
end

#de(contexts) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/spectre.rb', line 540

def de contexts
  contexts.map do |context|
    {
      name: context.name,
      desc: context.desc,
      specs: context.specs.map do |spec|
        {
          name: spec.name,
          desc: spec.desc,
          tags: spec.tags,
          file: spec.file,
          data: spec.data,
        }
      end,
      children: de(context.children),
    }
  end
end

#describe(contexts) ⇒ Object



536
537
538
# File 'lib/spectre.rb', line 536

def describe contexts
  @out.puts JSON.dump de(contexts)
end

#environment(env) ⇒ Object



594
595
596
# File 'lib/spectre.rb', line 594

def environment env
  @out.puts env.to_json
end

#envs(envs) ⇒ Object



598
599
600
601
602
# File 'lib/spectre.rb', line 598

def envs envs
  @out.puts envs
    .map { |env_name, _| env_name }
    .to_json
end

#list(specs) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/spectre.rb', line 559

def list specs
  @out.puts JSON.pretty_generate(specs.map do |spec|
    {
      name: spec.name,
      desc: spec.desc,
      subj: spec.root.desc,
      ctxt: spec.parent.desc,
      tags: spec.tags,
      file: spec.file,
      data: spec.data,
    }
  end)
end

#log(level, message, status = nil, desc = nil) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/spectre.rb', line 635

def log level, message, status = nil, desc = nil
  id = SecureRandom.hex(8)

  @out.puts JSON.dump({
    id: id,
    parent: @curr_scope,
    type: 'log',
    level: level,
    message: message,
    status: status,
    desc: desc,
  })

  return unless block_given?

  begin
    level, status, desc = yield
  rescue StandardError => e
    level = :fatal
    status = :error
    desc = e.class
    error = e
  end

  @out.puts JSON.dump({
    id: id,
    type: 'status',
    level: level,
    status: status,
    desc: desc,
    error: error,
  })
end

#mixins(mixins) ⇒ Object



573
574
575
576
577
578
579
580
581
582
# File 'lib/spectre.rb', line 573

def mixins mixins
  @out.puts JSON.pretty_generate(mixins.each_value.map do |mixin|
    {
      desc: mixin.desc,
      params: mixin.params,
      file: mixin.file,
      line: mixin.line,
    }
  end)
end

#scope(desc, type) ⇒ Object



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/spectre.rb', line 604

def scope desc, type
  id = SecureRandom.hex(8)

  prev_scope = @curr_scope

  if type.is_a?(Specification)
    spec = type.name
    type = :spec
  end

  if type.is_a?(DefinitionContext)
    context = type.name
    type = :context
  end

  @out.puts JSON.dump({
    id: id,
    type: 'scope',
    desc: desc,
    parent: @curr_scope,
    scope: type,
    spec: spec,
    context: context,
  })

  @curr_scope = id
  yield
ensure
  @curr_scope = prev_scope
end