PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_examples.rb

http://github.com/coatl/rubymacros
Ruby | 91 lines | 59 code | 10 blank | 22 comment | 1 complexity | e4c4685f642d656c940d35a1315a5013 MD5 | raw file
Possible License(s): LGPL-3.0
  1. =begin
  2. rubymacros - a macro preprocessor for ruby
  3. Copyright (C) 2008, 2016 Caleb Clausen
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. =end
  15. require 'test/unit'
  16. require "macro"
  17. def def_example_test(name,expect)
  18. define_method "test_example_#{(name).gsub('/','Y')}" do
  19. out,err=capture_std_out_err{
  20. load name
  21. }
  22. # p [expect,out]
  23. assert_equal expect,out
  24. assert empty_but_for_warns?(err), "expected no warnings, but saw these:\n"+err.gsub!(/^/," ")
  25. end
  26. end
  27. def capture_std_out_err #dangerous... hard to debug
  28. old={:O=>STDOUT.dup,:o=>$stdout,:E=>STDERR.dup,:e=>$stderr}
  29. o1,o2=IO::pipe
  30. e1,e2=IO::pipe
  31. STDOUT.reopen(o2)
  32. STDERR.reopen(e2)
  33. $stdout=STDOUT
  34. $stderr=STDERR
  35. begin
  36. yield
  37. ensure
  38. STDOUT.reopen old[:O]
  39. STDERR.reopen old[:E]
  40. $stdout,$stderr=old[:o],old[:e]
  41. end
  42. o2.close; e2.close
  43. out=o1.read
  44. err=e1.read
  45. o1.close; e1.close
  46. return out,err
  47. end
  48. def empty_but_for_warns? str
  49. str=str.dup
  50. str.gsub!(/^([^:]+:\d+: warning: .*)$/){STDERR.puts $1;''}
  51. str.gsub!(/\n{2,}/,"\n")
  52. /\A\Z/===str
  53. end
  54. class ExamplesTest<Test::Unit::TestCase
  55. def setup
  56. Macro.delete_all!
  57. end
  58. def self.example_dir
  59. macropath=$LOADED_FEATURES.grep(/macro\.rb$/)[0]
  60. unless %r{^[/\\]}===macropath
  61. macropath=$LOAD_PATH.find{|dir| File.exist? dir+"/macro.rb"}
  62. else
  63. macropath=File.dirname(macropath)
  64. end
  65. File.dirname(macropath)+"/example/"
  66. end
  67. StringNode=RedParse::StringNode
  68. can=File.read(example_dir+"/expected_output.txt").gsub(/^#.*$/,'').split(/^(.*) :\n/)
  69. can.shift
  70. #code=:()
  71. # warn "unroll example disabled for now"
  72. while name=can.shift
  73. expect=can.shift
  74. #code+=:(
  75. def_example_test(name,expect) #unless /unroll/===name
  76. end
  77. #Macro.eval code
  78. end