Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
wire-cell-build
util
test
test_jsonnet.cxx
Go to the documentation of this file.
1
#include "
WireCellUtil/Persist.h
"
2
#include "
WireCellUtil/Testing.h
"
3
#include "
WireCellUtil/Exceptions.h
"
4
5
#include <string>
6
#include <iostream>
7
#include <cmath>
8
#include <cstdlib>
// for getenv
9
10
using namespace
std
;
11
using namespace
WireCell
;
12
13
// from Jsonnet docs
14
const
std::string
give1
= R
"(
15
std.mergePatch({
16
person1: {
17
name: "Alice",
18
welcome: "Hello " + self.name + "!",
19
},
20
person2: self.person1 { name: "Bob" },
21
person3: self.person1 { name: std.extVar("person") },
22
}, std.extVar("override"))
23
)";
24
const
std::string
want1
= R
"({
25
"person1": {
26
"name": "Alice",
27
"welcome": "Hello Alice!"
28
},
29
"person2": {
30
"name": "Bob",
31
"welcome": "Hello Bob!"
32
},
33
"person3": {
34
"name": "Malory",
35
"welcome": "Hello Malory!"
36
},
37
"person4": {
38
"name": "Surley",
39
"welcome": "Don't call my Shirley"
40
}
41
}
42
)";
43
//' <-- make emacs c++ mode happy about matched quotes
44
45
int
main
()
46
{
47
Persist::externalvars_t
extravars{ {
"person"
,
"Malory"
} };
48
Persist::externalvars_t
extracode{ {
"override"
,
49
"{person4: {name: \"Surley\", welcome:\"Don't call my Shirley\"}}"
} };
50
51
string
got1 =
Persist::evaluate_jsonnet_text
(
give1
, extravars, extracode);
52
cerr <<
"------give:\n"
;
53
cerr <<
give1
<<
endl
;
54
cerr <<
"------got:\n"
;
55
cerr << got1 <<
endl
;
56
cerr <<
"------want:\n"
;
57
cerr <<
want1
<<
endl
;
58
cerr <<
"------\n"
;
59
Assert
(got1 ==
want1
);
60
61
try
{
62
cerr <<
"There should be errors following:\n"
;
63
string
what =
Persist::evaluate_jsonnet_text
(
"[ std.extVar(\"doesnoteexists\") ]"
);
64
cerr << what <<
endl
;
65
}
66
catch
(
Exception
&
e
) {
67
cerr <<
"Properly caught reference to nonexistent extVar\n"
;
68
}
69
70
71
if
(!
std::getenv
(
"WIRECELL_PATH"
)) {
72
cerr <<
"test_jsonnet requires setting WIRECELL_PATH to point to where 'wirecell.jsonnet' exists\n"
;
73
return
0;
74
}
75
string
text =
Persist::evaluate_jsonnet_text
(
"local wc = import \"wirecell.jsonnet\"; [ wc.pi ]"
);
76
//cerr << text << endl;
77
auto
res =
Persist::loads
(text);
78
79
const
double
mypi = 2*std::acos(0.0);
80
const
double
jspi = res[0].asDouble();
81
Assert
(
std::abs
(jspi - mypi) < 1.0e-16);
82
83
{
84
cerr <<
"Exercise Parser class:\n"
;
85
Persist::Parser
p
({}, extravars, extracode);
86
auto
j =
p
.loads(
give1
);
87
cerr << j <<
endl
;
88
}
89
90
return
0;
91
}
want1
const std::string want1
Definition:
test_jsonnet.cxx:24
Exceptions.h
WireCell::Persist::evaluate_jsonnet_text
std::string evaluate_jsonnet_text(const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition:
Persist.cxx:205
update_metadata.loads
loads
Definition:
update_metadata.py:26
string
std::string string
Definition:
nybbler.cc:12
std
STL namespace.
WireCell::Persist::externalvars_t
std::map< std::string, std::string > externalvars_t
Definition:
Persist.h:69
Assert
#define Assert
Definition:
Testing.h:7
abs
T abs(T value)
Definition:
sparse_vector_test.cc:30
main
int main()
Definition:
test_jsonnet.cxx:45
e
const double e
Definition:
gUpMuFluxGen.cxx:165
give1
const std::string give1
Definition:
test_jsonnet.cxx:14
cet::getenv
std::string getenv(std::string const &name)
Definition:
getenv.cc:15
Testing.h
WireCell
Definition:
Main.h:22
test.p
p
Definition:
test.py:223
Persist.h
WireCell::Exception
The base wire cell exception.
Definition:
Exceptions.h:31
WireCell::Persist::Parser
Definition:
Persist.h:128
endl
QTextStream & endl(QTextStream &s)
Definition:
qtextstream.cpp:2030
Generated by
1.8.11