summaryrefslogtreecommitdiffstats
path: root/third_party/rust/yaml-rust/tests/specs/cpp2rust.rb
blob: 25813c87a6cf8c7f5cdb19ca60e9fe76078e8655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby

TEST_REGEX = /TEST_F\([a-zA-Z0-9_]+,\s+([a-zA-Z0-9_]+)\)/

DISABLED_TESTS = %w(
	test_ex7_10_plain_characters
	test_ex7_17_flow_mapping_separate_values
	test_ex7_21_single_pair_implicit_entries
	test_ex7_2_empty_nodes
	test_ex8_2_block_indentation_header
)

class Context
	attr_accessor :name, :ev, :src
	def initialize
		@name = ""
		@src = ""
		@ev = []
	end
end

class String
	def snakecase
		self
		.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
		.gsub(/([a-z\d])([A-Z])/, '\1_\2')
		.tr('-', '_')
		.gsub(/\s/, '_')
		.gsub(/__+/, '_')
		.downcase
	end
end

ctx = nil

tests = []
IO.foreach(ARGV[0]) do |line|
	line.strip!
	if ctx
		fail "unexpected TEST_F" if line =~ TEST_REGEX
		if line =~ /^}/
			tests << ctx
			ctx = nil
		end
		if line =~ /^EXPECT_CALL/
			fail 'not end with ;' unless line[-1] == ';'
			v = line.gsub('(', ' ').gsub(')', ' ').split
			ctx.ev << v[2]
		end
	else
		next unless line =~ TEST_REGEX
		name = $1
		next unless name =~ /^(Ex\d+_\d+)/
		str = $1.upcase
		$stderr.puts "found #{name}"
		ctx = Context.new
		ctx.name = "test_#{name.snakecase}"
		ctx.src = str
	end
end

# code gen
tests.each do |t|
	next if t.ev.size == 0
	if DISABLED_TESTS.include? t.name
		puts "#[allow(dead_code)]"
	else
		puts "#[test]"
	end
	puts "fn #{t.name}() {"
	puts "    let mut v = str_to_test_events(#{t.src}).into_iter();"
	t.ev.each do |e|
		puts "    assert_next!(v, TestEvent::#{e});"
	end
	puts "}"
	puts
end