1#!/usr/bin/env ruby 2 3TEST_REGEX = /TEST_F\([a-zA-Z0-9_]+,\s+([a-zA-Z0-9_]+)\)/ 4 5DISABLED_TESTS = %w( 6 test_ex7_10_plain_characters 7 test_ex7_17_flow_mapping_separate_values 8 test_ex7_21_single_pair_implicit_entries 9 test_ex7_2_empty_nodes 10 test_ex8_2_block_indentation_header 11) 12 13class Context 14 attr_accessor :name, :ev, :src 15 def initialize 16 @name = "" 17 @src = "" 18 @ev = [] 19 end 20end 21 22class String 23 def snakecase 24 self 25 .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') 26 .gsub(/([a-z\d])([A-Z])/, '\1_\2') 27 .tr('-', '_') 28 .gsub(/\s/, '_') 29 .gsub(/__+/, '_') 30 .downcase 31 end 32end 33 34ctx = nil 35 36tests = [] 37IO.foreach(ARGV[0]) do |line| 38 line.strip! 39 if ctx 40 fail "unexpected TEST_F" if line =~ TEST_REGEX 41 if line =~ /^}/ 42 tests << ctx 43 ctx = nil 44 end 45 if line =~ /^EXPECT_CALL/ 46 fail 'not end with ;' unless line[-1] == ';' 47 v = line.gsub('(', ' ').gsub(')', ' ').split 48 ctx.ev << v[2] 49 end 50 else 51 next unless line =~ TEST_REGEX 52 name = $1 53 next unless name =~ /^(Ex\d+_\d+)/ 54 str = $1.upcase 55 $stderr.puts "found #{name}" 56 ctx = Context.new 57 ctx.name = "test_#{name.snakecase}" 58 ctx.src = str 59 end 60end 61 62# code gen 63tests.each do |t| 64 next if t.ev.size == 0 65 if DISABLED_TESTS.include? t.name 66 puts "#[allow(dead_code)]" 67 else 68 puts "#[test]" 69 end 70 puts "fn #{t.name}() {" 71 puts " let mut v = str_to_test_events(#{t.src}).into_iter();" 72 t.ev.each do |e| 73 puts " assert_next!(v, TestEvent::#{e});" 74 end 75 puts "}" 76 puts 77end 78 79