<commit_before>require 'pp'
require 'set'

$test_count = 0
$failure_count = 0
$success_count = 0

DEBUG_ENABLED = ENV['VERBOSE'] ? ENV['VERBOSE'] == 'true' : false
START_TIME = Time.now()

def print_debug(message)
    if DEBUG_ENABLED
        puts("DEBUG %.2f:\t %s" % [Time.now() - START_TIME, message])
    end
end

DRIVER_PORT = (ARGV[0] || ENV['RDB_DRIVER_PORT'] || raise('driver port not supplied')).to_i
print_debug("Using driver port #{DRIVER_PORT}")

$required_external_tables = []
if ARGV[1] || ENV['TEST_DB_AND_TABLE_NAME']
  (ARGV[1] || ENV['TEST_DB_AND_TABLE_NAME']).split(',').each { |table_raw|
    table_raw = table_raw.strip()
    if table_raw == ''
      next
    end
    split_value = table_raw.split('.')
    case split_value.count
    when 1
      $required_external_tables.push(['test', split_value[0]])
    when 2
      $required_external_tables.push([split_value[0], split_value[1]])
    else
      raise('Unusable value for external tables: ' + table_raw)
    end
  }
end
$required_external_tables = $required_external_tables.reverse
$stdout.flush

# -- import the rethinkdb driver

require_relative '../importRethinkDB.rb'

# --

$reql_conn = RethinkDB::Connection.new(:host => 'localhost', :port => DRIVER_PORT)
begin
  r.db_create('test').run($reql_conn)
rescue
end

$defines = binding

# --

def show(x)
  if x.is_a?(Err)
    name = x.type.name.sub(/^RethinkDB::/, "")
    return "<#{name} #{'~ ' if x.message.is_a? Regexp}#{show x.message}>"
  end
  return (PP.pp x, "").chomp
end

NoError = "<no error>"
AnyUUID = "<any uuid>"
Err = Struct.new(:type, :message, :backtrace)
Bag = Struct.new(:items, :partial)
PartialHash = Struct.new(:hash)

def bag(list, partial=false)
  Bag.new(list, partial)
end

def partial(expected)
  if expected.kind_of?(Array)
    bag(expected, true)
  elsif expected.kind_of?(Bag)
    bag(expected.items, true)
  elsif expected.kind_of?(Hash)
    PartialHash.new(expected)
  else
    raise("partial can only handle Hashs, Arrays, or Bags. Got: #{expected.class}")
  end
end

def fetch(cursor, limit=nil)
  raise "The limit value of fetch must be nil or > 0, got: #{limit}" unless limit.nil? or true
  if limit.nil?
    return cursor.to_a
  else
    result = []
    limit.times do
      result.push(cursor.next)
    end
    return result
  end
end

def wait(seconds)
  sleep(seconds)
end

def arrlen(len, x)
  Array.new(len, x)
end

def uuid
  AnyUUID
end

def shard
  # do nothing in Ruby tests
end

def err(type, message, backtrace=[])
  Err.new(RethinkDB.const_get(type), message, backtrace)
end

def err_regex(type, message, backtrace=[])
  Err.new(RethinkDB.const_get(type), Regexp.new(message), backtrace)
end

class Number
  def initialize(value)
    @value = value
    @requiredType = value.class
  end

  def method_missing(name, *args)
    return @number.send(name, *args)
  end

  def to_s
    return @value.to_s + "(explicit" + @requiredType.name + ")"
  end

  def inspect
    return @value.to_s + " (explicit " + @requiredType.name + ")"
  end
  attr_reader :value
  attr_reader :requiredType
end

def int_cmp(value)
  raise "int_cmp got a non Fixnum input: " + value.to_s unless value.kind_of?(Fixnum)
  return Number.new(value)
end

def float_cmp(value)
  raise "int_cmp got a non Float input: " + value.to_s unless value.kind_of?(Float)
  return Number.new(value)
end

def cmp_test(expected, result, testopts={}, partial=false)
  if expected.object_id == NoError.object_id
    if result.is_a?(Err)
      puts result
      puts result.backtrace
      return -1
    end
    return 0
  end

  if expected.nil? and result.nil?
    return 0
  elsif result.nil?
    return 1
  elsif expected.nil?
    return -1
  end

  if expected.object_id == AnyUUID.object_id
    return -1 if not result.kind_of? String
    return 0 if result.match /[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/
    return 1
  end

  if result.is_a?(String) then
    result = result.sub(/\nFailed assertion:(.|\n)*/, "")
  end

  case "#{expected.class}"
  when "Err"
    # Don't try to be clever and rearrange this.  `<=` is used to
    # check for subclasses, so swapping the arguments and using `>`
    # will break this.  Likewise, `===` isn't symmetric.
    return result.class <= expected.type && expected.message === result.message ? 0 : -1

  when "Array"
    if result.respond_to? :to_a
      result = result.to_a<commit_msg>fix ruby test driver handling of error comparisons<commit_after>require 'pp'
require 'set'

$test_count = 0
$failure_count = 0
$success_count = 0

DEBUG_ENABLED = ENV['VERBOSE'] ? ENV['VERBOSE'] == 'true' : false
START_TIME = Time.now()

def print_debug(message)
    if DEBUG_ENABLED
        puts("DEBUG %.2f:\t %s" % [Time.now() - START_TIME, message])
    end
end

DRIVER_PORT = (ARGV[0] || ENV['RDB_DRIVER_PORT'] || raise('driver port not supplied')).to_i
print_debug("Using driver port #{DRIVER_PORT}")

$required_external_tables = []
if ARGV[1] || ENV['TEST_DB_AND_TABLE_NAME']
  (ARGV[1] || ENV['TEST_DB_AND_TABLE_NAME']).split(',').each { |table_raw|
    table_raw = table_raw.strip()
    if table_raw == ''
      next
    end
    split_value = table_raw.split('.')
    case split_value.count
    when 1
      $required_external_tables.push(['test', split_value[0]])
    when 2
      $required_external_tables.push([split_value[0], split_value[1]])
    else
      raise('Unusable value for external tables: ' + table_raw)
    end
  }
end
$required_external_tables = $required_external_tables.reverse
$stdout.flush

# -- import the rethinkdb driver

require_relative '../importRethinkDB.rb'

# --

$reql_conn = RethinkDB::Connection.new(:host => 'localhost', :port => DRIVER_PORT)
begin
  r.db_create('test').run($reql_conn)
rescue
end

$defines = binding

# --

def show(x)
  if x.is_a?(Err)
    name = x.type.name.sub(/^RethinkDB::/, "")
    return "<#{name} #{'~ ' if x.message.is_a? Regexp}#{show x.message}>"
  end
  return (PP.pp x, "").chomp
end

NoError = "<no error>"
AnyUUID = "<any uuid>"
Err = Struct.new(:type, :message, :backtrace)
Bag = Struct.new(:items, :partial)
PartialHash = Struct.new(:hash)

def bag(list, partial=false)
  Bag.new(list, partial)
end

def partial(expected)
  if expected.kind_of?(Array)
    bag(expected, true)
  elsif expected.kind_of?(Bag)
    bag(expected.items, true)
  elsif expected.kind_of?(Hash)
    PartialHash.new(expected)
  else
    raise("partial can only handle Hashs, Arrays, or Bags. Got: #{expected.class}")
  end
end

def fetch(cursor, limit=nil)
  raise "The limit value of fetch must be nil or > 0, got: #{limit}" unless limit.nil? or true
  if limit.nil?
    return cursor.to_a
  else
    result = []
    limit.times do
      result.push(cursor.next)
    end
    return result
  end
end

def wait(seconds)
  sleep(seconds)
end

def arrlen(len, x)
  Array.new(len, x)
end

def uuid
  AnyUUID
end

def shard
  # do nothing in Ruby tests
end

def err(type, message, backtrace=[])
  Err.new(RethinkDB.const_get(type), message, backtrace)
end

def err_regex(type, message, backtrace=[])
  Err.new(RethinkDB.const_get(type), Regexp.new(message), backtrace)
end

class Number
  def initialize(value)
    @value = value
    @requiredType = value.class
  end

  def method_missing(name, *args)
    return @number.send(name, *args)
  end

  def to_s
    return @value.to_s + "(explicit" + @requiredType.name + ")"
  end

  def inspect
    return @value.to_s + " (explicit " + @requiredType.name + ")"
  end
  attr_reader :value
  attr_reader :requiredType
end

def int_cmp(value)
  raise "int_cmp got a non Fixnum input: " + value.to_s unless value.kind_of?(Fixnum)
  return Number.new(value)
end

def float_cmp(value)
  raise "int_cmp got a non Float input: " + value.to_s unless value.kind_of?(Float)
  return Number.new(value)
end

def cmp_test(expected, result, testopts={}, partial=false)
  if expected.object_id == NoError.object_id
    if result.is_a?(Err)
      puts result
      puts result.backtrace
      return -1
    end
    return 0
  end

  if expected.nil? and result.nil?
    return 0
  elsif result.nil?
    return 1
  elsif expected.nil?
    return -1
  end

  if expected.object_id == AnyUUID.object_id
    return -1 if not result.kind_of? String
    return 0 if result.match /[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/
    return 1
  end

  if result.is_a?(String) then
    result = result.sub(/\nFailed assertion:(.|\n)*/, "")
  end

  case "#{expected.class}"
  when "Err"
    if expected.is_a?(result.class)
      return result.message <=> result.message
    else
      return result.class.name <=> result.class.name
    end

  when "Array"
    if result.respond_to? :to_a
      result = result.to_a
{'fraction_non_alphanumeric': 0.13830032243205895, 'fraction_numerical': 0.008751727314601566, 'mean_word_length': 3.5328810020876826, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904917', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3190, 'n_words': 781}