-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocTests.fs
195 lines (157 loc) · 7.48 KB
/
LocTests.fs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
namespace Loc
module LocTests =
open LocParser
open FsUnit.Xunit
open Xunit
open FParsec
open System
open System.Collections.Generic
open System.IO
open System.Text
let testParser p str =
match run p str with
| Success(result, _, _) -> Some result
| Failure(_, _, _) -> None
[<Fact>]
let ``Quotated string must be Literal`` () =
let str = "'abc'"
let expected = Some(Value.Literal "abc")
testParser vliteral str |> should equal expected
[<Fact>]
let ``Quotated string with escaped symbols must be Literal`` () =
let expectedStr = "a b s ' sas 123 __ !@#$%^ \n 02980"
let str = "'a b s \\' sas 123 __ !@#$%^ \\n 02980'"
let expected = Some(Value.Literal expectedStr)
testParser vliteral str |> should equal expected
[<Fact>]
let ``Not quotated string is not Literal`` () =
let str = "'abc"
testParser vliteral str |> should equal None
[<Fact>]
let ``DollarSign names is Param`` () =
let str = "$abc"
let expected = Some(Value.Param "abc")
testParser vparam str |> should equal expected
[<Fact>]
let ``Without dollarSign names is not Param`` () =
let str = "abc"
testParser vparam str |> should equal None
[<Fact>]
let ``DollarSign names with plural forms must be ParamPluralForm`` () =
let str = "{$abc: 'p1', 'p2'}"
let expected = Some(Value.ParamPluralForm("abc",["p1";"p2"]))
testParser vparamPluralForm str |> should equal expected
[<Fact>]
let ``Without dollarSign names with plural forms Is't ParamPluralForm`` () =
let str = "{abc: 'p1', 'p2'}"
testParser vparam str |> should equal None
[<Fact>]
let ``Without quots and with dollarSign names with plural forms Is't ParamPluralForm`` () =
let str = "{$abc: p1, 'p2'}"
testParser vparam str |> should equal None
[<Fact>]
let ``Literal must be Value`` () =
let str = "'abc'"
let expected = Some([Value.Literal "abc"])
testParser value str |> should equal expected
[<Fact>]
let ``Param must be Value`` () =
let str = "$abc"
let expected = Some([Value.Param "abc"])
testParser value str |> should equal expected
[<Fact>]
let ``ParamPluralForm must be Value`` () =
let str = "{$abc: 'p1', 'p2'}"
let expected = Some([Value.ParamPluralForm("abc",["p1";"p2"])])
testParser value str |> should equal expected
[<Fact>]
let ``KeyRef must be Value`` () =
let str = "abc"
let expected = Some([Value.KeyRef "abc"])
testParser value str |> should equal expected
[<Fact>]
let ``Literal + Param + ParamPluralForm + KeyRef must be Value`` () =
let str = "'literal' + $param + {$param: 'p1', 'p2'} + keyref"
let expected =Some [Literal "literal"; Param "param"; ParamPluralForm ("param",["p1"; "p2"]); KeyRef "keyref"]
testParser value str |> should equal expected
[<Fact>]
let ``Id equal value must be LocData`` () =
let str = "a = 'abc'"
let expected = Some(Data("a",[Value.Literal "abc"]))
testParser locData str |> should equal expected
[<Fact>]
let ``Id empty value Isn't LocData`` () =
let str = "a 'abc'"
testParser locData str |> should equal None
[<Fact>]
let ``Comment is LocData`` () =
let str = "; comment"
let expected = Some( Loc.Comment "; comment")
testParser locComment str |> should equal expected
[<Fact>]
let ``Eval Literal LocData`` () =
let data = [Value.Literal "test"]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> v :> Object) emptyDict |> should equal "test"
[<Fact>]
let ``Eval Param LocData`` () =
let data = [Value.Param "param"]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> (v+"1") :> Object) emptyDict |> should equal "param1"
[<Fact>]
let ``Eval ParamPluralForm select p1 LocData`` () =
let data = [ParamPluralForm ("param",["p1"; "p2"; "p3"])]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> 1 :> Object) emptyDict |> should equal "p1"
[<Fact>]
let ``Eval ParamPluralForm select p2 LocData`` () =
let data = [ParamPluralForm ("param",["p1"; "p2"; "p3"])]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> 2 :> Object) emptyDict |> should equal "p2"
[<Fact>]
let ``Eval ParamPluralForm select p3 LocData`` () =
let data = [ParamPluralForm ("param",["p1"; "p2"; "p3"])]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> 10 :> Object) emptyDict |> should equal "p3"
[<Fact>]
let ``Eval KeyRef LocData`` () =
let data = [Value.KeyRef "key1"]
let emptyDict : IDictionary<string,Value list> = dict[("key1", [Value.Literal "Key1Data"])]
evalLocValue data "ru" (fun v -> v :> Object) emptyDict |> should equal "Key1Data"
[<Fact>]
let ``Eval Literals LocData must be in correct order`` () =
let data = [Value.Literal "test1"; Value.Literal "test2"; Value.Literal "test3"]
let emptyDict : IDictionary<string,Value list> = dict[]
evalLocValue data "ru" (fun v -> v :> Object) emptyDict |> should equal "test1test2test3"
[<Fact>]
let ``Eval nested KeyRef LocData`` () =
let data = [Value.KeyRef "key1"]
let emptyDict : IDictionary<string,Value list> = dict[("key1", [Value.KeyRef "Key2"]); ("Key2", [Value.Literal "Data"])]
evalLocValue data "ru" (fun v -> v :> Object) emptyDict |> should equal "Data"
[<Fact>]
let ``Loc file is LocData list`` () =
let str = @"; comment
key1 = 'data1'
key2 = 'data2' + $param"
let expected =Some( [Comment "; comment";
Data ("key1",[Literal "data1"]);
Data ("key2",[Literal "data2"; Param "param"])])
testParser locFile str |> should equal expected
[<Fact(Skip = "Fill correct path and keys")>]
let ``Real loc file is LocData list`` () =
let path = @"ru\locs\Common.txt"
use stream = File.OpenRead(path)
let parserResult = match loadLocFile stream Encoding.UTF8 with
| Success(result, _, _) -> Some result
| Failure(_, _, _) -> None
do (parserResult.IsSome |> should equal true)
let locDataMap = parserResult.Value
|> List.filter (fun v -> match v with
| Loc.Comment x -> false
| _ -> true)
|> List.map (fun v -> match v with
|Loc.Data(key,value) -> (key, value)
|_ -> failwith "unexpected")
|> dict
evalLocValue locDataMap.["Key1"] "ru" (fun v -> v :> Object) locDataMap |> should equal "Fill correct data for Key1"
evalLocValue locDataMap.["Key2"] "ru" (fun v -> 1 :> Object) locDataMap |> should equal "Fill correct data for Key2"