Skip to content

Commit 792dc1c

Browse files
authored
Merge pull request #195 from scotje/maint_fixup_json_specs
(MAINT) Refactor and clean up JSON-related unit tests
2 parents a665166 + 4f36870 commit 792dc1c

File tree

19 files changed

+139
-1917
lines changed

19 files changed

+139
-1917
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ Gemfile.lock
4141

4242
## local coverage results
4343
/coverage/
44+
45+
## local rspec config
46+
/.rspec-local

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ group :test do
1818
gem 'puppetlabs_spec_helper'
1919
gem 'serverspec'
2020
gem 'simplecov-console'
21-
gem "rspec", "~> 3.1"
21+
gem 'rspec', '~> 3.1'
22+
gem 'json_spec', '~> 1.1', '>= 1.1.5'
2223
end
2324

2425
group :acceptance do

lib/puppet-strings/json.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,4 @@ def self.render(file = nil)
2626
puts JSON.pretty_generate(document)
2727
end
2828
end
29-
30-
# Converts a list of tags into an array of hashes.
31-
# @param [Array] tags List of tags to be converted into an array of hashes.
32-
# @return [Array] Returns an array of tag hashes.
33-
def self.tags_to_hashes(tags)
34-
# Skip over the API tags that are public
35-
tags.select { |t| (t.tag_name != 'api' || t.text != 'public') }.map do |t|
36-
next t.to_hash if t.respond_to?(:to_hash)
37-
38-
tag = { tag_name: t.tag_name }
39-
# grab nested information for @option tags
40-
if tag[:tag_name] == 'option'
41-
tag[:opt_name] = t.pair.name
42-
tag[:opt_text] = t.pair.text
43-
tag[:opt_types] = t.pair.types
44-
tag[:parent] = t.name
45-
end
46-
tag[:text] = t.text if t.text
47-
tag[:types] = t.types if t.types
48-
tag[:name] = t.name if t.name
49-
tag
50-
end
51-
end
52-
53-
# Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
54-
# @param [YARD::Docstring, String] docstring The docstring to convert to a hash.
55-
# @param [Array] select_tags List of tags to select. Other tags will be filtered out.
56-
# @return [Hash] Returns a hash representation of the given docstring.
57-
def self.docstring_to_hash(docstring, select_tags=nil)
58-
hash = {}
59-
hash[:text] = docstring
60-
if docstring.is_a? YARD::Docstring
61-
tags = tags_to_hashes(docstring.tags.select { |t| select_tags.nil? || select_tags.include?(t.tag_name.to_sym) })
62-
63-
hash[:tags] = tags unless tags.empty?
64-
end
65-
hash
66-
end
6729
end

lib/puppet-strings/yard/code_objects/class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def to_hash
5050
hash[:file] = file
5151
hash[:line] = line
5252
hash[:inherits] = statement.parent_class if statement.parent_class
53-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
53+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
5454
defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
5555
hash[:defaults] = defaults unless defaults.empty?
5656
hash[:source] = source unless source && source.empty?

lib/puppet-strings/yard/code_objects/defined_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def to_hash
4949
hash[:name] = name
5050
hash[:file] = file
5151
hash[:line] = line
52-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
52+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
5353
defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
5454
hash[:defaults] = defaults unless defaults.empty?
5555
hash[:source] = source unless source && source.empty?

lib/puppet-strings/yard/code_objects/function.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ def to_hash
8888
if self.has_tag? :overload
8989
# loop over overloads and append onto the signatures array
9090
self.tags(:overload).each do |o|
91-
hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Json.docstring_to_hash(o.docstring, [:param, :option, :return, :example]) }
91+
hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, [:param, :option, :return, :example]) }
9292
end
9393
else
94-
hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Json.docstring_to_hash(docstring, [:param, :option, :return, :example]) }
94+
hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(docstring, [:param, :option, :return, :example]) }
9595
end
9696

97-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
97+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
9898
defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
9999
hash[:defaults] = defaults unless defaults.empty?
100100
hash[:source] = source unless source && source.empty?

lib/puppet-strings/yard/code_objects/plan.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def to_hash
4747
hash[:name] = name
4848
hash[:file] = file
4949
hash[:line] = line
50-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
50+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
5151
defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
5252
hash[:defaults] = defaults unless defaults.empty?
5353
hash[:source] = source unless source && source.empty?

lib/puppet-strings/yard/code_objects/provider.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def to_hash
8282
hash[:type_name] = type_name
8383
hash[:file] = file
8484
hash[:line] = line
85-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
85+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
8686
hash[:confines] = confines if confines && !confines.empty?
8787
hash[:features] = features if features && !features.empty?
8888
hash[:defaults] = defaults if defaults && !defaults.empty?

lib/puppet-strings/yard/code_objects/type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def to_hash
141141
hash[:name] = name
142142
hash[:file] = file
143143
hash[:line] = line
144-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
144+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
145145
hash[:properties] = properties.map(&:to_hash) if properties && !properties.empty?
146146
hash[:parameters] = parameters.map(&:to_hash) if parameters && !parameters.empty?
147147
hash[:features] = features.map(&:to_hash) if features && !features.empty?

lib/puppet-strings/yard/parsers/json/parser.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def enumerator
2222
def parse
2323
begin
2424
json = JSON.parse(source)
25-
@statements.push(PuppetStrings::Yard::Parsers::JSON::TaskStatement.new(json, @source, @file))
25+
# TODO: this should compare json to a Task metadata json-schema or perform some other hueristics
26+
# to determine what type of statement it represents
27+
@statements.push(PuppetStrings::Yard::Parsers::JSON::TaskStatement.new(json, @source, @file)) unless json.empty?
2628
rescue
2729
log.error "Failed to parse #{@file}: "
2830
@statements = []

lib/puppet-strings/yard/tags/overload_tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def to_hash
9999
hash[:tag_name] = tag_name
100100
hash[:text] = text if text
101101
hash[:signature] = signature
102-
hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring) if !docstring.blank?
102+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring) if !docstring.blank?
103103
defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
104104
hash[:defaults] = defaults unless defaults.empty?
105105
hash[:types] = types if types

lib/puppet-strings/yard/util.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,52 @@ def self.github_to_yard_links(data)
2828
end
2929
data
3030
end
31+
32+
# Converts a list of tags into an array of hashes.
33+
# @param [Array] tags List of tags to be converted into an array of hashes.
34+
# @return [Array] Returns an array of tag hashes.
35+
def self.tags_to_hashes(tags)
36+
# Skip over the API tags that are public
37+
tags.select { |t| (t.tag_name != 'api' || t.text != 'public') }.map do |t|
38+
next t.to_hash if t.respond_to?(:to_hash)
39+
40+
tag = { tag_name: t.tag_name }
41+
# grab nested information for @option tags
42+
if tag[:tag_name] == 'option'
43+
tag[:opt_name] = t.pair.name
44+
tag[:opt_text] = t.pair.text
45+
tag[:opt_types] = t.pair.types
46+
tag[:parent] = t.name
47+
end
48+
tag[:text] = t.text if t.text
49+
tag[:types] = t.types if t.types
50+
tag[:name] = t.name if t.name
51+
tag
52+
end
53+
end
54+
55+
# Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
56+
# @param [YARD::Docstring, String] docstring The docstring to convert to a hash.
57+
# @param [Array] select_tags List of tags to select. Other tags will be filtered out.
58+
# @return [Hash] Returns a hash representation of the given docstring.
59+
def self.docstring_to_hash(docstring, select_tags=nil)
60+
hash = {}
61+
hash[:text] = docstring
62+
63+
if docstring.is_a? YARD::Docstring
64+
tags = tags_to_hashes(docstring.tags.select { |t| select_tags.nil? || select_tags.include?(t.tag_name.to_sym) })
65+
66+
unless tags.empty?
67+
hash[:tags] = tags
68+
# .sort_by do |tag|
69+
# sort_key = tag[:tag_name].dup
70+
# sort_key << "-#{tag[:name]}" if tag[:name]
71+
# sort_key << "-#{tag[:opt_name]}" if tag[:opt_name]
72+
# sort_key
73+
# end
74+
end
75+
end
76+
77+
hash
78+
end
3179
end

0 commit comments

Comments
 (0)