aboutsummaryrefslogtreecommitdiff
path: root/tests/odf/odf_ExpressionParser_test.ml
blob: 8b4e4ff7aec5c3a8f64fa382583cb4fd3102f1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
open OUnit2

let u = UTF8.from_utf8string

let _msg ~(expected:ScTypes.expression) ~(result:ScTypes.expression) =
    let b1 = UTF8.Buffer.create 16
    and b2 = UTF8.Buffer.create 16 in
    ScTypes.show_expr b1 expected;
    ScTypes.show_expr b2 result;

    Printf.sprintf "Expected \n\t%s but got \n\t%s"
      (UTF8.raw_encode @@ UTF8.Buffer.contents b1)
      (UTF8.raw_encode @@ UTF8.Buffer.contents b2)



let build_num value = ScTypes.number (
  DataType.Num.of_num @@ Num.num_of_int value
)


let test_formula ctx = begin

  let test1 = "of:=CONCATENATE(SUM([.F16:.AJ16]);\"/\";8*NETWORKDAYS([.F6]; [.F6]+(ORG.OPENOFFICE.DAYSINMONTH([.F6])-1)))" in

  let line = Lexing.from_string test1 in
  let result = Odf_ExpressionParser.value Odf_ExpressionLexer.read line in

  let expected = ScTypes.(

    Call(u"CONCATENATE", [
      Call (u"SUM", [
        Ref (Range (((6, 16), (false, false)), (((36, 16), (false, false)))))]);
      Value (string (u"/"));
      Call(u"*", [
        Value (build_num 8);
        Call(u"NETWORKDAYS", [
          Ref (Cell ((6, 6), (false, false)));
          Call(u"+", [
            Ref (Cell ((6, 6), (false, false)));
            Expression (
              Call( u"-", [
                Call(u"ORG.OPENOFFICE.DAYSINMONTH", [
                  Ref (Cell ((6, 6), (false, false)))]);
                Value (build_num 1);
              ]))])])])])) in

  assert_equal
    ~msg:(_msg ~expected ~result)
    expected
    result

end

let test_formula2 ctx = begin
  let value = "of:=+[.H51]*[.G52]" in
  let line = Lexing.from_string value in
  let result = Odf_ExpressionParser.value Odf_ExpressionLexer.read line in

  let expected = ScTypes.(

    Call (u"+", [
      Call(u"*", [
        Ref (Cell ((8, 51), (false, false)));
        Ref (Cell ((7, 52), (false, false)))
      ])])) in

  assert_equal
    ~msg:(_msg ~expected ~result)
    expected
    result

end

let tests = "odf_ExpressionParser_test">::: [

  "test_formula"              >:: test_formula;
  "test_formula2"             >:: test_formula2;

 ]