socialforge/spec/utils/git_diff_spec.rb

58 lines
1.9 KiB
Ruby

require 'spec_helper'
require 'rails_helper'
require 'ostruct'
describe "Git diff" do
before(:each) do
# 参照此diff的输出 http://ucloudgit.trustie.net/Hjqreturn/pullrequesttest111/commit/f36f15bc1d3e256a4fa5115d82201af72301884e
@content = '''
[{"diff":"--- a/readme\n+++ b/readme\n@@ -1,7 +1,8 @@\n 邀请界面\n 1.1 界面设计 (已处理)\n- 1.2 改写成html功能 (已处理)\n+sdkfsjkdf\n 1.3 集成到游戏邀请功能 (已处理)\n+adfjsdajlfk\n \n 微信登录功能\n 2.1 集成sdk (已处理)\n","new_path":"readme","old_path":"readme","a_mode":"100644","b_mode":"100644","new_file":false,"renamed_file":false,"deleted_file":false}]
'''
@output = %Q{
| 1 | 1 | 邀请界面
| 2 | 2 | 1.1 界面设计 (已处理)
| 3 | |-<span class='idiff'> 1.2 改写成html功能 (已处理)</span>
| | 3 |+<span class='idiff'>sdkfsjkdf</span>
| 4 | 4 | 1.3 集成到游戏邀请功能 (已处理)
| | 5 |+adfjsdajlfk
| 5 | 6 | } +
%Q{
| 6 | 7 | 微信登录功能
| 7 | 8 | 2.1 集成sdk (已处理)
}
end
it "正确解析diff文件" do
diff = ActiveSupport::JSON.decode(@content).first
diff = OpenStruct.new(diff)
diff_file = Trustie::Gitlab::Diff::File.new(diff)
output = ''
diff_file.diff_lines.each_with_index do |line, index|
type = line.type
last_line = line.new_pos
line_old = line.old_pos.to_s
if type == 'match'
## 表示没有修改,两个都要显示行号
output += "|#{line_old.center(4)}|#{last_line.to_s.center(4)}|#{line.text}\n"
else
old_line = type == 'new' ? ' '*4: line_old
new_line = type == 'old' ? ' '*4: last_line
output += "|#{old_line.to_s.center(4)}|#{new_line.to_s.center(4)}|#{line.text}\n"
end
end
puts output
expect(output.strip).to eq(@output.strip)
end
end