aboutsummaryrefslogtreecommitdiff
path: root/tests/expression_test.ml
blob: 5b5d99157b312ea7035f310194df76d8a417eac1 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(*
This file is part of licht.

licht is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

licht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with licht.  If not, see <http://www.gnu.org/licenses/>.
*)

open OUnit2
module T = Tools

let u = UTF8.from_utf8string

let _msg ~expected ~result =

    Printf.sprintf "Expected %s but got %s"
    (UTF8.raw_encode @@ Expression.show expected)
    (UTF8.raw_encode @@ Expression.show result)

let assert_equal expected result =
  OUnit2.assert_equal
    ~cmp:(Expression.(=))
    ~msg:(_msg ~expected ~result)
    expected result


let test_str ctx = begin
  let result = Expression.load @@ u"cafe" in
  let expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.string (u"cafe")) in
  assert_equal expected result
end

(** If the string start with space, it should be interpreted as a litteral *)
let test_str_space ctx = begin
  let result = Expression.load @@ u" =cafe" in
  let expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.string (u" =cafe")) in
  assert_equal expected result
end

let test_formula_str ctx = begin
  let result = Expression.load @@ u"=\"cafe\"" in
  let expected = Expression.load_expr @@ Expression.Formula (
    Expression.Expression (
      ScTypes.Expr.value (
        ScTypes.Type.string (u"cafe")))) in
  assert_equal expected result
end

let test_num ctx = begin
  let result = Expression.load @@ u"123" in
  let expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.number (
      DataType.Num.of_int 123
    )) in
  assert_equal expected result
end

let test_float ctx = begin
  let result = Expression.load @@ u"12.45" in
  let expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.number (
      DataType.Num.of_float @@ float_of_string "12.45"
    )) in
  assert_equal expected result
end

let test_relative ctx = begin
  let result = Expression.load @@ u"-123" in
  let expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.number (
      DataType.Num.of_int (-123)
    )) in
  assert_equal expected result
end

let test_date ctx = begin
  let result = Expression.load @@ u"1900/01/01"
  and expected = Expression.load_expr @@ Expression.Basic (
    ScTypes.Type.date (
      DataType.Date.get_julian_day 1900 01 01
    )) in
  assert_equal expected result
end

let test_sources ctx = begin
  let result = Expression.load @@ u"=A1"
             |> Expression.collect_sources in

  let expected = Cell.Set.singleton (1, 1) in

  let msg = Printf.sprintf "Expected %s but got %s"
    (UTF8.raw_encode @@ Tools.String.print_buffer Cell.Set.printb expected)
    (UTF8.raw_encode @@ Tools.String.print_buffer Cell.Set.printb result) in

  OUnit2.assert_equal ~msg expected result
end

let test_sources2 ctx = begin
  let result = Expression.load @@ u"=if($A$1>0;rand()*10+1;0)"
            |> Expression.collect_sources
  and expected = Cell.Set.singleton (1, 1) in

  let msg_buffer = UTF8.Buffer.create 16 in
  UTF8.Printf.bprintf msg_buffer "Expected %a but got %a"
    Cell.Set.printb expected
    Cell.Set.printb result;

  OUnit2.assert_equal
    ~msg:(UTF8.to_utf8string @@ UTF8.Buffer.contents msg_buffer)
    expected
    result

end

let tests = "expression_test">::: [
  (* Test litteral input *)
  "test_str"            >:: test_str;
  "test_str_space"      >:: test_str_space;

  "test_formula_str"    >:: test_formula_str;

  (* Test numeric input *)
  "test_num"            >:: test_num;
  "test_float"          >:: test_float;
  "test_relative"       >:: test_relative;

  "test_date"           >:: test_date;

  "test_sources"        >:: test_sources;
  "test_sources2"       >:: test_sources2;
]