@@ -2,72 +2,107 @@ local helper = require("tests.helper")
2
2
3
3
local analyzer = require (" js-i18n.analyzer" )
4
4
5
- --- test helper
5
+ describe (" analyzer.get_node_for_key" , function ()
6
+ --- @type test.Project
7
+ local project = nil
8
+ before_each (function ()
9
+ project = helper .use_project (" i18next" )
10
+ end )
6
11
7
- --- @param get_project function (): test.Project
8
- --- @param file string
9
- --- @param assertion function (result : FindTExpressionResultItem[] , utils : table )
10
- local function test_analyze_file (get_project , file , assertion )
11
- it (" should find 't' function calls in " .. file , function ()
12
- -- Arrange
13
- local project = get_project ()
14
- vim .cmd (" e " .. project .path .. " /test_analyzer/" .. file )
12
+ after_each (function ()
13
+ vim .cmd (" bufdo bd!" )
14
+ end )
15
15
16
- -- Act
17
- local result = analyzer .find_call_t_expressions (0 )
16
+ local tests = {
17
+ -- stylua: ignore start
18
+ { key = " exists-key" , exp_key_row = 2 },
19
+ { key = " nested.key" , exp_key_row = 4 },
20
+ { key = " nested" , exp_key_row = 3 },
21
+ -- stylua: ignore end
22
+ }
18
23
19
- local assert_item = function (idx , exp )
20
- local item = result [idx ]
24
+ for _ , test in ipairs (tests ) do
25
+ it (" #hoge " .. test .key , function ()
26
+ -- Arrange
27
+ vim .cmd (" e " .. project .path .. " /locales/en/translation.json" )
21
28
22
- local function assert_key_value (key )
23
- -- stylua: ignore start
24
- assert (
25
- exp [key ] == item [key ],
26
- string.format (" result[%d].%s to be equal.\n Passed:\n (%s) %s\n Expected:\n (%s) %s" ,
27
- idx , key , type (item [key ]), item [key ], type (exp [key ]), exp [key ])
28
- )
29
- -- stylua: ignore end
29
+ -- Act
30
+ local result = analyzer .get_node_for_key (0 , vim .split (test .key , " ." , { plain = true }))
31
+
32
+ -- Assert
33
+ if not result then
34
+ error (" Key not found: " .. test .key )
30
35
end
31
- assert_key_value (" key" )
32
- assert_key_value (" key_prefix" )
33
- assert_key_value (" key_arg" )
34
- end
36
+ local key_row = result :start () + 1
37
+ assert .are .equal (test .exp_key_row , key_row )
38
+ end )
39
+ end
40
+ end )
35
41
36
- local assert_items = function (exp_list )
37
- assert .are .equal (# exp_list , # result )
38
- for idx , exp in ipairs (exp_list ) do
39
- assert_item (idx , exp )
42
+ describe (" analyzer.find_call_t_expressions" , function ()
43
+ --- @param get_project function (): test.Project
44
+ --- @param file string
45
+ --- @param assertion function (result : FindTExpressionResultItem[] , utils : table )
46
+ local function test_analyze_file (get_project , file , assertion )
47
+ it (" should find 't' function calls in " .. file , function ()
48
+ -- Arrange
49
+ local project = get_project ()
50
+ vim .cmd (" e " .. project .path .. " /test_analyzer/" .. file )
51
+
52
+ -- Act
53
+ local result = analyzer .find_call_t_expressions (0 )
54
+
55
+ local assert_item = function (idx , exp )
56
+ local item = result [idx ]
57
+
58
+ local function assert_key_value (key )
59
+ -- stylua: ignore start
60
+ assert (
61
+ exp [key ] == item [key ],
62
+ string.format (" result[%d].%s to be equal.\n Passed:\n (%s) %s\n Expected:\n (%s) %s" ,
63
+ idx , key , type (item [key ]), item [key ], type (exp [key ]), exp [key ])
64
+ )
65
+ -- stylua: ignore end
66
+ end
67
+ assert_key_value (" key" )
68
+ assert_key_value (" key_prefix" )
69
+ assert_key_value (" key_arg" )
40
70
end
41
- end
42
71
43
- -- Assert
44
- assertion (result , {
45
- assert_item = assert_item ,
46
- assert_items = assert_items ,
47
- })
48
- end )
49
- end
50
-
51
- --- @param get_project function (): test.Project
52
- --- @param text string
53
- --- @param expected boolean
54
- local function test_find_t_call (get_project , text , expected )
55
- local assertion = expected and " should" or " should NOT"
56
- it (assertion .. " find 't' function calls in `" .. text .. " `" , function ()
57
- -- Arrange
58
- local project = get_project ()
59
- vim .cmd (" e " .. project .path .. " /index.js" )
60
- vim .api .nvim_buf_set_lines (0 , 0 , - 1 , false , vim .split (text , " \n " ))
61
-
62
- -- Act
63
- local result = analyzer .find_call_t_expressions (0 )
64
-
65
- -- Assert
66
- assert .are .equal (expected and 1 or 0 , # result )
67
- end )
68
- end
72
+ local assert_items = function (exp_list )
73
+ assert .are .equal (# exp_list , # result )
74
+ for idx , exp in ipairs (exp_list ) do
75
+ assert_item (idx , exp )
76
+ end
77
+ end
78
+
79
+ -- Assert
80
+ assertion (result , {
81
+ assert_item = assert_item ,
82
+ assert_items = assert_items ,
83
+ })
84
+ end )
85
+ end
86
+
87
+ --- @param get_project function (): test.Project
88
+ --- @param text string
89
+ --- @param expected boolean
90
+ local function test_find_t_call (get_project , text , expected )
91
+ local assertion = expected and " should" or " should NOT"
92
+ it (assertion .. " find 't' function calls in `" .. text .. " `" , function ()
93
+ -- Arrange
94
+ local project = get_project ()
95
+ vim .cmd (" e " .. project .path .. " /index.js" )
96
+ vim .api .nvim_buf_set_lines (0 , 0 , - 1 , false , vim .split (text , " \n " ))
97
+
98
+ -- Act
99
+ local result = analyzer .find_call_t_expressions (0 )
100
+
101
+ -- Assert
102
+ assert .are .equal (expected and 1 or 0 , # result )
103
+ end )
104
+ end
69
105
70
- describe (" analyzer.find_call_t_expressions" , function ()
71
106
describe (" when using 'i18next'" , function ()
72
107
--- @type test.Project
73
108
local project = nil
0 commit comments