Rename tests consistently with the naming convention
[idea/community.git] / plugins / stats-collector / log-events / src / test / kotlin / com / intellij / stats / completion / EventStreamValidatorTest.kt
1 /*
2  * Copyright 2000-2017 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.intellij.stats.completion
18
19 import com.intellij.stats.completion.events.LogEvent
20 import com.intellij.stats.validation.InputSessionValidator
21 import com.intellij.stats.validation.SimpleSessionValidationResult
22 import org.assertj.core.api.Assertions.assertThat
23 import org.junit.Test
24
25 private fun List<LogEvent>.serialize(): List<String> = map { LogEventSerializer.toString(it) }
26
27
28 class EventStreamValidatorTest {
29
30     @Test
31     fun `simple sequence of actions`() {
32         val list = listOf(LogEventFixtures.completion_started_3_items_shown, LogEventFixtures.explicit_select_position_0)
33         validate(list, list.map { LogEventSerializer.toString(it) }, emptyList())
34     }
35
36     @Test
37     fun `sample error sequence of actions`() {
38         val list = listOf(LogEventFixtures.completion_started_3_items_shown, LogEventFixtures.explicit_select_position_1)
39         validate(list, expectedOut = emptyList(), expectedErr = list.serialize())
40     }
41
42     @Test
43     fun `up down actions`() {
44         val list = listOf(
45                 LogEventFixtures.completion_started_3_items_shown,
46                 LogEventFixtures.down_event_new_pos_1,
47                 LogEventFixtures.up_pressed_new_pos_0,
48                 LogEventFixtures.up_pressed_new_pos_2,
49                 LogEventFixtures.up_pressed_new_pos_1,
50                 LogEventFixtures.explicit_select_position_1
51         )
52         validate(list, list.serialize(), expectedErr = emptyList())
53     }
54
55     @Test
56     fun `up down actions wrong`() {
57         val list = listOf(
58                 LogEventFixtures.completion_started_3_items_shown,
59                 LogEventFixtures.down_event_new_pos_1,
60                 LogEventFixtures.up_pressed_new_pos_0,
61                 LogEventFixtures.up_pressed_new_pos_2,
62                 LogEventFixtures.up_pressed_new_pos_1,
63                 LogEventFixtures.explicit_select_position_0
64         )
65         validate(list, expectedOut = emptyList(), expectedErr = list.serialize())
66     }
67
68     @Test
69     fun `selected by typing relaxed conditions`() {
70         val list = listOf(
71                 LogEventFixtures.completion_started_3_items_shown,
72                 LogEventFixtures.type_event_current_pos_0_left_ids_1_2,
73                 LogEventFixtures.type_event_current_pos_0_left_id_0,
74                 LogEventFixtures.selected_by_typing_0
75         )
76         validate(list, expectedOut = list.serialize(), expectedErr = emptyList())
77     }
78
79     @Test
80     fun `selected by typing error`() {
81         val list = listOf(
82                 LogEventFixtures.completion_started_3_items_shown,
83                 LogEventFixtures.type_event_current_pos_0_left_ids_0_1,
84                 LogEventFixtures.down_event_new_pos_1,
85                 LogEventFixtures.explicit_select_position_1
86         )
87         validate(list, expectedOut = list.serialize(), expectedErr = emptyList())
88     }
89
90     private fun validate(list: List<LogEvent>,
91                          expectedOut: List<String>,
92                          expectedErr: List<String>) {
93         val input: List<String> = list.map { LogEventSerializer.toString(it) }
94         val result = SimpleSessionValidationResult()
95         val separator = InputSessionValidator(result)
96         separator.validate(input)
97
98         assertThat(result.errorLines).isEqualTo(expectedErr)
99         assertThat(result.validLines).isEqualTo(expectedOut)
100     }
101
102 }