jiffy 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +27 -28
  5. data/Rakefile +17 -0
  6. data/bin/jiffy +28 -3
  7. data/jiffy.gemspec +15 -66
  8. data/lib/jiffy.rb +27 -27
  9. data/lib/jiffy/array_mimicking_io.rb +1 -1
  10. data/lib/jiffy/outputter.rb +85 -0
  11. data/lib/jiffy/outputters/json.rb +35 -0
  12. data/lib/jiffy/outputters/ruby.rb +35 -0
  13. data/lib/jiffy/parser.rb +17 -0
  14. data/lib/jiffy/parsers/json.rb +18 -42
  15. data/lib/jiffy/parsers/json.rl +10 -22
  16. data/lib/jiffy/parsers/json_array.rb +22 -29
  17. data/lib/jiffy/parsers/json_array.rl +11 -12
  18. data/lib/jiffy/parsers/json_float.rb +39 -21
  19. data/lib/jiffy/parsers/json_float.rl +15 -7
  20. data/lib/jiffy/parsers/json_object.rb +30 -49
  21. data/lib/jiffy/parsers/json_object.rl +13 -20
  22. data/lib/jiffy/parsers/json_string.rb +70 -27
  23. data/lib/jiffy/parsers/json_string.rl +21 -6
  24. data/lib/jiffy/parsers/json_value.rb +33 -52
  25. data/lib/jiffy/parsers/json_value.rl +15 -34
  26. data/lib/jiffy/version.rb +1 -1
  27. data/test/jiffy_test.rb +261 -34
  28. data/test/outputter_test.rb +155 -0
  29. data/test/outputters/json_test.rb +22 -0
  30. metadata +9 -60
  31. data/lib/jiffy/json_outputter.rb +0 -62
  32. data/test/negative-examples/hexadecimal.json +0 -3
  33. data/test/negative-examples/infinity-value.json +0 -3
  34. data/test/negative-examples/leading-comma.json +0 -3
  35. data/test/negative-examples/leading-zero.json +0 -3
  36. data/test/negative-examples/line-break.json +0 -4
  37. data/test/negative-examples/missing-colon.json +0 -3
  38. data/test/negative-examples/nan-value,json +0 -3
  39. data/test/negative-examples/positive-float.json +0 -3
  40. data/test/negative-examples/positive-integer.json +0 -3
  41. data/test/negative-examples/single-quote.json +0 -3
  42. data/test/negative-examples/string-as-root.json +0 -1
  43. data/test/negative-examples/tab-character.json +0 -3
  44. data/test/negative-examples/trailing-array-seperator.json +0 -3
  45. data/test/negative-examples/trailing-object-seperator.json +0 -3
  46. data/test/negative-examples/true-as-root.json +0 -1
  47. data/test/negative-examples/unclosed-array.json +0 -2
  48. data/test/negative-examples/unclosed-object.json +0 -2
  49. data/test/positive-examples/array-as-root.json +0 -3
  50. data/test/positive-examples/array-nested-inside-array.json +0 -5
  51. data/test/positive-examples/array-nested-inside-object.json +0 -5
  52. data/test/positive-examples/false-value.json +0 -3
  53. data/test/positive-examples/null-value.json +0 -3
  54. data/test/positive-examples/number-1.json +0 -3
  55. data/test/positive-examples/number-10.json +0 -3
  56. data/test/positive-examples/number-11.json +0 -3
  57. data/test/positive-examples/number-12.json +0 -3
  58. data/test/positive-examples/number-13.json +0 -3
  59. data/test/positive-examples/number-14.json +0 -3
  60. data/test/positive-examples/number-15.json +0 -3
  61. data/test/positive-examples/number-16.json +0 -3
  62. data/test/positive-examples/number-17.json +0 -3
  63. data/test/positive-examples/number-18.json +0 -3
  64. data/test/positive-examples/number-19.json +0 -3
  65. data/test/positive-examples/number-2.json +0 -3
  66. data/test/positive-examples/number-20.json +0 -3
  67. data/test/positive-examples/number-3.json +0 -3
  68. data/test/positive-examples/number-4.json +0 -3
  69. data/test/positive-examples/number-5.json +0 -3
  70. data/test/positive-examples/number-6.json +0 -3
  71. data/test/positive-examples/number-7.json +0 -3
  72. data/test/positive-examples/number-8.json +0 -3
  73. data/test/positive-examples/number-9.json +0 -3
  74. data/test/positive-examples/object-as-root.json +0 -3
  75. data/test/positive-examples/object-nested-inside-array.json +0 -5
  76. data/test/positive-examples/object-nested-inside-object.json +0 -5
  77. data/test/positive-examples/seperated-array-values.json +0 -4
  78. data/test/positive-examples/seperated-object-properties.json +0 -4
  79. data/test/positive-examples/string-backspace.json +0 -3
  80. data/test/positive-examples/string-carriage-return.json +0 -3
  81. data/test/positive-examples/string-formfeed.json +0 -3
  82. data/test/positive-examples/string-horizontal-tab.json +0 -3
  83. data/test/positive-examples/string-newline.json +0 -3
  84. data/test/positive-examples/string-quotation.json +0 -3
  85. data/test/positive-examples/string-reverse-solidus.json +0 -3
  86. data/test/positive-examples/string-solidus.json +0 -3
  87. data/test/positive-examples/string-trivial.json +0 -3
  88. data/test/positive-examples/string-unicode.json +0 -3
  89. data/test/positive-examples/true-value.json +0 -3
@@ -0,0 +1,155 @@
1
+ require "stringio"
2
+ require "minitest/autorun"
3
+ require "jiffy/outputter"
4
+
5
+ def create_test_outputter(options = {})
6
+ options[:out] = StringIO.new
7
+
8
+ [options[:out], Jiffy::Outputter.new(options)]
9
+ end
10
+
11
+ describe Jiffy::Outputter do
12
+ describe "::rule" do
13
+ it "should raise an error when specifying both :break_before and :break_after" do
14
+ assert_raises ArgumentError do
15
+ Jiffy::Outputter.rule :foo, break_before: true, break_after: true
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "#initialize" do
21
+ it 'should raise an error when :out does not respond to #print' do
22
+ out = Object.new
23
+
24
+ def out.respond_to?(method)
25
+ if method == :print
26
+ false
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ assert_raises ArgumentError do
33
+ Jiffy::Outputter.new out: out
34
+ end
35
+ end
36
+
37
+ it "should raise an error when :cur_indent is not a Fixnum" do
38
+ assert_raises ArgumentError do
39
+ Jiffy::Outputter.new cur_indent: "not a fixnum"
40
+ end
41
+ end
42
+
43
+ it "should raise an error when :indent_size is not a Fixnum" do
44
+ assert_raises ArgumentError do
45
+ Jiffy::Outputter.new indent_size: "not a fixnum"
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#process_token" do
51
+ it "should raise an error when invoked with a token that has no rule" do
52
+ dummy_outputter = Class.new Jiffy::Outputter do
53
+ rule :foo
54
+ end
55
+
56
+ assert_raises ArgumentError do
57
+ dummy_outputter.new.process_token :bar
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#apply_rule" do
63
+ describe ":break_before option" do
64
+ it "should create a newline before the payload" do
65
+ out, outputter = create_test_outputter
66
+
67
+ outputter.apply_rule break_before: true, payload: "foo"
68
+
69
+ assert_equal out.string, "\nfoo"
70
+ end
71
+
72
+ it "should respect the value for current indentation and size" do
73
+ out, outputter = create_test_outputter cur_indent: 1, indent_size: 4
74
+
75
+ outputter.apply_rule break_before: true, payload: "foo"
76
+
77
+ assert_equal out.string, "\n foo"
78
+ end
79
+ end
80
+
81
+ describe ":break_after option" do
82
+ it "should create a newline after the payload" do
83
+ out, outputter = create_test_outputter
84
+
85
+ outputter.apply_rule break_after: true, payload: "foo"
86
+
87
+ assert_equal out.string, "foo\n"
88
+ end
89
+
90
+ it "should respect the value for current indentation and size" do
91
+ out, outputter = create_test_outputter cur_indent: 1, indent_size: 4
92
+
93
+ outputter.apply_rule break_after: true, payload: "foo"
94
+
95
+ assert_equal out.string, "foo\n "
96
+ end
97
+ end
98
+
99
+ describe ":indent option" do
100
+ it "should change cur_indent" do
101
+ _, outputter = create_test_outputter
102
+
103
+ outputter.apply_rule indent: 1
104
+
105
+ assert_equal outputter.cur_indent, 1
106
+ end
107
+ end
108
+
109
+ describe ":format option" do
110
+ it "should invoke the given callback to format the payload" do
111
+ out, outputter = create_test_outputter
112
+
113
+ outputter.apply_rule format: :upcase.to_proc, payload: "foo"
114
+
115
+ assert_equal out.string, "FOO"
116
+ end
117
+ end
118
+
119
+ describe "given that colors are enabled" do
120
+ describe ":color option" do
121
+ it "should change last_color" do
122
+ _, outputter = create_test_outputter color: true
123
+
124
+ outputter.apply_rule color: :green
125
+
126
+ assert_equal outputter.last_color, :green
127
+ end
128
+
129
+ it "should output a color sequence when last_color is nil" do
130
+ out, outputter = create_test_outputter color: true, last_color: nil
131
+
132
+ outputter.apply_rule color: :green
133
+
134
+ assert_match "\e[0;32m", out.string
135
+ end
136
+
137
+ it "should output a color sequence when last_color is a different color" do
138
+ out, outputter = create_test_outputter color: true, last_color: :red
139
+
140
+ outputter.apply_rule color: :green
141
+
142
+ assert_match "\e[0;32m", out.string
143
+ end
144
+
145
+ it "should set last_color to nil when given no :color option" do
146
+ _, outputter = create_test_outputter color: true, last_color: :red
147
+
148
+ outputter.apply_rule color: nil
149
+
150
+ assert_equal outputter.last_color, nil
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'jiffy/outputters/json'
3
+
4
+ describe Jiffy::JsonOutputter do
5
+ describe '#initialize' do
6
+ it 'should raise an error when :out does not respond to #print' do
7
+ out = Object.new
8
+
9
+ def out.respond_to?(method)
10
+ if method == :print
11
+ false
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ assert_raises ArgumentError do
18
+ Jiffy::JsonOutputter.new out: out
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jiffy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Amundsen
@@ -47,15 +47,20 @@ executables:
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
+ - ".travis.yml"
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
53
  - LICENSE
53
54
  - README.md
55
+ - Rakefile
54
56
  - bin/jiffy
55
57
  - jiffy.gemspec
56
58
  - lib/jiffy.rb
57
59
  - lib/jiffy/array_mimicking_io.rb
58
- - lib/jiffy/json_outputter.rb
60
+ - lib/jiffy/outputter.rb
61
+ - lib/jiffy/outputters/json.rb
62
+ - lib/jiffy/outputters/ruby.rb
63
+ - lib/jiffy/parser.rb
59
64
  - lib/jiffy/parsers/json.rb
60
65
  - lib/jiffy/parsers/json.rl
61
66
  - lib/jiffy/parsers/json_array.rb
@@ -71,64 +76,8 @@ files:
71
76
  - lib/jiffy/parsers/json_value.rl
72
77
  - lib/jiffy/version.rb
73
78
  - test/jiffy_test.rb
74
- - test/negative-examples/hexadecimal.json
75
- - test/negative-examples/infinity-value.json
76
- - test/negative-examples/leading-comma.json
77
- - test/negative-examples/leading-zero.json
78
- - test/negative-examples/line-break.json
79
- - test/negative-examples/missing-colon.json
80
- - test/negative-examples/nan-value,json
81
- - test/negative-examples/positive-float.json
82
- - test/negative-examples/positive-integer.json
83
- - test/negative-examples/single-quote.json
84
- - test/negative-examples/string-as-root.json
85
- - test/negative-examples/tab-character.json
86
- - test/negative-examples/trailing-array-seperator.json
87
- - test/negative-examples/trailing-object-seperator.json
88
- - test/negative-examples/true-as-root.json
89
- - test/negative-examples/unclosed-array.json
90
- - test/negative-examples/unclosed-object.json
91
- - test/positive-examples/array-as-root.json
92
- - test/positive-examples/array-nested-inside-array.json
93
- - test/positive-examples/array-nested-inside-object.json
94
- - test/positive-examples/false-value.json
95
- - test/positive-examples/null-value.json
96
- - test/positive-examples/number-1.json
97
- - test/positive-examples/number-10.json
98
- - test/positive-examples/number-11.json
99
- - test/positive-examples/number-12.json
100
- - test/positive-examples/number-13.json
101
- - test/positive-examples/number-14.json
102
- - test/positive-examples/number-15.json
103
- - test/positive-examples/number-16.json
104
- - test/positive-examples/number-17.json
105
- - test/positive-examples/number-18.json
106
- - test/positive-examples/number-19.json
107
- - test/positive-examples/number-2.json
108
- - test/positive-examples/number-20.json
109
- - test/positive-examples/number-3.json
110
- - test/positive-examples/number-4.json
111
- - test/positive-examples/number-5.json
112
- - test/positive-examples/number-6.json
113
- - test/positive-examples/number-7.json
114
- - test/positive-examples/number-8.json
115
- - test/positive-examples/number-9.json
116
- - test/positive-examples/object-as-root.json
117
- - test/positive-examples/object-nested-inside-array.json
118
- - test/positive-examples/object-nested-inside-object.json
119
- - test/positive-examples/seperated-array-values.json
120
- - test/positive-examples/seperated-object-properties.json
121
- - test/positive-examples/string-backspace.json
122
- - test/positive-examples/string-carriage-return.json
123
- - test/positive-examples/string-formfeed.json
124
- - test/positive-examples/string-horizontal-tab.json
125
- - test/positive-examples/string-newline.json
126
- - test/positive-examples/string-quotation.json
127
- - test/positive-examples/string-reverse-solidus.json
128
- - test/positive-examples/string-solidus.json
129
- - test/positive-examples/string-trivial.json
130
- - test/positive-examples/string-unicode.json
131
- - test/positive-examples/true-value.json
79
+ - test/outputter_test.rb
80
+ - test/outputters/json_test.rb
132
81
  homepage: https://github.com/badeball/jiffy
133
82
  licenses:
134
83
  - MIT
@@ -1,62 +0,0 @@
1
- class Jiffy
2
- class JsonOutputter
3
- attr_accessor :out, :indent
4
-
5
- def initialize(options)
6
- self.out = options[:out] || $stdout
7
-
8
- raise ArgumentError, 'Invalid output source' unless out.respond_to? :print
9
-
10
- self.indent = 0
11
- end
12
-
13
- def process_token(token, payload = nil)
14
- case token
15
- when :begin_object
16
- output '{', indent: 1, newline_after: true
17
- when :end_object
18
- output '}', indent: -1, newline_before: true
19
- when :begin_array
20
- output '[', indent: 1, newline_after: true
21
- when :end_array
22
- output ']', indent: -1, newline_before: true
23
- when :value_separator
24
- output ',', newline_after: true
25
- when :name_separator
26
- output ': '
27
- when :begin_string, :end_string
28
- output '"'
29
- when :null
30
- output 'null'
31
- when :nan
32
- output 'NaN'
33
- when :inf
34
- output 'Infinity'
35
- when :true
36
- output 'true'
37
- when :false
38
- output 'false'
39
- when :char
40
- output payload
41
- when :number
42
- output payload.to_s
43
- else
44
- puts "Unknown token: #{token.inspect}"
45
- end
46
- end
47
-
48
- alias_method :t, :process_token
49
-
50
- def output(content, options = {})
51
- self.indent += options[:indent] if options[:indent]
52
-
53
- if options[:newline_before]
54
- out.print "\n" << ' ' * self.indent
55
- end
56
-
57
- out.print content
58
-
59
- out.print "\n" << ' ' * self.indent if options[:newline_after]
60
- end
61
- end
62
- end
@@ -1,3 +0,0 @@
1
- [
2
- 0x00
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- Infinity
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- ,""
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- 01
3
- ]
@@ -1,4 +0,0 @@
1
- [
2
- "line
3
- break"
4
- ]
@@ -1,3 +0,0 @@
1
- {
2
- "" ""
3
- }
@@ -1,3 +0,0 @@
1
- [
2
- NaN
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- +1.123
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- +100
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- 'single quoted string'
3
- ]
@@ -1 +0,0 @@
1
- ""
@@ -1,3 +0,0 @@
1
- [
2
- 'tab character'
3
- ]
@@ -1,3 +0,0 @@
1
- [
2
- true,
3
- ]
@@ -1,3 +0,0 @@
1
- {
2
- "": "",
3
- }
@@ -1 +0,0 @@
1
- true
@@ -1,2 +0,0 @@
1
- [
2
- true
@@ -1,2 +0,0 @@
1
- {
2
- "": ""
@@ -1,3 +0,0 @@
1
- [
2
-
3
- ]
@@ -1,5 +0,0 @@
1
- {
2
- "": [
3
-
4
- ]
5
- }
@@ -1,3 +0,0 @@
1
- [
2
- false
3
- ]