about summary refs log tree commit diff
path: root/third_party/nix/src/tests/attr-set.cc
blob: d52956f6ac023e53546a86612725dd703aa54564 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "libexpr/attr-set.hh"

#include <cstdio>
#include <optional>
#include <string>
#include <vector>

#include <absl/container/btree_map.h>
#include <bits/stdint-intn.h>
#include <gtest/gtest.h>
#include <rapidcheck.h>
#include <rapidcheck/Assertions.h>
#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/gen/Build.h>
#include <rapidcheck/gen/Create.h>
#include <rapidcheck/gen/Transform.h>
#include <rapidcheck/gtest.h>

#include "libexpr/eval.hh"
#include "libexpr/nixexpr.hh"
#include "libexpr/symbol-table.hh"
#include "libexpr/value.hh"
#include "tests/dummy-store.hh"

static nix::SymbolTable* symbol_table;

namespace rc {
using nix::Pos;
using nix::Value;

// TODO(grfn): These arbitrary implementations should be pulled out to a util
// file sooner rather than later

template <>
struct Arbitrary<nix::Symbol> {
  static Gen<nix::Symbol> arbitrary() {
    return gen::map(gen::arbitrary<std::string>(),
                    [](std::string s) { return symbol_table->Create(s); });
  }
};

template <>
struct Arbitrary<Value> {
  static Gen<nix::Value> arbitrary() {
    return gen::build(gen::construct<Value>(),
                      // TODO(grfn) generalize to more types
                      gen::set(&Value::type, gen::just(nix::ValueType::tInt)),
                      gen::set(&Value::integer, gen::arbitrary<int64_t>()));
  }
};

template <>
struct Arbitrary<Value*> {
  static Gen<nix::Value*> arbitrary() {
    return gen::apply(
        [](nix::ValueType typ, int i) {
          auto ret = new Value();
          ret->type = typ;
          ret->integer = i;
          return ret;
        },
        gen::just(nix::ValueType::tInt), gen::arbitrary<int64_t>());
  }
};

template <>
struct Arbitrary<nix::Pos> {
  static Gen<nix::Pos> arbitrary() {
    return gen::construct<nix::Pos>(gen::arbitrary<nix::Symbol>(),
                                    gen::arbitrary<unsigned int>(),
                                    gen::arbitrary<unsigned int>());
  }
};

template <>
struct Arbitrary<nix::Pos*> {
  static Gen<nix::Pos*> arbitrary() {
    return gen::apply(
        [](unsigned int line, unsigned int column) {
          return new Pos({}, line, column);
        },
        gen::arbitrary<unsigned int>(), gen::arbitrary<unsigned int>());
  }
};

template <>
struct Arbitrary<nix::Attr> {
  static Gen<nix::Attr> arbitrary() {
    return gen::construct<nix::Attr>(gen::arbitrary<nix::Symbol>(),
                                     gen::arbitrary<Value*>(),
                                     gen::arbitrary<nix::Pos*>());
  }
};

template <>
struct Arbitrary<nix::Bindings> {
  static Gen<nix::Bindings> arbitrary() {
    return gen::map(gen::arbitrary<std::vector<nix::Attr>>(), [](auto attrs) {
      nix::Bindings res;
      for (const auto& attr : attrs) {
        res.push_back(attr);
      }
      return res;
    });
  }
};

}  // namespace rc

namespace nix {

using nix::tests::DummyStore;

class AttrSetTest : public ::testing::Test {
 protected:
  EvalState* eval_state_;
  void SetUp() override {
    nix::expr::InitGC();
    auto store = std::make_shared<DummyStore>();
    eval_state_ = new EvalState({"."}, ref<Store>(store));
    symbol_table = &eval_state_->symbols;
  }

  void assert_bindings_equal(nix::Bindings& lhs, nix::Bindings& rhs) {
    Value lhs_val;
    Value rhs_val;
    lhs_val.type = rhs_val.type = ValueType::tAttrs;
    lhs_val.attrs = &lhs;
    rhs_val.attrs = &lhs;

    RC_ASSERT(eval_state_->eqValues(lhs_val, rhs_val));
  }
};

class AttrSetMonoidTest : public AttrSetTest {};

RC_GTEST_FIXTURE_PROP(AttrSetMonoidTest, mergeLeftIdentity,
                      (nix::Bindings && bindings)) {
  auto empty_bindings = nix::Bindings::NewGC();
  auto result = *Bindings::Merge(*empty_bindings, bindings);
  assert_bindings_equal(result, bindings);
}

RC_GTEST_FIXTURE_PROP(AttrSetMonoidTest, mergeRightIdentity,
                      (nix::Bindings && bindings)) {
  auto empty_bindings = nix::Bindings::NewGC();
  auto result = *Bindings::Merge(bindings, *empty_bindings);
  assert_bindings_equal(result, bindings);
}

RC_GTEST_FIXTURE_PROP(AttrSetMonoidTest, mergeAssociative,
                      (nix::Bindings && bindings_1, nix::Bindings&& bindings_2,
                       nix::Bindings&& bindings_3)) {
  assert_bindings_equal(
      *Bindings::Merge(bindings_1, *Bindings::Merge(bindings_2, bindings_3)),
      *Bindings::Merge(*Bindings::Merge(bindings_1, bindings_2), bindings_3));
}

}  // namespace nix