Class: MarkdownExec::TestHashDelegator

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

Instance Method Summary collapse

Instance Method Details

#setupObject



6201
6202
6203
6204
# File 'lib/hash_delegator.rb', line 6201

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

Test case for empty body



6233
6234
6235
6236
# File 'lib/hash_delegator.rb', line 6233

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



6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
# File 'lib/hash_delegator.rb', line 6248

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



6239
6240
6241
6242
6243
6244
6245
# File 'lib/hash_delegator.rb', line 6239

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



6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
# File 'lib/hash_delegator.rb', line 6206

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,
    erls: {},
    shell: ShellType::BASH
  )

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

#test_indent_all_lines_with_empty_indentObject



6280
6281
6282
6283
6284
6285
# File 'lib/hash_delegator.rb', line 6280

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



6265
6266
6267
6268
6269
6270
6271
# File 'lib/hash_delegator.rb', line 6265

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



6273
6274
6275
6276
6277
6278
# File 'lib/hash_delegator.rb', line 6273

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



6291
6292
6293
6294
6295
# File 'lib/hash_delegator.rb', line 6291

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

#test_safeval_successful_evaluationObject



6287
6288
6289
# File 'lib/hash_delegator.rb', line 6287

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

#test_set_fcb_titleObject



6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
# File 'lib/hash_delegator.rb', line 6297

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"
    },
    {
      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