Functions | Variables
test_jsonnet.cxx File Reference
#include "WireCellUtil/Persist.h"
#include "WireCellUtil/Testing.h"
#include "WireCellUtil/Exceptions.h"
#include <string>
#include <iostream>
#include <cmath>
#include <cstdlib>

Go to the source code of this file.

Functions

int main ()
 

Variables

const std::string give1
 
const std::string want1
 

Function Documentation

int main ( void  )

Definition at line 45 of file test_jsonnet.cxx.

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 }
const std::string want1
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
std::map< std::string, std::string > externalvars_t
Definition: Persist.h:69
#define Assert
Definition: Testing.h:7
T abs(T value)
const double e
const std::string give1
std::string getenv(std::string const &name)
Definition: getenv.cc:15
p
Definition: test.py:223
The base wire cell exception.
Definition: Exceptions.h:31
QTextStream & endl(QTextStream &s)

Variable Documentation

const std::string give1
Initial value:
= R"(
std.mergePatch({
person1: {
name: "Alice",
welcome: "Hello " + self.name + "!",
},
person2: self.person1 { name: "Bob" },
person3: self.person1 { name: std.extVar("person") },
}, std.extVar("override"))
)"

Definition at line 14 of file test_jsonnet.cxx.

const std::string want1
Initial value:
= R"({
"person1": {
"name": "Alice",
"welcome": "Hello Alice!"
},
"person2": {
"name": "Bob",
"welcome": "Hello Bob!"
},
"person3": {
"name": "Malory",
"welcome": "Hello Malory!"
},
"person4": {
"name": "Surley",
"welcome": "Don't call my Shirley"
}
}
)"

Definition at line 24 of file test_jsonnet.cxx.