102 lines
3.6 KiB
Ruby
102 lines
3.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Api::CompileController, type: :request do
|
|
|
|
let(:out) {JSON.parse(response.body)}
|
|
|
|
it "不支持的语言,报错" do
|
|
post "/api/realtime_test.json", {src: 'void main(){}', language: 'unknow'}
|
|
expect(out["status"].to_i).not_to be(0)
|
|
end
|
|
it "没填写测试数据,报错" do
|
|
post "/api/realtime_test.json", {src: 'void main(){}', language: '1'}
|
|
expect(out["status"].to_i).to_not be(0)
|
|
end
|
|
context 'c语言测试' do
|
|
it "编译错误" do
|
|
src = 'sfsdjf'
|
|
post "/api/realtime_test.json", {src: src, language: '1',
|
|
factor: [{input: '1 2', output: '3'}, {input: '3 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(-2)
|
|
expect(out["error_msg"]).to match(/mycode.c/)
|
|
end
|
|
|
|
it "答题错误" do
|
|
cFile = File.expand_path('../../../cfiles/add.c', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '1',
|
|
factor: [{input: '1 2', output: '6'}, {input: '3 4', output: '9'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(-1)
|
|
expect(out["results"]).to_not be_empty
|
|
end
|
|
|
|
it "答题正确" do
|
|
cFile = File.expand_path('../../../cfiles/add.c', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '1',
|
|
factor: [{input: '1 2', output: '3'}, {input: '3 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(0)
|
|
end
|
|
end
|
|
|
|
context 'python 语言测试' do
|
|
it "编译错误" do
|
|
cFile = File.expand_path('../../../pyfiles/error.py', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '3',
|
|
factor: [{input: '1 2', output: '3'}, {input: '3 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(-2)
|
|
expect(out["error_msg"]).to match(/mycode.py/)
|
|
end
|
|
|
|
it "答题正确" do
|
|
cFile = File.expand_path('../../../pyfiles/add.py', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '3',
|
|
factor: [{input: '1 2', output: '3'}, {input: '3 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(0)
|
|
end
|
|
|
|
it "答题不正确" do
|
|
cFile = File.expand_path('../../../pyfiles/add.py', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '3',
|
|
factor: [{input: '1 4', output: '3'}, {input: '5 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(-1)
|
|
expect(out["results"]).to_not be_empty
|
|
puts out
|
|
end
|
|
end
|
|
|
|
context "java语言测试" do
|
|
|
|
it "答题正确" do
|
|
cFile = File.expand_path('../../../javafiles/Main.java', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '4',
|
|
factor: [{input: '1 2', output: '3'}, {input: '3 4', output: '7'}]
|
|
}
|
|
expect(out["status"].to_i).to eq(0)
|
|
end
|
|
|
|
end
|
|
|
|
context "答案匹配测试" do
|
|
it "有\r\n也能答题正确" do
|
|
cFile = File.expand_path('../../../cfiles/r.c', __FILE__)
|
|
src = File.open(cFile).read
|
|
post "/api/realtime_test.json", {src: src, language: '1',
|
|
factor: [{input: '1 2', output: "2\r\n4"}, {input: '3 4', output: "6\n8"}]
|
|
}
|
|
expect(out["status"].to_i).to eq(0)
|
|
end
|
|
end
|
|
|
|
end
|