コンテンツを比較する
Google App Engine で開発。第十弾。
GAE python27 + nose です。
テスト用のデータをつくる方法を用意したので、次はテスト結果を検証する方法を用意します。
eq_content(exp_content, act_content) を作ってみます。
from difflib import SequenceMatcher from nose.tools import ok_ EQ_CONTENT_WILDCARD = '***' EQ_CONTENT_AROUND = 10 def eq_content(exp_content, act_content): matcher = SequenceMatcher(None, exp_content, act_content) def msg(tag, exp, act): def insert_mark(mark, content): return '\n'.join(mark + l for l in content.splitlines()) mark = { 'insert': (' ', '+ '), 'delete': ('- ', ' '), 'replace': ('- ', '+ '), 'equal': (' ', ' '), } em, am = mark[tag] return '\nexpect:\n%s\nactual:\n%s\n' % (insert_mark(em, exp), insert_mark(am, act)) for tag, i1, i2, j1, j2 in matcher.get_opcodes(): exp = exp_content[i1:i2] act = act_content[j1:j2] d = min(EQ_CONTENT_AROUND, i1, j1) i = min(EQ_CONTENT_AROUND, len(exp_content) - i2, len(act_content) - j2) around_exp = exp_content[(i1-d):(i2+i)] around_act = act_content[(j1-d):(j2+i)] is_only_newline = (exp == '\n' or act == '\n') msg_ = msg(tag, around_exp, around_act) ok_(tag == 'equal' or exp == EQ_CONTENT_WILDCARD or is_only_newline, msg_)
exp_content に下記のように書いておくと、任意の文字列を許容するようにしています。
<input type='hidden' name='csrfmiddlewaretoken' value='***' />
差分表示においては、差分のある箇所だけではどこで差分が発生したのかが分かりづらいため、差分のある箇所の前後を表示するようにしています。
参考: