您的位置:首页 > 编程语言 > Ruby

ruby功能模块测试方法集,仅供参考

2014-12-24 21:12 239 查看
#coding: utf-8
require 'open-uri'
require 'net/http'
require 'nokogiri'
#require 'iconv'
require 'uri'
require 'pathname'

class A
  def initialize
    @way = ""
  end

  def a_methods
    puts '-----a methods----'
  end
end

module TestModule
  def test_one
    
  end

  def test_two
  end
end

class TestFunction
  def initialize
    out('alert',"欢迎使用本脚本!!")
    @messages = ""
    @methods = {}
    TestFunction.instance_methods(false).each_with_index do |m, i|
      @methods[i+1] = m.to_s
    end
  end

  def test_single_class
    a = A.new
    c = "zhouzhen"
    puts a.a_methods
    class << a
      def test_a
        puts "---<#{c}>--test a----"
      end
    end
    puts a.test_a
  end

  def test_block 

    out('input', "请输入你的姓名:")
    out('start')
    params = $stdin.gets.chomp()
    puts main_block(params){|name|
      name+" 一名工程师!!"
    }
    out('end')
  end

  def test_excel

    out('start')
    sheet1 = open_excel(@url)
    sheets = open_excel(@url) {|book| book.worksheets }
    puts "1================="
    sheet1.each do |row|
      puts "----#{row}"
    end
    puts "2================="
    sheets.each_with_index do |sheet, index|
      puts "#{2+index+1}================="
      puts sheet.name
      sheet.each do |row|
        puts "----#{row}"
      end
    end

    out('end')
  end

  def main
    out('input',"*. 回车直接查看所有测试模块:")
    out('input',"*. 退出:(exit/quit)")
    function = $stdin.gets.chomp()

    if function.upcase == "EXIT" || function.upcase == "QUIT"
      out('alert',"欢迎下次使用本脚本!!")
      puts "goodbye!"
    else
      function = "get_all_function" if function.empty?
      self.send(function)
      main
    end
  end

  private

  def method_missing(name, *arg)
    if @methods.has_key?(name.to_s.to_i)
      self.send(@methods[name.to_s.to_i])
    else
      puts "#{name} 这个方法不存在!" 
      super
    end
  end

  def get_all_function(id=nil) 

    @methods.collect do |key, value|
      if id.nil?
        puts "#{key}: #{value}"
      else
        if id == key 
          return value 
        end
      end
    end

    out('input',"*. 请选择指定的方法进行测试:")
  end

  def open_excel(url,&block)
    Spreadsheet.client_encoding = "UTF-8"
    begin
      book = Spreadsheet.open(url)
      if block.nil?
        sheets = book.worksheet 0
      else
        sheets = yield book
      end
      return sheets
    rescue
      return nil
    end
  end

  def main_block(name, &block)
    new_name = yield name
  end

  def get_url(request_url)
    result = []
    request_url.collect do |k, v|
      result << "#{k}=#{v}"
    end
    return result.join("&")
  end

  def http_request(method, uri, query_hash={})
    query = query_hash.map{|k, v| "#{k}=#{v}"}.join('&')
    query_escaped = URI.escape(query)

    uri_parsed = URI.parse(uri)
    http = Net::HTTP.new(uri_parsed.host)

    case method.downcase!
      when 'get'
        return http.get(uri_parsed.path + '?' + query_escaped).body
      when 'post'
        return http.post(uri_parsed.path, query_escaped).body
    end
  end

  def out(type,message=nil)
    if type == 'start' 
      puts "开始测试...."
    elsif type == 'end' 
      puts "测试结束!!!!"
    elsif type == 'input' 
    elsif type == 'alert' 
      puts "提示:"
    else  
      puts "==========="
    end
    puts message unless message.nil?
  end
end
#TestFunction.new.send(ARGV[0])

TestFunction.new.main
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ruby 脚本 测试 uri methods