aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/tutorial/make_tutorial.cpp
blob: ff48fd5920d33d2f96fbdda5f6959e38e0751fa7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/process/process.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <vespa/vespalib/io/mapped_file_input.h>

using namespace vespalib;

vespalib::string readFile(const vespalib::string &filename) {
    TEST_STATE(filename.c_str());
    MappedFileInput file(filename);
    ASSERT_TRUE(file.valid());
    Memory data = file.get();
    return vespalib::string(data.data, data.size);
}

vespalib::string runCommand(const vespalib::string &cmd) {
    vespalib::string out;
    ASSERT_TRUE(Process::run(cmd, out));
    return out;
}

void insertExample(const vespalib::string &name, const vespalib::string &src_dir) {
    vespalib::string str = runCommand(make_string("%s/make_example.sh %s", src_dir.c_str(),
                                             name.c_str()));
    fprintf(stdout, "%s", str.c_str());
}

void insertSource(const vespalib::string &name, const vespalib::string &src_dir) {
    vespalib::string str = runCommand(make_string("%s/make_source.sh %s", src_dir.c_str(),
                                             name.c_str()));
    fprintf(stdout, "%s", str.c_str());
}

void insertFile(const vespalib::string &name, const vespalib::string &src_dir) {
    vespalib::string str = readFile(src_dir + "/" + name);
    fprintf(stdout, "%s", str.c_str());
}

TEST_MAIN() {
    vespalib::string pre("[insert:");
    vespalib::string example("example:");
    vespalib::string source("source:");
    vespalib::string file("file:");
    vespalib::string post("]\n");

    size_t pos = 0;
    size_t end = 0;
    size_t cursor = 0;
    vespalib::string input = readFile(TEST_PATH("tutorial_source.html"));
    while ((pos = input.find(pre, cursor)) < input.size() &&
           (end = input.find(post, pos)) < input.size())
    {
        fprintf(stdout, "%.*s", (int)(pos - cursor), (input.data() + cursor));
        pos += pre.size();
        if (input.find(example, pos) == pos) {
            pos += example.size();
            insertExample(vespalib::string((input.data() + pos), (end - pos)), TEST_PATH(""));
        } else if (input.find(source, pos) == pos) {
            pos += source.size();
            insertSource(vespalib::string((input.data() + pos), (end - pos)), TEST_PATH(""));
        } else if (input.find(file, pos) == pos) {
            pos += file.size();
            insertFile(vespalib::string((input.data() + pos), (end - pos)), TEST_PATH(""));
        } else {
            vespalib::string str((input.data() + pos), (end - pos));
            TEST_FATAL(make_string("invalid directive >%s<", str.c_str()).c_str());
        }
        cursor = end + post.size();
    }
    fprintf(stdout, "%.*s", (int)(input.size() - cursor), (input.data() + cursor));
}