Skip to content
Newer
Older
100644 102 lines (85 sloc) 2.34 KB
89fc461 Initial commit to soroban.
Jason Hutchens authored Apr 21, 2012
1 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3 describe "Soroban" do
add89fc move a few things around to get rewriting a bit neater
Jason Hutchens authored Apr 22, 2012
4
5 let(:sheet) { Soroban::Sheet.new }
6
47d898c everything working; basic tests pass
Jason Hutchens authored Apr 22, 2012
7 it "can add two numbers" do
46ec3eb wrote more tests to define expectations for first version
Jason Hutchens authored Apr 22, 2012
8 sheet.x = 2
9 sheet.y = 3
10 sheet.f = "=x+y"
11 sheet.f.should eq(5)
6b2a557 work with strings at a low level to make serialisation easier
Jason Hutchens authored Apr 22, 2012
12 sheet.x -= 1
13 sheet.f.should eq(4)
47d898c everything working; basic tests pass
Jason Hutchens authored Apr 21, 2012
14 end
add89fc move a few things around to get rewriting a bit neater
Jason Hutchens authored Apr 21, 2012
15
16 it "can rewrite Excel to Ruby" do
17 sheet.A1 = "=foo(A1^2<>3)"
6b2a557 work with strings at a low level to make serialisation easier
Jason Hutchens authored Apr 22, 2012
18 sheet.A1?.should eq("func_foo(@A1.get**2!=3)")
add89fc move a few things around to get rewriting a bit neater
Jason Hutchens authored Apr 21, 2012
19 end
20
46ec3eb wrote more tests to define expectations for first version
Jason Hutchens authored Apr 21, 2012
21 it "can iterate over a collection of cells" do
22 sheet.A1 = 'a'
23 sheet.B1 = 'b'
24 sheet.A2 = 'c'
25 sheet.B2 = 'd'
26 data = sheet.walk('A1:B2').to_a
27 data.sort.join.should eq('abcd')
28 end
29
30 it "can set a value" do
31 sheet.set(:foo, 'hello')
32 sheet.foo.should eq('hello')
33 end
34
35 it "can get a value" do
36 sheet.foo = 'hello'
37 sheet.get(:foo).should eq('hello')
38 end
39
40 it "can set an array" do
41 sheet.set("A1:A5", [ 1, 2, 3, 4, 5 ])
42 sheet.B1 = '=SUM(A1:A5)'
43 sheet.B1.should eq(15)
44 end
45
46 it "can set a hash" do
47 sheet.set("A1:B3", { 'one' => 'mot', 'two' => 'hai', 'three' => 'bah' } )
48 sheet.C1 = '=VLOOKUP(A1:A5)'
49 sheet.B1.should eq(15)
50 end
51
52 it "can iterate over all cells" do
53 sheet.set("A1:B3", { 'one' => 'mot', 'two' => 'hai', 'three' => 'bah' } )
54 sheet.cells.each do |label, contents|
55 # TODO
56 end
57 end
58
59 it "can bind variables to cells" do
60 sheet.A1 = 0
61 sheet.A2 = "=A1^2"
62 sheet.bind(:input, :A1)
63 sheet.bind(:output, :A2)
64 sheet.input = 5
65 sheet.output.should eq(25)
66 sheet.bindings.keys.should include :input
67 sheet.bindings.keys.should include :output
68 sheet.bindings.values.should include :A1
69 sheet.bindings.values.should include :A2
70 end
71
72 it "can define new functions" do
d2f5725 implemented user-defined functions
Jason Hutchens authored Apr 22, 2012
73 sheet.define(:FOO, lambda { |a, b| 2 * a + b / 2 })
46ec3eb wrote more tests to define expectations for first version
Jason Hutchens authored Apr 21, 2012
74 sheet.A1 = 7
75 sheet.A2 = 8
76 sheet.A3 = "=foo(A1, A2)"
77 sheet.A3.should eq(18)
6b2a557 work with strings at a low level to make serialisation easier
Jason Hutchens authored Apr 22, 2012
78 sheet.functions.should include 'FOO'
46ec3eb wrote more tests to define expectations for first version
Jason Hutchens authored Apr 21, 2012
79 end
80
81 it "can report on undefined cells" do
82 sheet.A3 = "=A2+foo(A3:B4)"
83 expected = [:A2, :A3, :A3, :B3, :B4 ]
84 sheet.undefined.map.sort.should eq(expected)
85 end
86
87 it "can detect loops when running formulas" do
88 lambda {
89 sheet.A1 = "=A2"
90 sheet.A2 = "=A1"
91 sheet.A2
92 }.should raise_error(Soroban::RecursionError)
93 end
94
95 it "can reject valid ruby code in formulas" do
96 lambda {
97 sheet.set(:A1, "=3**2")
98 }.should raise_error(Soroban::ParseError)
99 end
100
89fc461 Initial commit to soroban.
Jason Hutchens authored Apr 21, 2012
101 end
Something went wrong with that request. Please try again.