aboutsummaryrefslogtreecommitdiff
path: root/tests/expression_test.ml
blob: 5def730d6b6b0ee4f6c155894bbaa5f233a01378 (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
open OUnit2
module T = Tools

let u = UTF8.from_utf8string

let _msg ~expected ~result =

    let get_type = function
    | Expression.Basic ScTypes.Num (ScTypes.Number, _)  -> "N"
    | Expression.Basic ScTypes.Num (ScTypes.Date, _)    -> "D"
    | Expression.Basic ScTypes.Str _                    -> "S"
    | Expression.Basic ScTypes.Bool _                   -> "B"
    | Expression.Formula _                              -> "F" in

    Printf.sprintf "Expected %s:%s but got %s:%s"
    (UTF8.raw_encode @@ Expression.show expected)
    (get_type expected)
    (UTF8.raw_encode @@ Expression.show result)
    (get_type 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.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.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.Value (
        ScTypes.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.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.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.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.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 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;

]