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



2456
2457
2458
2459
# File 'lib/hash_delegator.rb', line 2456

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

#test_calling_execute_required_lines_calls_command_execute_with_argument_args_valueObject



2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
# File 'lib/hash_delegator.rb', line 2461

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



2524
2525
2526
2527
2528
2529
# File 'lib/hash_delegator.rb', line 2524

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



2510
2511
2512
2513
2514
2515
# File 'lib/hash_delegator.rb', line 2510

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



2517
2518
2519
2520
2521
2522
# File 'lib/hash_delegator.rb', line 2517

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



2483
2484
2485
2486
# File 'lib/hash_delegator.rb', line 2483

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



2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
# File 'lib/hash_delegator.rb', line 2496

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



2489
2490
2491
2492
2493
# File 'lib/hash_delegator.rb', line 2489

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



2535
2536
2537
2538
# File 'lib/hash_delegator.rb', line 2535

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

#test_safeval_successful_evaluationObject



2531
2532
2533
# File 'lib/hash_delegator.rb', line 2531

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

#test_set_fcb_titleObject



2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
# File 'lib/hash_delegator.rb', line 2540

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



3178
3179
3180
3181
3182
3183
3184
3185
3186
# File 'lib/hash_delegator.rb', line 3178

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



3196
3197
3198
3199
# File 'lib/hash_delegator.rb', line 3196

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



3188
3189
3190
3191
3192
3193
3194
# File 'lib/hash_delegator.rb', line 3188

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