#!/usr/bin/ruby
#
# Check that "require 'readline'" loads the GNU Readline based extension
# shipped in this package instead of falling back to Reline, and that it
# can read a line and record it in the history.

require "readline"

def fail!(message)
  abort "FAIL: #{message}"
end

spec = Gem.loaded_specs["readline-ext"]
fail! "readline-ext gem not activated, Readline is #{Readline}" unless spec
fail! "gemspec version #{spec.version} does not match extension version " \
      "#{Readline::GEM_VERSION}" unless spec.version.to_s == Readline::GEM_VERSION
fail! "not built against GNU Readline: #{Readline::VERSION}" if Readline::VERSION.include?("EditLine")
fail! "Readline.quoting_detection_proc is missing" unless Readline.respond_to?(:quoting_detection_proc)

line = File.open(IO::NULL, "w") do |null|
  IO.pipe do |r, w|
    w.write("hello world\n")
    w.close
    Readline.input = r
    Readline.output = null
    Readline.readline("> ", true)
  end
end
fail! "unexpected line read: #{line.inspect}" unless line == "hello world"
fail! "unexpected history: #{Readline::HISTORY.to_a.inspect}" unless Readline::HISTORY.to_a == ["hello world"]

puts "OK: readline-ext #{Readline::GEM_VERSION} using GNU Readline #{Readline::VERSION}"
