GenVectorTests 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env ruby
  2. # Permission is hereby granted, free of charge, to any person obtaining a
  3. # copy of this software and associated documentation files (the "Software"),
  4. # to deal in the Software without restriction, including without limitation
  5. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. # and/or sell copies of the Software, and to permit persons to whom the
  7. # Software is furnished to do so, subject to the following conditions:
  8. #
  9. # The above copyright notice and this permission notice shall be included in
  10. # all copies or substantial portions of the Software.
  11. #
  12. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  15. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. # DEALINGS IN THE SOFTWARE.
  19. #
  20. require 'optparse'
  21. require File.join(File.dirname(__FILE__), '../Spec/vectors', 'vectorparser')
  22. @test_files = ["v3/kdf", "v3/password", "v3/key",
  23. "v2/kdf", "v2/password",
  24. "v1/kdf", "v1/password",
  25. ]
  26. @options = {}
  27. # Returns the text for an NSDictionary assignment from a hash
  28. def DictionaryForHash(hash)
  29. "[\n" + hash.collect { |key, value| %Q( "#{key}": "#{value}") }.join(",\n") + "]"
  30. end
  31. # Output the file header to output stream
  32. def outputHeader(output)
  33. output << <<-HEADER
  34. // Automatically Generated by GenVectorTests
  35. import XCTest
  36. class GeneratedVectorTests: XCTestCase {
  37. HEADER
  38. end
  39. # Output the tests for a given filename to the output stream
  40. def outputTestsForFile(output, name)
  41. name_identifier = name.gsub('/', '_')
  42. VectorParser.new(@options[:vector_directory] + '/' + name).vectors.each do |vector|
  43. output << <<-TEST_CASE
  44. func test_#{name_identifier}_#{vector["title"].gsub(/[ ()-]+/, '_')}() {
  45. verify_#{name_identifier}(#{DictionaryForHash(vector)})
  46. }
  47. TEST_CASE
  48. end
  49. end
  50. # Output the footer to the output stream
  51. def outputFooter(output)
  52. output<< <<-FOOTER
  53. }
  54. FOOTER
  55. end
  56. ###################
  57. ### MAIN
  58. ###################
  59. opt_parser = OptionParser.new do |opt|
  60. opt.banner = "Usage: GenVectorTest -o VectorTests.m <vector_directory>"
  61. opt.separator ""
  62. opt.on("-o","--output PATH","path to output code") do |output_path|
  63. @options[:output_path] = output_path
  64. end
  65. end
  66. opt_parser.parse!
  67. raise OptionParser::MissingArgument if @options[:output_path].nil?
  68. raise OptionParser::MissingArgument if ARGV.length != 1
  69. @options[:vector_directory] = ARGV[0]
  70. File.open(@options[:output_path], "w") do |output|
  71. outputHeader(output)
  72. @test_files.each do |file|
  73. outputTestsForFile(output, file)
  74. end
  75. outputFooter(output)
  76. end