about summary refs log tree commit diff stats
path: root/test_history.c
blob: f455d7d11840e5dfc6b1d0413e107844f32eaa1b (plain) (blame)
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
#include <stdlib.h>
#include <head-unit.h>
#include "history.h"

static void beforetest(void)
{
    history_init();
}

static void previous_returns_null_after_init(void)
{
    char *prev = history_previous();
    assert_is_null(prev);
}

static void next_returns_null_after_init(void)
{
    char *next = history_next();
    assert_is_null(next);
}

static void append_after_init_doesnt_fail(void)
{
    history_append("try append");
    assert_true(1);
}

static void append_then_previous_returns_appended(void)
{
    history_append("try append");
    char *prev = history_previous();
    assert_string_equals("try append", prev); 
}

static void append_then_next_returns_null(void)
{
    history_append("try append");
    char *next = history_next();
    assert_is_null(next);
}

static void hits_null_at_top(void)
{
    history_append("cmd1");
    history_append("cmd2");
    history_previous(); // cmd2
    history_previous(); // cmd1
    char *prev = history_previous();
    assert_is_null(prev);
}

static void navigate_to_correct_item(void)
{
    history_append("cmd1");
    history_append("cmd2");
    history_append("cmd3");
    history_append("cmd4");
    history_append("cmd5");
    history_append("cmd6");

    history_previous(); // cmd6
    history_previous(); // cmd5
    history_previous(); // cmd4
    history_previous(); // cmd3
    history_next(); // cmd4
    history_previous(); // cmd3
    history_previous(); // cmd2
    char *str = history_next(); // cmd3

    assert_string_equals("cmd3", str);
}

static void append_previous_item(void)
{
    history_append("cmd1");
    history_append("cmd2");
    history_append("cmd3");
    history_append("cmd4");
    history_append("cmd5");
    history_append("cmd6");

    history_previous(); // cmd6
    history_previous(); // cmd5
    history_previous(); // cmd4
    history_previous(); // cmd3
    history_next(); // cmd4
    history_previous(); // cmd3
    history_previous(); // cmd2
    char *str = history_next(); // cmd3

    history_append(str);

    char *cmd3_1 = history_previous();
    assert_string_equals("cmd3", cmd3_1);

    char *cmd6 = history_previous();
    assert_string_equals("cmd6", cmd6);

    char *cmd5 = history_previous();
    assert_string_equals("cmd5", cmd5);

    char *cmd4 = history_previous();
    assert_string_equals("cmd4", cmd4);

    char *cmd3 = history_previous();
    assert_string_equals("cmd3", cmd3);

    char *cmd2 = history_previous();
    assert_string_equals("cmd2", cmd2);

    char *cmd1 = history_previous();
    assert_string_equals("cmd1", cmd1);

    char *end = history_previous();
    assert_is_null(end);
}

void register_history_tests(void)
{
    TEST_MODULE("history tests");
    BEFORETEST(beforetest);
    TEST(previous_returns_null_after_init);
    TEST(next_returns_null_after_init);
    TEST(append_after_init_doesnt_fail);
    TEST(append_then_previous_returns_appended);
    TEST(append_then_next_returns_null);
    TEST(hits_null_at_top);
    TEST(navigate_to_correct_item);
    TEST(append_previous_item);
}