jsonnet 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1eae86ab6c46d74404411fd29d877109116617170f2423738623f067eef21b1
4
- data.tar.gz: ec239abefc2eaf068b36736bdfc0ef1cc7202538418575c6c19cbb077c1152d9
3
+ metadata.gz: 8557e0025fed7a2481d2bb42fc736399ef68203c42a982d9e7b3cbb7b4d9fb5c
4
+ data.tar.gz: 74c3995ba7b43df06fc928aab1790506508df78d05536df4915e5684c2c0a0a5
5
5
  SHA512:
6
- metadata.gz: 85538d0bec2ffba85010a2058b2652bb5fc59bacafe25dc2628bab9f2eb379041f8e3271273a76493b2608bd11f60494e8ab4ef0e55c57eed02272d5d024b453
7
- data.tar.gz: 6ec195d4487be4c68914081f3c83786f254d64f8da61ee7413a73a642e0e46f2d9943540534136ba1dcda69c23308485666a5fa235813e0e7bed6252f3b6e0fe
6
+ metadata.gz: 2a99ecc45d1a1e5c0518335e3a3332e6a48b199e29c50c4d3dadeaa52580ab5d1175b4205a109fc50678dff711f3814c4417e382258b8b7c959585450a4bce54
7
+ data.tar.gz: 80be3522daea154d4f7e6d4ce58bfcd217980468edce4c2db04fe00737c2a3d4130c656e165678ce114b74538e596db7216010770a79922b94984bc2bd087ca6
data/.travis.yml CHANGED
@@ -1,16 +1,19 @@
1
1
  language: ruby
2
- dist: trusty
2
+ dist: xenial
3
3
  script: bundle exec rake test
4
4
 
5
5
  matrix:
6
6
  include:
7
7
  - os: linux
8
- rvm: 2.3.4
8
+ rvm: 2.5.8
9
9
  - os: osx
10
- rvm: 2.3.4
10
+ rvm: 2.5.8
11
11
  - os: linux
12
- rvm: 2.4.0
12
+ rvm: 2.6.6
13
13
  - os: osx
14
- rvm: 2.4.0
14
+ rvm: 2.6.6
15
+ - os: linux
16
+ rvm: 2.7.2
17
+ - os: osx
18
+ rvm: 2.7.2
15
19
  fast_finish: true
16
-
data/README.md CHANGED
@@ -24,7 +24,7 @@ Or install it yourself as:
24
24
  $ gem install jsonnet
25
25
  ```
26
26
 
27
- By default this gem will compile and install Jsonnet (v0.10.0) as part of
27
+ By default this gem will compile and install Jsonnet (v0.18.0) as part of
28
28
  installation. However you can use the system version of Jsonnet if you prefer.
29
29
  This would be the recommended route if you want to use a different version
30
30
  of Jsonnet or are having problems installing this.
@@ -27,7 +27,7 @@ invoke_callback(VALUE args)
27
27
  {
28
28
  long len = RARRAY_LEN(args);
29
29
  VALUE callback = rb_ary_entry(args, 0);
30
- return rb_funcall2(callback, id_call, len - 1, RARRAY_PTR(args) + 1);
30
+ return rb_funcall2(callback, id_call, (int)(len - 1), RARRAY_PTR(args) + 1);
31
31
  }
32
32
 
33
33
  /*
@@ -13,9 +13,28 @@ unless using_system_libraries?
13
13
  require 'mini_portile2'
14
14
  message "Using mini_portile version #{MiniPortile::VERSION}\n"
15
15
 
16
- recipe = MiniPortile.new('jsonnet', 'v0.10.0')
17
- recipe.files = ['https://github.com/google/jsonnet/archive/v0.10.0.tar.gz']
16
+ recipe = MiniPortile.new('jsonnet', 'v0.18.0')
17
+ recipe.files = ['https://github.com/google/jsonnet/archive/v0.18.0.tar.gz']
18
18
  class << recipe
19
+ CORE_OBJS = %w[
20
+ desugarer.o formatter.o lexer.o libjsonnet.o parser.o pass.o static_analysis.o string_utils.o vm.o
21
+ ].map {|name| File.join('core', name) }
22
+ MD5_OBJS = %w[
23
+ md5.o
24
+ ].map {|name| File.join('third_party', 'md5', name) }
25
+ C4_CORE_OBJS = %w[
26
+ base64.o
27
+ char_traits.o
28
+ error.o
29
+ format.o
30
+ language.o
31
+ memory_resource.o
32
+ memory_util.o
33
+ time.o
34
+ ].map {|name| File.join('third_party', 'rapidyaml', 'rapidyaml', 'ext', 'c4core', 'src', 'c4', name) }
35
+ RAPID_YAML_OBJS = %w[
36
+ common.o parse.o preprocess.o tree.o
37
+ ].map {|name| File.join('third_party', 'rapidyaml', 'rapidyaml', 'src', 'c4', 'yml', name) }
19
38
 
20
39
  def compile
21
40
  # We want to create a file a library we can link to. Jsonnet provides us
@@ -24,7 +43,7 @@ unless using_system_libraries?
24
43
  # we compile the c into .o files and then create an archive that can
25
44
  # be linked to
26
45
  execute('compile', make_cmd)
27
- execute('archive', 'ar rcs libjsonnet.a core/desugarer.o core/formatter.o core/lexer.o core/libjsonnet.o core/parser.o core/pass.o core/static_analysis.o core/string_utils.o core/vm.o third_party/md5/md5.o')
46
+ execute('archive', 'ar rcs libjsonnet.a ' + target_object_files.join(' '))
28
47
  end
29
48
 
30
49
  def configured?
@@ -39,6 +58,16 @@ unless using_system_libraries?
39
58
 
40
59
  FileUtils.cp(File.join(work_path, 'libjsonnet.a'), lib_path)
41
60
  FileUtils.cp(File.join(work_path, 'include', 'libjsonnet.h'), include_path)
61
+ FileUtils.cp(File.join(work_path, 'include', 'libjsonnet_fmt.h'), include_path)
62
+ end
63
+
64
+ private
65
+ def target_object_files
66
+ if version >= 'v0.18.0'
67
+ CORE_OBJS + MD5_OBJS + C4_CORE_OBJS + RAPID_YAML_OBJS
68
+ else
69
+ CORE_OBJS + MD5_OBJS
70
+ end
42
71
  end
43
72
  end
44
73
 
@@ -47,14 +76,9 @@ unless using_system_libraries?
47
76
  # but the makefile to fail. These commands add the necessary paths to do both
48
77
  $LIBPATH = ["#{recipe.path}/lib"] | $LIBPATH
49
78
  $CPPFLAGS << " -I#{recipe.path}/include"
50
-
51
- # This resolves an issue where you can get improper linkage when compiling
52
- # and get an error like "undefined symbol: _ZTVN10__cxxabiv121__vmi_class_type_infoE"
53
- # experienced on ubuntu.
54
- # See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=193950
55
- $LIBS << " -lstdc++"
56
79
  end
57
80
 
58
81
  abort 'libjsonnet.h not found' unless have_header('libjsonnet.h')
59
82
  abort 'libjsonnet not found' unless have_library('jsonnet')
83
+ have_header('libjsonnet_fmt.h')
60
84
  create_makefile('jsonnet/jsonnet_wrap')
data/ext/jsonnet/vm.c CHANGED
@@ -1,9 +1,16 @@
1
1
  #include <libjsonnet.h>
2
+ #ifdef HAVE_LIBJSONNET_FMT_H
3
+ # include <libjsonnet_fmt.h>
4
+ #endif
2
5
  #include <ruby/ruby.h>
3
6
  #include <ruby/intern.h>
4
7
 
5
8
  #include "ruby_jsonnet.h"
6
9
 
10
+ #ifndef NORETURN
11
+ # define NORETURN(x) x
12
+ #endif
13
+
7
14
  /*
8
15
  * defines the core part of Jsonnet::VM
9
16
  */
@@ -20,8 +27,10 @@ static VALUE cVM;
20
27
  * Raised on evaluation errors in a Jsonnet VM.
21
28
  */
22
29
  static VALUE eEvaluationError;
30
+ static VALUE eFormatError;
23
31
 
24
32
  static void raise_eval_error(struct JsonnetVm *vm, char *msg, rb_encoding *enc);
33
+ static void raise_format_error(struct JsonnetVm *vm, char *msg, rb_encoding *enc);
25
34
  static VALUE str_new_json(struct JsonnetVm *vm, char *json, rb_encoding *enc);
26
35
  static VALUE fileset_new(struct JsonnetVm *vm, char *buf, rb_encoding *enc);
27
36
 
@@ -255,13 +264,137 @@ vm_set_max_trace(VALUE self, VALUE val)
255
264
  return Qnil;
256
265
  }
257
266
 
267
+ static VALUE
268
+ vm_set_fmt_indent(VALUE self, VALUE val)
269
+ {
270
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
271
+ jsonnet_fmt_indent(vm->vm, NUM2INT(val));
272
+ return val;
273
+ }
274
+
275
+ static VALUE
276
+ vm_set_fmt_max_blank_lines(VALUE self, VALUE val)
277
+ {
278
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
279
+ jsonnet_fmt_max_blank_lines(vm->vm, NUM2INT(val));
280
+ return val;
281
+ }
282
+
283
+ static VALUE
284
+ vm_set_fmt_string(VALUE self, VALUE str)
285
+ {
286
+ const char *ptr;
287
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
288
+ StringValue(str);
289
+ if (RSTRING_LEN(str) != 1) {
290
+ rb_raise(rb_eArgError, "fmt_string must have a length of 1");
291
+ }
292
+ ptr = RSTRING_PTR(str);
293
+ switch (*ptr) {
294
+ case 'd':
295
+ case 's':
296
+ case 'l':
297
+ jsonnet_fmt_string(vm->vm, *ptr);
298
+ return str;
299
+ default:
300
+ rb_raise(rb_eArgError, "fmt_string only accepts 'd', 's', or 'l'");
301
+ }
302
+ }
303
+
304
+ static VALUE
305
+ vm_set_fmt_comment(VALUE self, VALUE str)
306
+ {
307
+ const char *ptr;
308
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
309
+ StringValue(str);
310
+ if (RSTRING_LEN(str) != 1) {
311
+ rb_raise(rb_eArgError, "fmt_comment must have a length of 1");
312
+ }
313
+ ptr = RSTRING_PTR(str);
314
+ switch (*ptr) {
315
+ case 'h':
316
+ case 's':
317
+ case 'l':
318
+ jsonnet_fmt_comment(vm->vm, *ptr);
319
+ return str;
320
+ default:
321
+ rb_raise(rb_eArgError, "fmt_comment only accepts 'h', 's', or 'l'");
322
+ }
323
+ }
324
+
325
+ static VALUE
326
+ vm_set_fmt_pad_arrays(VALUE self, VALUE val)
327
+ {
328
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
329
+ jsonnet_fmt_pad_objects(vm->vm, RTEST(val) ? 1 : 0);
330
+ return val;
331
+ }
332
+
333
+ static VALUE
334
+ vm_set_fmt_pad_objects(VALUE self, VALUE val)
335
+ {
336
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
337
+ jsonnet_fmt_pad_objects(vm->vm, RTEST(val) ? 1 : 0);
338
+ return val;
339
+ }
340
+
341
+ static VALUE
342
+ vm_set_fmt_pretty_field_names(VALUE self, VALUE val)
343
+ {
344
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
345
+ jsonnet_fmt_pretty_field_names(vm->vm, RTEST(val) ? 1 : 0);
346
+ return val;
347
+ }
348
+
349
+ static VALUE
350
+ vm_set_fmt_sort_imports(VALUE self, VALUE val)
351
+ {
352
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
353
+ jsonnet_fmt_sort_imports(vm->vm, RTEST(val) ? 1 : 0);
354
+ return val;
355
+ }
356
+
357
+ static VALUE
358
+ vm_fmt_file(VALUE self, VALUE fname, VALUE encoding)
359
+ {
360
+ int error;
361
+ char *result;
362
+ rb_encoding *const enc = rb_to_encoding(encoding);
363
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
364
+
365
+ FilePathValue(fname);
366
+ result = jsonnet_fmt_file(vm->vm, StringValueCStr(fname), &error);
367
+ if (error) {
368
+ raise_format_error(vm->vm, result, rb_enc_get(fname));
369
+ }
370
+ return str_new_json(vm->vm, result, enc);
371
+ }
372
+
373
+ static VALUE
374
+ vm_fmt_snippet(VALUE self, VALUE snippet, VALUE fname)
375
+ {
376
+ int error;
377
+ char *result;
378
+ struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self);
379
+
380
+ rb_encoding *enc = rubyjsonnet_assert_asciicompat(StringValue(snippet));
381
+ FilePathValue(fname);
382
+ result = jsonnet_fmt_snippet(vm->vm, StringValueCStr(fname), StringValueCStr(snippet), &error);
383
+ if (error) {
384
+ raise_format_error(vm->vm, result, rb_enc_get(fname));
385
+ }
386
+ return str_new_json(vm->vm, result, enc);
387
+ }
388
+
258
389
  void
259
390
  rubyjsonnet_init_vm(VALUE mJsonnet)
260
391
  {
261
- cVM = rb_define_class_under(mJsonnet, "VM", rb_cData);
392
+ cVM = rb_define_class_under(mJsonnet, "VM", rb_cObject);
262
393
  rb_define_singleton_method(cVM, "new", vm_s_new, -1);
263
394
  rb_define_private_method(cVM, "eval_file", vm_evaluate_file, 3);
264
395
  rb_define_private_method(cVM, "eval_snippet", vm_evaluate, 3);
396
+ rb_define_private_method(cVM, "fmt_file", vm_fmt_file, 2);
397
+ rb_define_private_method(cVM, "fmt_snippet", vm_fmt_snippet, 2);
265
398
  rb_define_method(cVM, "ext_var", vm_ext_var, 2);
266
399
  rb_define_method(cVM, "ext_code", vm_ext_code, 2);
267
400
  rb_define_method(cVM, "tla_var", vm_tla_var, 2);
@@ -272,22 +405,30 @@ rubyjsonnet_init_vm(VALUE mJsonnet)
272
405
  rb_define_method(cVM, "gc_growth_trigger=", vm_set_gc_growth_trigger, 1);
273
406
  rb_define_method(cVM, "string_output=", vm_set_string_output, 1);
274
407
  rb_define_method(cVM, "max_trace=", vm_set_max_trace, 1);
408
+ rb_define_method(cVM, "fmt_indent=", vm_set_fmt_indent, 1);
409
+ rb_define_method(cVM, "fmt_max_blank_lines=", vm_set_fmt_max_blank_lines, 1);
410
+ rb_define_method(cVM, "fmt_string=", vm_set_fmt_string, 1);
411
+ rb_define_method(cVM, "fmt_comment=", vm_set_fmt_comment, 1);
412
+ rb_define_method(cVM, "fmt_pad_arrays=", vm_set_fmt_pad_arrays, 1);
413
+ rb_define_method(cVM, "fmt_pad_objects=", vm_set_fmt_pad_objects, 1);
414
+ rb_define_method(cVM, "fmt_pretty_field_names=", vm_set_fmt_pretty_field_names, 1);
415
+ rb_define_method(cVM, "fmt_sort_imports=", vm_set_fmt_sort_imports, 1);
416
+
417
+ rb_define_const(mJsonnet, "STRING_STYLE_DOUBLE", rb_str_new_cstr("d"));
418
+ rb_define_const(mJsonnet, "STRING_STYLE_SINGLE", rb_str_new_cstr("s"));
419
+ rb_define_const(mJsonnet, "STRING_STYLE_LEAVE", rb_str_new_cstr("l"));
420
+ rb_define_const(mJsonnet, "COMMENT_STYLE_HASH", rb_str_new_cstr("h"));
421
+ rb_define_const(mJsonnet, "COMMENT_STYLE_SLASH", rb_str_new_cstr("s"));
422
+ rb_define_const(mJsonnet, "COMMENT_STYLE_LEAVE", rb_str_new_cstr("l"));
275
423
 
276
424
  rubyjsonnet_init_callbacks(cVM);
277
425
 
278
426
  eEvaluationError = rb_define_class_under(mJsonnet, "EvaluationError", rb_eRuntimeError);
427
+ eFormatError = rb_define_class_under(mJsonnet, "FormatError", rb_eRuntimeError);
279
428
  }
280
429
 
281
- /**
282
- * raises an EvaluationError whose message is \c msg.
283
- * @param[in] vm a JsonnetVM
284
- * @param[in] msg must be a NUL-terminated string returned by \c vm.
285
- * @return never returns
286
- * @throw EvaluationError
287
- * @sa rescue_callback
288
- */
289
430
  static void
290
- raise_eval_error(struct JsonnetVm *vm, char *msg, rb_encoding *enc)
431
+ NORETURN(raise_error)(VALUE exception_class, struct JsonnetVm *vm, char *msg, rb_encoding *enc)
291
432
  {
292
433
  VALUE ex;
293
434
  const int state = rubyjsonnet_jump_tag(msg);
@@ -300,11 +441,31 @@ raise_eval_error(struct JsonnetVm *vm, char *msg, rb_encoding *enc)
300
441
  rb_jump_tag(state);
301
442
  }
302
443
 
303
- ex = rb_exc_new3(eEvaluationError, rb_enc_str_new_cstr(msg, enc));
444
+ ex = rb_exc_new3(exception_class, rb_enc_str_new_cstr(msg, enc));
304
445
  jsonnet_realloc(vm, msg, 0);
305
446
  rb_exc_raise(ex);
306
447
  }
307
448
 
449
+ /**
450
+ * raises an EvaluationError whose message is \c msg.
451
+ * @param[in] vm a JsonnetVM
452
+ * @param[in] msg must be a NUL-terminated string returned by \c vm.
453
+ * @return never returns
454
+ * @throw EvaluationError
455
+ * @sa rescue_callback
456
+ */
457
+ static void
458
+ NORETURN(raise_eval_error)(struct JsonnetVm *vm, char *msg, rb_encoding *enc)
459
+ {
460
+ raise_error(eEvaluationError, vm, msg, enc);
461
+ }
462
+
463
+ static void
464
+ NORETURN(raise_format_error)(struct JsonnetVm *vm, char *msg, rb_encoding *enc)
465
+ {
466
+ raise_error(eFormatError, vm, msg, enc);
467
+ }
468
+
308
469
  /**
309
470
  * Returns a String whose contents is equal to \c json.
310
471
  * It automatically frees \c json just after constructing the return value.
data/jsonnet.gemspec CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_runtime_dependency "mini_portile2", ">= 2.2.0"
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.7"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "test-unit", "~> 3.1.3"
27
- spec.add_development_dependency "rake-compiler", "~> 0.9.5"
24
+ spec.add_development_dependency "bundler", ">= 1.7"
25
+ spec.add_development_dependency "rake", ">= 10.0"
26
+ spec.add_development_dependency "test-unit", ">= 3.1.3"
27
+ spec.add_development_dependency "rake-compiler", ">= 0.9.5"
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonnet
2
- VERSION = "0.3.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/jsonnet/vm.rb CHANGED
@@ -14,9 +14,9 @@ module Jsonnet
14
14
  # @see #evaluate
15
15
  def evaluate(snippet, options = {})
16
16
  snippet_check = ->(key, value) { key.to_s.match(/^filename|multi$/) }
17
- snippet_options = options.select &snippet_check
18
- vm_options = options.reject &snippet_check
19
- new(vm_options).evaluate(snippet, snippet_options)
17
+ snippet_options = options.select(&snippet_check)
18
+ vm_options = options.reject(&snippet_check)
19
+ new(vm_options).evaluate(snippet, **snippet_options)
20
20
  end
21
21
 
22
22
  ##
@@ -30,9 +30,9 @@ module Jsonnet
30
30
  # @see #evaluate_file
31
31
  def evaluate_file(filename, options = {})
32
32
  file_check = ->(key, value) { key.to_s.match(/^encoding|multi$/) }
33
- file_options = options.select &file_check
34
- vm_options = options.reject &file_check
35
- new(vm_options).evaluate_file(filename, file_options)
33
+ file_options = options.select(&file_check)
34
+ vm_options = options.reject(&file_check)
35
+ new(vm_options).evaluate_file(filename, **file_options)
36
36
  end
37
37
  end
38
38
 
@@ -88,14 +88,39 @@ module Jsonnet
88
88
  eval_file(filename, encoding, multi)
89
89
  end
90
90
 
91
+ ##
92
+ # Format Jsonnet file.
93
+ #
94
+ # @param [String] filename filename of a Jsonnet source file.
95
+ # @return [String] a formatted Jsonnet representation
96
+ # @raise [FormatError] raised when the formatting results an error.
97
+ def format_file(filename, encoding: Encoding.default_external)
98
+ fmt_file(filename, encoding)
99
+ end
100
+
101
+ ##
102
+ # Format Jsonnet snippet.
103
+ #
104
+ # @param [String] jsonnet Jsonnet source string. Must be encoded in ASCII-compatible encoding.
105
+ # @param [String] filename filename of the source. Used in stacktrace.
106
+ # @return [String] a formatted Jsonnet representation
107
+ # @raise [FormatError] raised when the formatting results an error.
108
+ # @raise [UnsupportedEncodingError] raised when the encoding of jsonnt is not ASCII-compatible.
109
+ def format(jsonnet, filename: "(jsonnet)")
110
+ fmt_snippet(jsonnet, filename)
111
+ end
112
+
91
113
  ##
92
114
  # Lets the given block handle "import" expression of Jsonnet.
93
115
  # @yieldparam [String] base base path to resolve "rel" from.
94
116
  # @yieldparam [String] rel a relative or absolute path to the file to be imported
95
117
  # @yieldreturn [Array<String>] a pair of the content of the imported file and
96
118
  # its path.
97
- def handle_import
98
- self.import_callback = to_method(Proc.new)
119
+ def handle_import(&block)
120
+ if block.nil?
121
+ raise ArgumentError, 'handle_import requires a block'
122
+ end
123
+ self.import_callback = to_method(block)
99
124
  nil
100
125
  end
101
126
 
@@ -112,8 +137,11 @@ module Jsonnet
112
137
  # Also all the positional optional parameters of the body are interpreted
113
138
  # as required parameters. And the body cannot have keyword, rest or
114
139
  # keyword rest paramters.
115
- def define_function(name, body = nil)
116
- body = body ? body.to_proc : Proc.new
140
+ def define_function(name, body = nil, &block)
141
+ body = body ? body.to_proc : block
142
+ if body.nil?
143
+ raise ArgumentError, 'define_function requires a body argument or a block'
144
+ end
117
145
  params = body.parameters.map.with_index do |(type, name), i|
118
146
  raise ArgumentError, "rest or keyword parameters are not allowed: #{type}" \
119
147
  unless [:req, :opt].include? type
data/test/test_jsonnet.rb CHANGED
@@ -38,7 +38,7 @@ class TestJsonnet < Test::Unit::TestCase
38
38
  private
39
39
 
40
40
  def example_jsonnet_file
41
- example = Tempfile.open("example.jsonnet") do |f|
41
+ Tempfile.open("example.jsonnet") do |f|
42
42
  f.write %<
43
43
  local myvar = 1;
44
44
  {
data/test/test_vm.rb CHANGED
@@ -474,6 +474,96 @@ class TestVM < Test::Unit::TestCase
474
474
  end
475
475
  end
476
476
 
477
+ test "Jsonnet::VM#format_file formats Jsonnet file" do
478
+ vm = Jsonnet::VM.new
479
+ vm.fmt_indent = 4
480
+ with_example_file(%<
481
+ local myvar = 1;
482
+ {
483
+ "foo": myvar
484
+ }
485
+ >) {|fname|
486
+ result = vm.format_file(fname)
487
+ assert_equal <<-EOS, result
488
+ local myvar = 1;
489
+ {
490
+ foo: myvar,
491
+ }
492
+ EOS
493
+ }
494
+ end
495
+
496
+ test "Jsonnet::VM#format formats Jsonnet snippet" do
497
+ vm = Jsonnet::VM.new
498
+ vm.fmt_string = 'd'
499
+ result = vm.format(<<-EOS)
500
+ local myvar = 'myvar';
501
+ {
502
+ foo: [myvar,myvar]
503
+ }
504
+ EOS
505
+ assert_equal <<-EOS, result
506
+ local myvar = "myvar";
507
+ {
508
+ foo: [myvar, myvar],
509
+ }
510
+ EOS
511
+ end
512
+
513
+ test "Jsonnet::VM#fmt_string only accepts 'd', 's', or 'l'" do
514
+ vm = Jsonnet::VM.new
515
+ vm.fmt_string = Jsonnet::STRING_STYLE_DOUBLE
516
+ vm.fmt_string = Jsonnet::STRING_STYLE_SINGLE
517
+ vm.fmt_string = Jsonnet::STRING_STYLE_LEAVE
518
+ assert_raise(ArgumentError) do
519
+ vm.fmt_string = ''
520
+ end
521
+ assert_raise(ArgumentError) do
522
+ vm.fmt_string = 'a'
523
+ end
524
+ assert_raise(ArgumentError) do
525
+ vm.fmt_string = 'ds'
526
+ end
527
+ assert_raise(TypeError) do
528
+ vm.fmt_string = 0
529
+ end
530
+ end
531
+
532
+ test "Jsonnet::VM#fmt_comment only accepts 'h', 's', or 'l'" do
533
+ vm = Jsonnet::VM.new
534
+ vm.fmt_comment = Jsonnet::COMMENT_STYLE_HASH
535
+ vm.fmt_comment = Jsonnet::COMMENT_STYLE_SLASH
536
+ vm.fmt_comment = Jsonnet::COMMENT_STYLE_LEAVE
537
+ assert_raise(ArgumentError) do
538
+ vm.fmt_comment = ''
539
+ end
540
+ assert_raise(ArgumentError) do
541
+ vm.fmt_comment = 'a'
542
+ end
543
+ assert_raise(ArgumentError) do
544
+ vm.fmt_comment = 'hs'
545
+ end
546
+ assert_raise(TypeError) do
547
+ vm.fmt_comment = 0
548
+ end
549
+ end
550
+
551
+ test "Jsonnet::VM#fmt_file raises FormatError on error" do
552
+ vm = Jsonnet::VM.new
553
+ with_example_file('{foo: }') do |fname|
554
+ assert_raise(Jsonnet::FormatError) do
555
+ vm.format_file(fname)
556
+ end
557
+ end
558
+ end
559
+
560
+ test "Jsonnet::VM#fmt_snippet raises FormatError on error" do
561
+ vm = Jsonnet::VM.new
562
+ assert_raise(Jsonnet::FormatError) do
563
+ vm.format('{foo: }')
564
+ end
565
+ end
566
+
477
567
  private
478
568
  def with_example_file(content)
479
569
  Tempfile.open("example.jsonnet") {|f|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Yugui Sonoda
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-01 00:00:00.000000000 Z
11
+ date: 2022-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_portile2
@@ -28,56 +28,56 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.7'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.7'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: test-unit
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.1.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.1.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake-compiler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 0.9.5
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.9.5
83
83
  description: Wraps the official C++ implementation of Jsonnet
@@ -114,7 +114,7 @@ homepage: ''
114
114
  licenses:
115
115
  - MIT
116
116
  metadata: {}
117
- post_install_message:
117
+ post_install_message:
118
118
  rdoc_options: []
119
119
  require_paths:
120
120
  - lib
@@ -129,9 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []
132
- rubyforge_project:
133
- rubygems_version: 2.7.3
134
- signing_key:
132
+ rubygems_version: 3.3.7
133
+ signing_key:
135
134
  specification_version: 4
136
135
  summary: Jsonnet library
137
136
  test_files: