aboutsummaryrefslogtreecommitdiff
path: root/unit-tests/test/script/parser/driver.cxx
blob: 6e3fed5f2e20a32bd1f2ccb126ef4c8e0c83b3f3 (plain)
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
// file      : unit-tests/test/script/parser/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>
#include <iostream>

#include <build2/types>
#include <build2/utility>

#include <build2/target>
#include <build2/context>

#include <build2/test/target>

#include <build2/test/script/token>
#include <build2/test/script/parser>
#include <build2/test/script/runner>

using namespace std;

namespace build2
{
  namespace test
  {
    namespace script
    {
      // Here we assume we are running serially.
      //
      class print_runner: public runner
      {
      public:
        print_runner (bool scope): scope_ (scope) {}

        virtual void
        enter (scope&, const location&) override
        {
          if (scope_)
          {
            cout << ind_ << "{" << endl;
            ind_ += "  ";
          }
        }

        virtual void
        run (scope&, const command& t, size_t, const location&) override
        {
          cout << ind_ << t << endl;
        }

        virtual void
        leave (scope&, const location&) override
        {
          if (scope_)
          {
            ind_.resize (ind_.size () - 2);
            cout << ind_ << "}" << endl;
          }
        }

      private:
        bool scope_;
        string ind_;
      };

      // Usage: argv[0] [-s] [<testscript-name>]
      //
      int
      main (int argc, char* argv[])
      {
        tracer trace ("main");

        init ("false", 1);  // No build system driver, default verbosity.
        reset (strings ()); // No command line variables.

        bool scope (false);
        path name;

        for (int i (1); i != argc; ++i)
        {
          string a (argv[i]);

          if (a == "-s")
            scope = true;
          else
          {
            name = path (move (a));
            break;
          }
        }

        if (name.empty ())
          name = path ("testscript");

        try
        {
          cin.exceptions (istream::failbit | istream::badbit);

          // Enter mock targets. Use fixed names and paths so that we can use
          // them in expected results. Strictly speaking target paths should
          // be absolute. However, the testscript implementation doesn't
          // really care.
          //
          file& tt (
            targets.insert<file> (work,
                                  dir_path (),
                                  "driver",
                                  &extension_pool.find (""),
                                  trace));

          testscript& st (
            targets.insert<testscript> (work,
                                        dir_path (),
                                        "testscript",
                                        &extension_pool.find (""),
                                        trace));

          tt.path (path ("driver"));
          st.path (name);

          // Parse and run.
          //
          script s (tt, st, dir_path (work) /= "test-driver");
          print_runner r (scope);

          parser p;
          p.pre_parse (cin, name, s);
          p.parse (name, s, r);
        }
        catch (const failed&)
        {
          return 1;
        }

        return 0;
      }
    }
  }
}

int
main (int argc, char* argv[])
{
  return build2::test::script::main (argc, argv);
}