Class: MarkdownExec::TestHashDelegator

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

Instance Method Summary collapse

Instance Method Details

#setupObject



4253
4254
4255
4256
# File 'lib/hash_delegator.rb', line 4253

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

Test case for empty body



4284
4285
4286
4287
# File 'lib/hash_delegator.rb', line 4284

def test_execute_block_type_link_with_state_with_empty_body
  assert_equal LoadFile::REUSE,
               @hd.execute_block_type_link_with_state.load_file
end

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



4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
# File 'lib/hash_delegator.rb', line 4299

def test_execute_block_type_link_with_state_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.execute_block_type_link_with_state(
                 link_block_body: body,
                 selected: FCB.new(block_name: 'sample_block',
                                   filename: 'sample_file')
               )
end

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



4290
4291
4292
4293
4294
4295
4296
# File 'lib/hash_delegator.rb', line 4290

def test_execute_block_type_link_with_state_without_file_key
  body = ["vars:\n  KEY: VALUE"]
  assert_equal LoadFile::REUSE,
               @hd.execute_block_type_link_with_state(
                 link_block_body: body
               ).load_file
end

#test_execute_required_lines_with_argument_args_valueObject



4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
# File 'lib/hash_delegator.rb', line 4258

def test_execute_required_lines_with_argument_args_value
  # 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,
    shell: ShellType::BASH
  )

  # Call method opts_execute_required_lines
  c.execute_required_lines(shell: ShellType::BASH)
end

#test_indent_all_lines_with_empty_indentObject



4331
4332
4333
4334
4335
4336
# File 'lib/hash_delegator.rb', line 4331

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



4316
4317
4318
4319
4320
4321
4322
# File 'lib/hash_delegator.rb', line 4316

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



4324
4325
4326
4327
4328
4329
# File 'lib/hash_delegator.rb', line 4324

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_safeval_rescue_from_errorObject



4342
4343
4344
4345
4346
# File 'lib/hash_delegator.rb', line 4342

def test_safeval_rescue_from_error
  assert_raises(SystemExit) do
    HashDelegator.safeval('invalid_code_raises_exception')
  end
end

#test_safeval_successful_evaluationObject



4338
4339
4340
# File 'lib/hash_delegator.rb', line 4338

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

#test_set_fcb_titleObject



4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
# File 'lib/hash_delegator.rb', line 4348

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]
    title = HashDelegator.default_block_title_from_body(input)
    assert_equal output, title
  end
end