junklet 1.0.2 → 1.0.3

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Zjc3YzlmNzVjYTc3YmZhNTIwZjZlNTE4Y2I1ZTQ2YWM0ZmNmODk3ZQ==
4
+ MTJlN2QwZTEyOGU1OThhMzBmYzMwY2RhYTAzM2E1NzQzNWJhNGM4MQ==
5
5
  data.tar.gz: !binary |-
6
- NTI1YTRjZWJkMmFiMmI3ZWNhOGM2NTBhNDRkZmNjYWZiMzJjNGM4ZQ==
6
+ MjA3MDU1MDhlNzE4YmI2NGJlN2RmYmZjNGU2ZDIxMjIzMmQzMDIxNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGM0ZDRjYTkxYjdhNTdlNzA5YTM3MjU5MGEyZDk3ZWQwYTQ0YjU5NjczMGE2
10
- ZGU0NmIxNjk4ZDc1Yzg5NDAxOTU2NTM4NmJlNTE3NjIwMmM4NWZhMDM2MTNk
11
- OTFmN2FiNjAwMThiYTBlNTFkYTM3M2I4NjE5NDQzMDZiZjIxZjk=
9
+ MTg2NTc3MzJmNTBiYmM4OGI4NTZjZGU0MDA3MDgzNjg1NWY4ZDY5MDlmMGQ5
10
+ Mjc0MGRjYjY0ZWE2N2E1M2ZiM2E1MmVhOTBlYjk3NjdhZWQ4NGQ0YTk5YzQw
11
+ MDBjZDQxYmI0YzE2NGVkN2RiMmNlY2YwMTFkY2JkMzJmZmU1MGY=
12
12
  data.tar.gz: !binary |-
13
- OWIxNDM5ZGUyOWRhODg1YmM5NDFiMWZhMjNjNGY0MTRlZGQ1ZDNmY2JhNGJh
14
- Nzk5MzVlZTk4NDJhYWIwM2E0ZDUyNmI1NzEyZGQ2NGIyYjM0M2VhOWM5YTAx
15
- MmJiNTg0MGY3ZDY2NGNlNjJlYTU4NmViMmM2OWY5ZmNjZjlhYmU=
13
+ OWNhZDNlNjQ0NmZlNDdmNDg1N2U3YWZlOWMzMjIyNmUzZGY5ZGM0NDJhOWIx
14
+ OWZiYzk4ZDRjYmRkNTNlZTg0YjM5YmQxZGFlMDgwMGI5N2ZmODZkNGFmODI3
15
+ YmE5MTZhY2E5ZTAyMjc3NmZlNzYzYWExZDcwOTcwYWEzOTI5NGU=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- junklet (1.0.2)
4
+ junklet (1.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -135,6 +135,14 @@ junk(:int) # returns a random, positive ruby Fixnum between 0 and
135
135
  junk(:int, min: 5, max: 9) # returns a number from 5 to 9
136
136
  junk(:int, max: 1) # returns 0 or 1
137
137
 
138
+ # If you just know how many digits you need, use size:
139
+
140
+ junk(:int, size: 3) # generates number between 100 and 999
141
+
142
+ # min and max can further constrain size (but not expand it):
143
+ junk(:int, size: 3, min: 425) # number from 425 to 999
144
+ junk(:int, size: 3, max: 425) # number from 100 to 425
145
+
138
146
  junk(:bool) # returns true or false
139
147
 
140
148
  junk([:a, :b, :c]) # samples from the Array
data/lib/junklet.rb CHANGED
@@ -78,11 +78,23 @@ module RSpec
78
78
  # available here?
79
79
  case type
80
80
  when :int
81
- # Fun fact: you can get back an arbitrarily large number
82
- # by specifying a max value >= 2**62 so that Ruby promotes
83
- # it to a BigNum.
84
- min = opts[:min] || 0
85
- max = (opts[:max] || 2**62-2) + 1
81
+ # min,max cooperate with size to further constrain it. So
82
+ # size: 2, min: 30 would be min 30, max 99.
83
+ if opts[:size]
84
+ sized_min = 10**(opts[:size]-1)
85
+ sized_max = 10**opts[:size]-1
86
+ end
87
+ explicit_min = opts[:min] || 0
88
+ explicit_max = (opts[:max] || 2**62-2) + 1
89
+
90
+ if sized_min
91
+ min = [sized_min, explicit_min].max
92
+ max = [sized_max, explicit_max].min
93
+ else
94
+ min = sized_min || explicit_min
95
+ max = sized_max || explicit_max
96
+ end
97
+
86
98
  min,max = max,min if min>max
87
99
 
88
100
  generator = -> { rand(max-min) + min }
@@ -1,3 +1,3 @@
1
1
  module Junklet
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -111,6 +111,11 @@ describe Junklet do
111
111
  specify { expect([0,1]).to include(coin) }
112
112
  end
113
113
 
114
+ context "with size" do
115
+ let(:digit) { junk :int, size: 1 }
116
+ specify { expect(digit).to be < 10 }
117
+ end
118
+
114
119
  context "with exclude proc" do
115
120
  let(:junk_evens) { junk :int, min: 0, max: 10, exclude: ->(x) { x % 2 == 1 } }
116
121
  specify { expect(junk_evens % 2).to eq(0) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: junklet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Brady
@@ -97,7 +97,6 @@ files:
97
97
  - junklet.gemspec
98
98
  - lib/junklet.rb
99
99
  - lib/junklet/version.rb
100
- - lib/junklet_upgrade_parser.rb
101
100
  - lib/line.rb
102
101
  - spec/fixtures/block1_after.txt
103
102
  - spec/fixtures/block1_before.txt
@@ -110,7 +109,6 @@ files:
110
109
  - spec/fixtures/old_skool_after.txt
111
110
  - spec/fixtures/old_skool_before.txt
112
111
  - spec/lib/junklet_spec.rb
113
- - spec/lib/junklet_upgrade_parser_spec.rb
114
112
  - spec/lib/line_spec.rb
115
113
  - spec/spec_helper.rb
116
114
  homepage: ''
@@ -149,6 +147,5 @@ test_files:
149
147
  - spec/fixtures/old_skool_after.txt
150
148
  - spec/fixtures/old_skool_before.txt
151
149
  - spec/lib/junklet_spec.rb
152
- - spec/lib/junklet_upgrade_parser_spec.rb
153
150
  - spec/lib/line_spec.rb
154
151
  - spec/spec_helper.rb
@@ -1,151 +0,0 @@
1
- # What it does:
2
- # Find blocks of mixed lets and "junklets", a special DSL term we use here for
3
- # junk data. It then rewrites the block so that the lets come first in
4
- # original order, then a newline, then the junklets in order. So it turns this
5
- # code:
6
- #
7
- # let(:name) { "Bob" }
8
- # junklet :address
9
- # let(:city) { "Faketown" }
10
- # junklet :state
11
- # junklet :zip
12
- # let(:extra) { true }
13
- # let(:pants) { ON_FIRE }
14
- #
15
- # Into THIS code, leaving the rest of the file unaltered:
16
- #
17
- # let(:name) { "Bob" }
18
- # let(:city) { "Faketown" }
19
- # let(:extra) { true }
20
- # let(:pants) { ON_FIRE }
21
- #
22
- # junklet :address
23
- # junklet :state
24
- # junklet :zip
25
- #
26
-
27
- require_relative 'line'
28
-
29
- # As with all parsers, this is easy if we allow peeking.
30
- class JunkletUpgradeParser
31
- attr_reader :lines, :lets, :junklets, :mode, :current_line
32
-
33
- INACTIVE = 'inactive'
34
- MAYBE_ACTIVE = 'maybe_active'
35
- ACTIVE = 'active'
36
-
37
- def initialize
38
- end
39
-
40
- def inactive; INACTIVE; end
41
- def maybe_active; MAYBE_ACTIVE; end
42
- def active; ACTIVE; end
43
-
44
- def inactive?; mode == inactive; end
45
- def maybe_active?; mode == maybe_active; end
46
- def active?; mode == active; end
47
-
48
- def upgraded_lines(lines)
49
- @lines = lines
50
- return unless junky?
51
- we_are_inactive!
52
- emitted_lines = []
53
- lines.each do |line|
54
- self.current_line = imblart_line(line)
55
- case mode
56
- when inactive
57
- if current_line.let?
58
- lets << parse_line(current_line)
59
- we_are_maybe_active!
60
- elsif current_line.junklet?
61
- junklets << current_line
62
- we_are_active!
63
- elsif current_line.code?
64
- emitted_lines << parse_line(current_line)
65
- end
66
- when maybe_active
67
- if current_line.let?
68
- lets << parse_line(current_line)
69
- elsif current_line.junklet?
70
- junklets << current_line
71
- we_are_active!
72
- elsif current_line.code?
73
- emitted_lines += lets
74
- emitted_lines << parse_line(current_line)
75
- we_are_inactive!
76
- end
77
- when active
78
- if current_line.let?
79
- lets << parse_line(current_line)
80
- elsif current_line.junklet?
81
- junklets << current_line
82
- elsif current_line.code?
83
- emitted_lines += reordered_block
84
- emitted_lines << parse_line(current_line)
85
- reset
86
- we_are_inactive!
87
- end
88
- end
89
- end
90
-
91
- # if lets || junklets we've hit EOF while active
92
- emitted_lines += reordered_block if active?
93
- emitted_lines
94
- end
95
-
96
- def parse_line(line)
97
- numeric_range_match = /(\s*)SecureRandom.(hex|uuid)\[(\d+)\.\.(\d+)\](\s*)/
98
- range_match = /(\s*)SecureRandom.(hex|uuid)\[(\d+)\.\.(.+?)\](\s*)/
99
- simple_match = /(\s*)SecureRandom.(hex|uuid)(\s*)/
100
-
101
- # match these in order--simple match will match all complicated matches.
102
- line \
103
- .gsub(numeric_range_match) {|s| "#{$1}junk(#{$4.to_i-$3.to_i})#{$5}" } \
104
- .gsub(range_match) {|s| "#{$1}junk(#{$4})#{$5}" } \
105
- .gsub(simple_match) {|s| "#{$1}junk#{$3}" }
106
- end
107
-
108
- def reordered_block
109
- lets << "\n" unless lets.empty? || lets.last == "\n" || junklets.empty?
110
- lets + sort_junklets
111
- end
112
-
113
- def sort_junklets
114
- return if junklets.empty?
115
- indent = junklets.first.indent
116
- ["#{indent}junklet #{(junklets.map(&:names).sort * ', ')}\n"]
117
- end
118
-
119
- def imblart_line(line)
120
- Line.new line
121
- end
122
-
123
- def we_are_inactive!
124
- reset
125
- @mode = INACTIVE
126
- end
127
-
128
- def we_are_maybe_active!
129
- @mode = MAYBE_ACTIVE
130
- end
131
-
132
- def we_are_active!
133
- @mode = ACTIVE
134
- end
135
-
136
- def reset
137
- @lets, @junklets = [], []
138
- end
139
-
140
- def junky?
141
- lines.any? { |line| junk_line? line }
142
- end
143
-
144
- def junk_line?(line)
145
- line =~ /\bSecureRandom\./ || line =~ /\bjunklet\b/
146
- end
147
-
148
- private
149
-
150
- attr_accessor :current_line
151
- end
@@ -1,99 +0,0 @@
1
- require 'spec_helper'
2
- require_relative '../../lib/junklet_upgrade_parser'
3
-
4
-
5
- describe JunkletUpgradeParser do
6
- specify { expect(JunkletUpgradeParser).to be }
7
-
8
- let(:parser) { JunkletUpgradeParser.new }
9
-
10
- # Process test files
11
- context "with test fixtures" do
12
- # TODO: Mothballing upgrader for now. Embedded gives a good
13
- # example of how the parser is fascinatingly broken.
14
- #%w(block1 mixed_code combined old_skool embedded).each do |name|
15
- %w(block1 mixed_code combined old_skool).each do |name|
16
- describe "processing #{name}_before.txt -> #{name}_after.txt" do
17
- let(:fixture_path) { File.expand_path(File.join(File.dirname(__FILE__), '../fixtures')) }
18
- let(:lines) { IO.readlines("#{fixture_path}/#{name}_before.txt") }
19
- let(:output_file) { "#{fixture_path}/#{name}_after.txt" }
20
- let(:output_lines) { IO.readlines(output_file) }
21
-
22
- specify { expect(parser.upgraded_lines(lines)).to eq(output_lines) }
23
- specify { expect(parser.upgraded_lines(lines) * '').to eq(File.read(output_file)) }
24
-
25
- # This dumps a purty colorized diff of what we expected to get
26
- # on the left against what we actually got on the right. It's
27
- # a very helpful visual aid when the spec is acting up. It's a
28
- # fork in the eyeballs* if you just want to see green dots.
29
- #
30
- # * two forks, if you're into async
31
- # it "dumps the file diff (no-op spec)" do
32
- # parsed_lines = parser.upgraded_lines(lines)
33
-
34
- # puts '-' * 80
35
- # dump_diff(output_lines, parser.upgraded_lines(lines))
36
- # puts '-' * 80
37
- # puts parser.inspect
38
- # end
39
- end
40
- end
41
- end
42
-
43
- # TODO: replace this with a hash of line => replacement pairs and a single spec
44
- describe '#parse_line' do
45
- context 'when line contains no SecureRandom.uuid or SecureRandom.hex calls' do
46
- let(:line) { SecureRandom.hex(100) }
47
- specify { expect(subject.parse_line(line)).to eq(line) }
48
- end
49
-
50
- # context "when line contains SecureRandom.hex(int)" do
51
- # let(:line) { ' emit_noise("#{SecureRandom.hex(12)")' }
52
- # it { pending "write me you clods" }
53
- # end
54
-
55
- # context "when line contains SecureRandom.hex int" do
56
- # it { pending "write me you clods" }
57
- # end
58
-
59
- [:hex, :uuid].each do |method|
60
- context "when line contains SecureRandom.#{method}" do
61
- let(:line) { " let(:extra_fields) { Array.new(2) { SecureRandom.#{method} } }" }
62
- it "replaces SecureRandom.#{method} with junk" do
63
- expect(parser.parse_line(line)).to eq(' let(:extra_fields) { Array.new(2) { junk } }')
64
- end
65
- end
66
-
67
- context "when extra whitespace is present around SecureRandom.#{method}" do
68
- let(:line) { " let(:extra_fields) { Array.new(2) { SecureRandom.#{method} } }" }
69
- it "replaces SecureRandom.#{method} with junk" do
70
- expect(parser.parse_line(line)).to eq(' let(:extra_fields) { Array.new(2) { junk } }')
71
- end
72
- end
73
-
74
- context "when line contains SecureRandom.#{method}[range]" do
75
- context "and range is [int..int] (e.g. [0..10])" do
76
- let(:line) { "question_id: SecureRandom.#{method}[0..10]" }
77
- it "replaces SecureRandom.#{method}[0..10] with junk(10)" do
78
- expect(parser.parse_line(line)).to eq('question_id: junk(10)')
79
- end
80
- end
81
-
82
- context "and range is of form [n..int]" do
83
- let(:line) { "question_id: SecureRandom.#{method}[3..10]" }
84
- it "replaces SecureRandom.#{method}[0..10] with junk(7)" do
85
- expect(parser.parse_line(line)).to eq('question_id: junk(7)')
86
- end
87
- end
88
-
89
- context "and range is [0..var] (e.g. [0..MAX_QUESTION_ID_LEN-1]" do
90
- let(:line) { "SecureRandom.#{method}[0..Mogrifier::NcpdpScriptStandard::MAX_QUESTION_ID_LEN-1]" }
91
- it "replaces SecureRandom.#{method}[0..n] with junk(n)" do
92
- expect(parser.parse_line(line)).to eq('junk(Mogrifier::NcpdpScriptStandard::MAX_QUESTION_ID_LEN-1)')
93
- end
94
- end
95
- # TODO: three-dot ranges not yet supported due to rarity.
96
- end
97
- end
98
- end
99
- end