Module: Parse::Assertions

Includes:
Test::Unit::Assertions
Included in:
Parse
Defined in:
app/models/parse.rb

Overview

rows_and_columns

Instance Method Summary collapse

Instance Method Details

#add_parse_message(string, pattern, message = '') ⇒ Object

newline_if_not_empty


111
112
113
# File 'app/models/parse.rb', line 111

def add_parse_message(string, pattern, message='')
	newline_if_not_empty(message)+"\n#{string.inspect}.match(#{pattern.inspect})=#{string.match(pattern).inspect}"
end

#assert_parse(answer, string, pattern, message = '') ⇒ Object

add_parse_message


114
115
116
117
# File 'app/models/parse.rb', line 114

def assert_parse(answer, string, pattern, message='')
	message=add_parse_message(string, pattern, message)
	assert_equal(answer, parse_string(string, pattern), add_parse_message(string, pattern, message))
end

#assert_parse_repetition(answer, string, pattern, repetition_range, message = '') ⇒ Object

parse_sequence


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/parse.rb', line 134

def assert_parse_repetition(answer, string, pattern, repetition_range, message='')
	match1=parse_string(string, pattern)
	assert_equal(match1, answer[0, match1.size], add_parse_message(string, pattern, message))
	match_any=parse_string(string, pattern*Regexp::Any)
	assert_equal(answer, match_any[-answer.size..-1], add_parse_message(string, pattern*Regexp::Any, message))
	match=parse_string(string, pattern*repetition_range)
	if match==[] || match=={} then
		message+="match1=#{match1.inspect}\n"
		message+="match2=#{match2.inspect}\n"
		message+="match12=#{match12.inspect}\n"
		message+="string.match(#{pattern*repetition_range})=#{string.match(pattern*repetition_range).inspect}"
		assert_equal(answer, parse_string(string, pattern*repetition_range), message)
	end #if
end

#assert_parse_sequence(answer, string, pattern1, pattern2, message = '') ⇒ Object

parse


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/parse.rb', line 118

def assert_parse_sequence(answer, string, pattern1, pattern2, message='')
	match1=parse_string(string, pattern1)
	assert_equal(answer[0, match1.size], match1, add_parse_message(string, pattern1, message))
	match2=parse_string(string, pattern2)
	assert_empty(match2-answer, add_parse_message(string, pattern2, message))
	match12=parse_string(pattern1.match(string).post_match, pattern2)
	assert_equal(match12, answer[-match12.size..-1], add_parse_message(pattern1.match(string).post_match, pattern2, message))
	match=parse_string(string, pattern1*pattern2)
	if match==[] || match=={} then
		message+="match1=#{match1.inspect}\n"
		message+="match2=#{match2.inspect}\n"
		message+="match12=#{match12.inspect}\n"
		message+="string.match(#{pattern1*pattern2})=#{string.match(pattern1*pattern2).inspect}"
		assert_equal(answer, parse_string(string, pattern1*pattern2), message)
	end #if
end

#newline_if_not_empty(message) ⇒ Object


104
105
106
107
108
109
110
# File 'app/models/parse.rb', line 104

def newline_if_not_empty(message)
	if message.empty? then
		message
	else
		message+"\n"
	end #if
end