{ Josh Rendek }

<3 Go & Kubernetes

Using RSpec to test C code

Feb 27, 2012 - 2 minutes

I was working on a C assignment for school and wanted an easy way to test the output of a program running against multiple test cases.

Using RSpec I came up with the following spec:

 1describe "Calculator "do
 2    before(:all) do
 3        `make clean; make;`
 4    end
 5    it "should accept p1" do
 6        `./calc < testing/p1.cal`.should include "accept"
 7    end
 8
 9    it "should reject p2" do
10        `./calc < testing/p2_err.cal`.should include "reject"
11    end
12
13    it "should reject p3" do
14        `./calc < testing/p3_err.cal`.should include "Variable a duplicate declaration"
15    end
16
17    it "should reject p4" do
18        `./calc < testing/p4_err.cal`.should include "Variable b uninitiated at line 5"
19    end
20
21    it "should accept p5" do
22        `./calc < testing/p5.cal`.should include "accept"
23    end
24
25    it "should accept p6" do
26        `./calc < testing/p6.cal`.should include "accept"
27    end
28
29    it "should reject p7" do
30        `./calc < testing/p7_err.cal`.should include "syntax error at line 9"
31    end
32
33    it "should reject p8" do
34        `./calc < testing/p8_err.cal`.should include "Variable d undeclared"
35    end
36
37    it "should reject p9" do
38        `./calc < testing/p9_err.cal`.should include "divide by zero at line 7"
39    end
40
41end

I was then able to run all my tests with a single command and get informative output.

1.........
2
3Finished in 0.49705 seconds
49 examples, 0 failures

comments powered by Disqus