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

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值