Class: MarkdownExec::TestHashDelegator

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

Defined Under Namespace

Classes: TestHashDelegatorAppendDivider, TestHashDelegatorBlockFind, TestHashDelegatorBlocksFromNestedFiles, TestHashDelegatorCollectRequiredCodeLines, TestHashDelegatorCommandOrUserSelectedBlock, TestHashDelegatorCountBlockInFilename, TestHashDelegatorCreateAndWriteFile, TestHashDelegatorDetermineBlockState, TestHashDelegatorDisplayRequiredCode, TestHashDelegatorFetchColor, TestHashDelegatorFormatExecutionStreams, TestHashDelegatorFormatReferencesSendColor, TestHashDelegatorHandleBackLink, TestHashDelegatorHandleBlockState, TestHashDelegatorHandleGenericBlock, TestHashDelegatorHandleStream, TestHashDelegatorIterBlocksFromNestedFiles, TestHashDelegatorMenuChromeColoredOption, TestHashDelegatorMenuChromeFormattedOptionWithoutFormat, TestHashDelegatorStartFencedBlock, TestHashDelegatorStringSendColor

Instance Method Summary collapse

Instance Method Details

#setupObject



2570
2571
2572
2573
# File 'lib/hash_delegator.rb', line 2570

def setup
  @hd = HashDelegator.new
  @mdoc = mock('MarkdownDocument')
end

#test_calling_execute_required_lines_calls_command_execute_with_argument_args_valueObject



2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
# File 'lib/hash_delegator.rb', line 2575

def test_calling_execute_required_lines_calls_command_execute_with_argument_args_value
  pigeon = 'E'
  obj = {
    output_execution_label_format: '',
    output_execution_label_name_color: 'plain',
    output_execution_label_value_color: 'plain'
  }

  c = MarkdownExec::HashDelegator.new(obj)
  c.pass_args = pigeon

  # Expect that method opts_command_execute is called with argument args having value pigeon
  c.expects(:command_execute).with(
    '',
    args: pigeon
  )

  # Call method opts_execute_required_lines
  c.execute_required_lines
end

#test_indent_all_lines_with_empty_indentObject



2638
2639
2640
2641
2642
2643
# File 'lib/hash_delegator.rb', line 2638

def test_indent_all_lines_with_empty_indent
  body = "Line 1\nLine 2"
  indent = ''

  assert_equal body, HashDelegator.indent_all_lines(body, indent)
end

#test_indent_all_lines_with_indentObject



2624
2625
2626
2627
2628
2629
# File 'lib/hash_delegator.rb', line 2624

def test_indent_all_lines_with_indent
  body = "Line 1\nLine 2"
  indent = '  ' # Two spaces
  expected_result = "  Line 1\n  Line 2"
  assert_equal expected_result, HashDelegator.indent_all_lines(body, indent)
end

#test_indent_all_lines_without_indentObject



2631
2632
2633
2634
2635
2636
# File 'lib/hash_delegator.rb', line 2631

def test_indent_all_lines_without_indent
  body = "Line 1\nLine 2"
  indent = nil

  assert_equal body, HashDelegator.indent_all_lines(body, indent)
end

Test case for empty body



2597
2598
2599
2600
# File 'lib/hash_delegator.rb', line 2597

def test_push_link_history_and_trigger_load_with_empty_body
  assert_equal LoadFile::Reuse,
               @hd.push_link_history_and_trigger_load.load_file
end

Test case for non-empty body with ‘file’ key



2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
# File 'lib/hash_delegator.rb', line 2610

def test_push_link_history_and_trigger_load_with_file_key
  body = ["file: sample_file\nblock: sample_block\nvars:\n  KEY: VALUE"]
  expected_result = LoadFileLinkState.new(LoadFile::Load,
                                          LinkState.new(block_name: 'sample_block',
                                                        document_filename: 'sample_file',
                                                        inherited_dependencies: {},
                                                        inherited_lines: ['# ', 'KEY="VALUE"']))
  assert_equal expected_result,
               @hd.push_link_history_and_trigger_load(
                 link_block_body: body,
                 selected: FCB.new(block_name: 'sample_block', filename: 'sample_file')
               )
end

Test case for non-empty body without ‘file’ key



2603
2604
2605
2606
2607
# File 'lib/hash_delegator.rb', line 2603

def test_push_link_history_and_trigger_load_without_file_key
  body = ["vars:\n  KEY: VALUE"]
  assert_equal LoadFile::Reuse,
               @hd.push_link_history_and_trigger_load(link_block_body: body).load_file
end

#test_safeval_rescue_from_errorObject



2649
2650
2651
2652
# File 'lib/hash_delegator.rb', line 2649

def test_safeval_rescue_from_error
  HashDelegator.stubs(:error_handler).with('safeval')
  assert_nil HashDelegator.safeval('invalid code')
end

#test_safeval_successful_evaluationObject



2645
2646
2647
# File 'lib/hash_delegator.rb', line 2645

def test_safeval_successful_evaluation
  assert_equal 4, HashDelegator.safeval('2 + 2')
end

#test_set_fcb_titleObject



2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
# File 'lib/hash_delegator.rb', line 2654

def test_set_fcb_title
  # sample input and output data for testing default_block_title_from_body method
  input_output_data = [
    {
      input: MarkdownExec::FCB.new(title: nil,
                                   body: ["puts 'Hello, world!'"]),
      output: "puts 'Hello, world!'"
    },
    {
      input: MarkdownExec::FCB.new(title: '',
                                   body: ['def add(x, y)',
                                          '  x + y', 'end']),
      output: "def add(x, y)\n    x + y\n  end\n"
    },
    {
      input: MarkdownExec::FCB.new(title: 'foo', body: %w[bar baz]),
      output: 'foo' # expect the title to remain unchanged
    }
  ]

  # iterate over the input and output data and
  # assert that the method sets the title as expected
  input_output_data.each do |data|
    input = data[:input]
    output = data[:output]
    HashDelegator.default_block_title_from_body(input)
    assert_equal output, input.title
  end
end

#test_yield_line_if_selected_with_lineObject



3292
3293
3294
3295
3296
3297
3298
3299
3300
# File 'lib/hash_delegator.rb', line 3292

def test_yield_line_if_selected_with_line
  block_called = false
  HashDelegator.yield_line_if_selected('Test line', [:line]) do |type, content|
    block_called = true
    assert_equal :line, type
    assert_equal 'Test line', content.body[0]
  end
  assert block_called
end

#test_yield_line_if_selected_without_blockObject



3310
3311
3312
3313
# File 'lib/hash_delegator.rb', line 3310

def test_yield_line_if_selected_without_block
  result = HashDelegator.yield_line_if_selected('Test line', [:line])
  assert_nil result
end

#test_yield_line_if_selected_without_lineObject



3302
3303
3304
3305
3306
3307
3308
# File 'lib/hash_delegator.rb', line 3302

def test_yield_line_if_selected_without_line
  block_called = false
  HashDelegator.yield_line_if_selected('Test line', [:other]) do |_|
    block_called = true
  end
  refute block_called
end