aboutsummaryrefslogtreecommitdiff
path: root/tests/expressionParser_test.ml
blob: 12ceeb07c20da7f24bb6a16c695e29e477b61cb1 (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
81
82
83
84
85
86
open OUnit2

let u = UTF8.from_utf8string

let _msg ~(expected:Expression.t) ~(result:Expression.t) =
    Printf.sprintf "Expected %s but got %s"
    (UTF8.raw_encode @@ Expression.show @@ Expression.load_expr expected)
    (UTF8.raw_encode @@ Expression.show @@ Expression.load_expr result)

let load_expr str =
  Expression.Formula (
    Expression.Expression(
      ExpressionParser.value ExpressionLexer.read
      @@ Lexing.from_string str )
  )


let test_num ctx = begin

  let expected = Expression.Formula (
    Expression.Expression (
      ScTypes.Value (ScTypes.number (
          DataType.Num.of_int 1
        )
      ))) in
  let result = load_expr "=1" in

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

let test_call ctx = begin

  let expected = Expression.Formula (
    Expression.Expression (
      ScTypes.Call (
        u"sum", []))) in
  let result = load_expr "=sum()" in

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

let test_call2 ctx = begin

  (*  The expression "foo2(" has to be parsed as a function call, and not as a
      reference to cell "FOO2", followed by another expression. *)

  let expected = Expression.Formula (
    Expression.Expression (
      ScTypes.Call (
        u"foo2", [ScTypes.Value (ScTypes.number (
          DataType.Num.of_int 4
        ))]))) in
  let result = load_expr "=foo2(4)" in

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

let test_ref ctx = begin

  let expected = Expression.Formula (
    Expression.Expression (
      ScTypes.Ref(
        ScTypes.Cell ((1, 3), (false, false))))) in
  let result = load_expr "=A3" in

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

let tests = "expression_parser_test">::: [
  "test_num"    >:: test_num;
  "test_call"   >:: test_call;
  "test_call2"  >:: test_call2;
  "test_ref"    >:: test_ref;
]