cucumber-messages 16.0.1 → 17.1.1

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: 5dc7353136f14ca646f8548de717c06e42a44fe7fed631fbfc56be51b5c82b7f
4
- data.tar.gz: c654cce5e60eb55e7099af06d20ef3dae13f041fba5168f31a2cd53e437b42cd
3
+ metadata.gz: '0845ecc513db604a2e3e94faeacfc5be6dc389772cbc589f654396dbd6ebfc6d'
4
+ data.tar.gz: 6de9d6ee8b182754da2cbf05da993e14ecffe3616058ee2e5760d203eb3b2392
5
5
  SHA512:
6
- metadata.gz: 9319a4c524c81b9ae89d5d0972015bb44b38039d54f4b7bc6a2387a90c2db6c200c8ff02e15a048d88ee117569c004992ff784ad2379241b22b4cde05d3f3c12
7
- data.tar.gz: 3109430754f68352da28323be284797bd9f7a5e131cec07e0e0034f2cf1fd00c23cf1dbe8d93ee07024eafeace3dc3ff0553a7f6cce951749d94d35152e6f26a
6
+ metadata.gz: 73e9d3f8e508a2737fa2bb291e868e5cedb4156f464407f503fb4fb3361ccc35136d7c2abb68465242d8ea15a14f1f9815675382a5c83d50f75ad2d286f769ba
7
+ data.tar.gz: a9609a5f6b91f13d2d00a8efe0a24bdee9f4a7cad2b5ba457001339955c698c267318758b9f8276e1b4dded1fd9a517b095d28e2801e5e9ac060b5c571bd6c01
data/README.md CHANGED
@@ -1,2 +1,2 @@
1
- # Cucumber Messages for Ruby (Protocol Buffers)
1
+ # Cucumber Messages for Ruby (JSON schema)
2
2
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 16.0.1
1
+ 17.1.1
@@ -0,0 +1,39 @@
1
+ require 'cucumber/messages/message/utils'
2
+ require 'json'
3
+
4
+ module Cucumber
5
+ module Messages
6
+ class Message
7
+ include Cucumber::Messages::Message::Utils
8
+
9
+ module Deserialization
10
+ def self.included(other)
11
+ other.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ ##
17
+ # Returns a new Message - or messages into an array - deserialized from the given json document.
18
+ # CamelCased keys are properly converted to snake_cased attributes in the process
19
+ #
20
+ # Cucumber::Messages::Duration.from_json('{"seconds":1,"nanos":42}') # => #<Cucumber::Messages::Duration:0x00007efda134c290 @seconds=1, @nanos=42>
21
+ # Cucumber::Messages::PickleTag.from_json('{"name":"foo","astNodeId":"abc-def"}') # => #<Cucumber::Messages::PickleTag:0x00007efda138cdb8 @name="foo", @ast_node_id="abc-def">
22
+ #
23
+ # It is recursive so embedded messages are also processed.
24
+ #
25
+ # json_string = { location: { line: 2 }, text: "comment" }.to_json
26
+ # Cucumber::Messages::Comment.from_json(json_string) # => #<Cucumber::Messages::Comment:0x00007efda6abf888 @location=#<Cucumber::Messages::Location:0x00007efda6abf978 @line=2, @column=nil>, @text="comment">
27
+ #
28
+ # json_string = { uri: 'file:///...', comments: [{text: 'text comment'}, {text: 'another comment'}]}.to_json
29
+ # Cucumber::Messages::GherkinDocument.from_json(json_string) # => #<Cucumber::Messages::GherkinDocument:0x00007efda11e6a90 ... @comments=[#<Cucumber::Messages::Comment:0x00007efda11e6e50 ..., #<Cucumber::Messages::Comment:0x00007efda11e6b58 ...>]>
30
+ #
31
+
32
+ def from_json(json_string)
33
+ from_h(JSON.parse(json_string, { symbolize_names: true }))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,73 @@
1
+ require 'cucumber/messages/message/utils'
2
+ require 'json'
3
+
4
+ module Cucumber
5
+ module Messages
6
+ class Message
7
+ include Cucumber::Messages::Message::Utils
8
+
9
+ module Serialization
10
+
11
+ ##
12
+ # Returns a new Hash formed from the message attributes
13
+ # If +camelize:+ keyword parameter is set to true, then keys will be camelized
14
+ # If +reject_nil_values:+ keyword parameter is set to true, resulting hash won't include nil values
15
+ #
16
+ # Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_h # => { seconds: 1, nanos: 42 }
17
+ # Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_h(camelize: true) # => { name: 'foo', astNodeId: 'abc-def' }
18
+ # Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_h(reject_nil_values: true) # => { name: 'foo' }
19
+ #
20
+ # It is recursive so embedded messages are also processed
21
+ #
22
+ # location = Cucumber::Messages::Location.new(line: 2)
23
+ # Cucumber::Messages::Comment.new(location: location, text: 'comment').to_h # => { location: { line: 2, :column: nil }, text: "comment" }
24
+ #
25
+
26
+ def to_h(camelize: false, reject_nil_values: false)
27
+ resulting_hash = self.instance_variables.map do |variable_name|
28
+ h_key = variable_name[1..-1]
29
+ h_key = Cucumber::Messages::Message.camelize(h_key) if camelize
30
+
31
+ h_value = prepare_value(
32
+ self.instance_variable_get(variable_name),
33
+ camelize: camelize,
34
+ reject_nil_values: reject_nil_values
35
+ )
36
+
37
+ [ h_key.to_sym, h_value ]
38
+ end.to_h
39
+
40
+ resulting_hash.reject! { |_, value| value.nil? } if reject_nil_values
41
+ resulting_hash
42
+ end
43
+
44
+ ##
45
+ # Generates a JSON document from the message.
46
+ # Keys are camelized during the process. Null values are not part of the json document.
47
+ #
48
+ # Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_json # => '{"seconds":1,"nanos":42}'
49
+ # Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_json # => '{"name":"foo","astNodeId":"abc-def"}'
50
+ # Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_json # => '{"name":"foo"}'
51
+ #
52
+ # As #to_h, the method is recursive
53
+ #
54
+ # location = Cucumber::Messages::Location.new(line: 2)
55
+ # Cucumber::Messages::Comment.new(location: location, text: 'comment').to_json # => '{"location":{"line":2,"column":null},"text":"comment"}'
56
+ #
57
+
58
+ def to_json
59
+ to_h(camelize: true, reject_nil_values: true).to_json
60
+ end
61
+
62
+ private
63
+
64
+ def prepare_value(value, camelize:, reject_nil_values:)
65
+ return value.to_h(camelize: camelize, reject_nil_values: reject_nil_values) if value.is_a?(Cucumber::Messages::Message)
66
+ return value.map { |v| prepare_value(v, camelize: camelize, reject_nil_values: reject_nil_values) } if value.is_a?(Array)
67
+
68
+ value
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,46 @@
1
+ module Cucumber
2
+ module Messages
3
+ class Message
4
+ module Utils
5
+ def self.included(other)
6
+ other.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ ##
12
+ # Makes an underscored, lowercase form from the expression in the string.
13
+ #
14
+ # underscore('GherkinDocument') # => "gherkin_document"
15
+ #
16
+ # This is a simplified version of the Ruby on Rails implementation
17
+ # https://github.com/rails/rails/blob/v6.1.3.2/activesupport/lib/active_support/inflector/methods.rb#L92
18
+
19
+ def underscore(term)
20
+ return term unless /[A-Z-]/.match?(term)
21
+
22
+ word = term.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
23
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
24
+ word.tr!("-", "_")
25
+ word.downcase!
26
+ word
27
+ end
28
+
29
+ ##
30
+ # Converts strings to UpperCamelCase.
31
+ #
32
+ # camelize('gherkin_document') # => "GherkinDocument"
33
+ #
34
+ # This is a simplified version of the Ruby on Rails implementation
35
+ # https://github.com/rails/rails/blob/v6.1.3.2/activesupport/lib/active_support/inflector/methods.rb#L69
36
+
37
+ def camelize(term)
38
+ camelized = term.to_s
39
+ camelized.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
40
+ camelized
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ require 'cucumber/messages/message/deserialization'
2
+ require 'cucumber/messages/message/serialization'
3
+
4
+ module Cucumber
5
+ module Messages
6
+ class Message
7
+ include Cucumber::Messages::Message::Deserialization
8
+ include Cucumber::Messages::Message::Serialization
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,4 @@
1
- require 'json'
1
+ require 'cucumber/messages.deserializers'
2
2
 
3
3
  module Cucumber
4
4
  module Messages
@@ -8,7 +8,7 @@ module Cucumber
8
8
  io.each_line do |line|
9
9
  next if line.strip.empty?
10
10
  begin
11
- m = JSON.parse(line)
11
+ m = Envelope.from_json(line)
12
12
  rescue => e
13
13
  raise "Not JSON: #{line.strip}"
14
14
  end
@@ -19,7 +19,7 @@ module Cucumber
19
19
  nanos = second_modulus * NANOSECONDS_PER_SECOND
20
20
  {
21
21
  'seconds' => seconds,
22
- 'nanos' => nanos
22
+ 'nanos' => nanos.to_i
23
23
  }
24
24
  end
25
25