Module: Yamatanooroti::WindowsTestCaseModule

Included in:
WindowsTestCase
Defined in:
lib/yamatanooroti/windows.rb

Constant Summary collapse

DL =
Yamatanooroti::WindowsDefinition

Instance Method Summary collapse

Instance Method Details

#assert_screen(expected_lines) ⇒ Object



487
488
489
490
491
492
493
494
# File 'lib/yamatanooroti/windows.rb', line 487

def assert_screen(expected_lines)
  case expected_lines
  when Array
    assert_equal(expected_lines, @result)
  when String
    assert_equal(expected_lines, @result.join("\n").sub(/\n*\z/, "\n"))
  end
end

#closeObject



446
447
448
449
450
451
452
# File 'lib/yamatanooroti/windows.rb', line 446

def close
  sleep @wait
  # read first before kill the console process including output
  @result = retrieve_screen

  free_resources
end

#resultObject



483
484
485
# File 'lib/yamatanooroti/windows.rb', line 483

def result
  @result
end

#start_terminal(height, width, command, wait: 1, startup_message: nil) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/yamatanooroti/windows.rb', line 496

def start_terminal(height, width, command, wait: 1, startup_message: nil)
  @height = height
  @width = width
  @wait = wait
  @result = nil
  setup_console(height, width)
  launch(command.map{ |c| quote_command_arg(c) }.join(' '))
  case startup_message
  when String
    check_startup_message = ->(message) { message.start_with?(startup_message) }
  when Regexp
    check_startup_message = ->(message) { startup_message.match?(message) }
  end
  if check_startup_message
    loop do
      screen = retrieve_screen.join("\n").sub(/\n*\z/, "\n")
      break if check_startup_message.(screen)
      sleep @wait
    end
  end
end

#write(str) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/yamatanooroti/windows.rb', line 368

def write(str)
  sleep @wait
  str.force_encoding(Encoding::ASCII_8BIT).tr!("\n", "\r")
  records = Fiddle::Pointer.malloc(DL::INPUT_RECORD_WITH_KEY_EVENT.size * str.size * 2, DL::FREE)
  str.chars.each_with_index do |c, i|
    byte = c.ord
    if c.bytesize == 1 and byte.allbits?(0x80) # with Meta key
      c = (byte ^ 0x80).chr
      control_key_state = DL::LEFT_ALT_PRESSED
    else
      control_key_state = 0
    end
    record_index = i * 2
    r = DL::INPUT_RECORD_WITH_KEY_EVENT.new(records + DL::INPUT_RECORD_WITH_KEY_EVENT.size * record_index)
    set_input_record(r, c, true, control_key_state)
    record_index = i * 2 + 1
    r = DL::INPUT_RECORD_WITH_KEY_EVENT.new(records + DL::INPUT_RECORD_WITH_KEY_EVENT.size * record_index)
    set_input_record(r, c, false, control_key_state)
  end
  written_size = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, DL::FREE)
  r = DL.WriteConsoleInputW(DL.GetStdHandle(DL::STD_INPUT_HANDLE), records, str.size * 2, written_size)
  error_message(r, 'WriteConsoleInput')
end