nuggets 1.4.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +5 -5
  2. data/ChangeLog +14 -0
  3. data/README +4 -4
  4. data/lib/nuggets/array/format.rb +1 -1
  5. data/lib/nuggets/array/histogram_mixin.rb +4 -4
  6. data/lib/nuggets/file/sub_mixin.rb +8 -8
  7. data/lib/nuggets/hash/in_order.rb +2 -2
  8. data/lib/nuggets/hash/nest_mixin.rb +5 -9
  9. data/lib/nuggets/hash/unroll_mixin.rb +4 -4
  10. data/lib/nuggets/io/modes.rb +15 -15
  11. data/lib/nuggets/io/redirect_mixin.rb +2 -4
  12. data/lib/nuggets/json/canonical.rb +5 -0
  13. data/lib/nuggets/{io/null_mixin.rb → json/canonical_mixin.rb} +31 -12
  14. data/lib/nuggets/json/multi.rb +5 -0
  15. data/lib/nuggets/json/multi_mixin.rb +71 -0
  16. data/lib/nuggets/log_parser/rails.rb +18 -18
  17. data/lib/nuggets/string/format.rb +5 -0
  18. data/lib/nuggets/string/format_mixin.rb +59 -0
  19. data/lib/nuggets/string/highlight.rb +5 -0
  20. data/lib/nuggets/string/highlight_mixin.rb +62 -0
  21. data/lib/nuggets/version.rb +2 -2
  22. data/spec/nuggets/array/format_spec.rb +2 -1
  23. data/spec/nuggets/enumerable/minmax_spec.rb +2 -2
  24. data/spec/nuggets/env/set_spec.rb +2 -2
  25. data/spec/nuggets/env/user_home_spec.rb +1 -21
  26. data/spec/nuggets/hash/at_spec.rb +1 -1
  27. data/spec/nuggets/hash/deep_fetch_spec.rb +1 -1
  28. data/spec/nuggets/hash/deep_merge_spec.rb +16 -16
  29. data/spec/nuggets/hash/seen_spec.rb +1 -1
  30. data/spec/nuggets/hash/unroll_spec.rb +6 -18
  31. data/spec/nuggets/json/canonical_spec.rb +74 -0
  32. data/spec/nuggets/json/multi_spec.rb +67 -0
  33. data/spec/nuggets/object/blank_spec.rb +2 -2
  34. data/spec/nuggets/object/singleton_class_spec.rb +3 -2
  35. data/spec/nuggets/string/format_spec.rb +85 -0
  36. data/spec/nuggets/string/highlight_spec.rb +47 -0
  37. metadata +25 -16
  38. data/lib/nuggets/io/null.rb +0 -5
@@ -0,0 +1,62 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2016 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class String
29
+ module HighlightMixin
30
+
31
+ # call-seq:
32
+ # str.highlight(needle[, prefix[, postfix]]) => new_string
33
+ #
34
+ # Highlight occurrences of +needle+ (String(s) and/or Regexp(s))
35
+ # in _str_ by surrounding them with +prefix+ and +postfix+.
36
+ def highlight(needle, prefix = '|', postfix = prefix)
37
+ offsets = []
38
+
39
+ Array(needle).each { |arg|
40
+ while index = index(arg, index || 0)
41
+ offsets << [index, index += ($& || arg).length]
42
+ end
43
+ }
44
+
45
+ flattened = [current = offsets.sort!.shift]
46
+
47
+ offsets.each { |offset|
48
+ i1, j1 = current
49
+ i2, j2 = offset
50
+
51
+ i2 > j1 ? flattened << current = offset :
52
+ j2 > j1 ? current[-1] = j2 : nil
53
+ }
54
+
55
+ dup.tap { |result| flattened.reverse_each { |i, j|
56
+ result.insert(j, postfix).insert(i, prefix)
57
+ } }
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -3,8 +3,8 @@ module Nuggets
3
3
  module Version
4
4
 
5
5
  MAJOR = 1
6
- MINOR = 4
7
- TINY = 0
6
+ MINOR = 6
7
+ TINY = 1
8
8
 
9
9
  class << self
10
10
 
@@ -3,6 +3,7 @@ require 'nuggets/array/format'
3
3
  describe Array, 'format' do
4
4
 
5
5
  def self.test_format(*f)
6
+ return if RUBY_ENGINE == 'jruby' && JRUBY_VERSION.include?('pre')
6
7
  f.pop.each { |x, y| example { (f.dup % x).should == y } }
7
8
  end
8
9
 
@@ -30,7 +31,7 @@ describe Array, 'format' do
30
31
  ['', '', '' ] => ''
31
32
  )
32
33
 
33
- test_format({ :sep => ':' },
34
+ test_format({ sep: ':' },
34
35
  ['1', '2', '3', '4'] => '1:2:3:4',
35
36
  ['1', '2', '3', '' ] => '1:2:3',
36
37
  ['1', '2', '', '4'] => '1:2:4',
@@ -7,7 +7,7 @@ describe Enumerable, 'minmax' do
7
7
 
8
8
  e.max.should == 'quux'
9
9
  e.max_by(:length).should == 'quuux'
10
- e.max(:length).should == 5
10
+ e.max(:length).should == 5 if RUBY_VERSION < '2.4' && RUBY_ENGINE != 'jruby'
11
11
  }
12
12
 
13
13
  example {
@@ -15,7 +15,7 @@ describe Enumerable, 'minmax' do
15
15
 
16
16
  e.max.should == 222
17
17
  e.max_by(b).should == 45
18
- e.max(b).should == 5
18
+ e.max(b).should == 5 if RUBY_VERSION < '2.4' && RUBY_ENGINE != 'jruby'
19
19
  }
20
20
 
21
21
  end
@@ -12,12 +12,12 @@ describe_extended ENV, Nuggets::Env::SetMixin, true do
12
12
  end
13
13
 
14
14
  example do
15
- ENV.with(:lang => 'C') { ENV['LANG'].should == 'C' }
15
+ ENV.with(lang: 'C') { ENV['LANG'].should == 'C' }
16
16
  ENV['LANG'].should == @original['LANG']
17
17
  end
18
18
 
19
19
  example do
20
- ENV.set(:lang => 'C').to_hash.should == @original
20
+ ENV.set(lang: 'C').to_hash.should == @original
21
21
  ENV.to_hash.should == { 'LANG' => 'C' }
22
22
  end
23
23
 
@@ -7,36 +7,16 @@ describe_extended ENV, Nuggets::Env::UserHomeMixin, true do
7
7
  end
8
8
 
9
9
  after do
10
- ENV.clear
11
- ENV.update(@old_env)
10
+ ENV.clear.update(@old_env)
12
11
  end
13
12
 
14
13
  example do
15
14
  ENV.user_home.should be_an_instance_of(String)
16
15
  end
17
16
 
18
- example do
19
- ENV.clear
20
- ENV.user_home.should be_an_instance_of(String)
21
- end
22
-
23
17
  example do
24
18
  ENV['HOME'] = 'foo'
25
19
  ENV.user_home.should == 'foo'
26
20
  end
27
21
 
28
- unless RUBY_ENGINE == 'jruby'
29
-
30
- example do
31
- ENV.clear
32
- ENV.user_home('bar').should == 'bar'
33
- end
34
-
35
- example do
36
- ENV.clear
37
- ENV.user_home(nil).should be_nil
38
- end
39
-
40
- end
41
-
42
22
  end
@@ -2,7 +2,7 @@ require 'nuggets/hash/at'
2
2
 
3
3
  describe Hash, 'at' do
4
4
 
5
- let(:h) { { :a => 1, 2 => 3, nil => nil, 'foo' => %w[b a r] } }
5
+ let(:h) { { a: 1, 2 => 3, nil => nil, 'foo' => %w[b a r] } }
6
6
 
7
7
  example { h.first.should == { a: 1 } }
8
8
 
@@ -54,7 +54,7 @@ describe_extended Hash, Nuggets::Hash::DeepFetchMixin do
54
54
 
55
55
  context do
56
56
 
57
- let(:hash) { { :foo => { :bar => { :baz => 42 } } } }
57
+ let(:hash) { { foo: { bar: { baz: 42 } } } }
58
58
 
59
59
  example do
60
60
  lambda { hash.deep_fetch('foo/bar/baz') }.should raise_error(KeyError)
@@ -3,58 +3,58 @@ require 'nuggets/hash/deep_merge'
3
3
  describe_extended Hash, Nuggets::Hash::DeepMergeMixin do
4
4
 
5
5
  example do
6
- { :a => 1 }.deep_merge(:a => 2).should == { :a => 2 }
6
+ { a: 1 }.deep_merge(a: 2).should == { a: 2 }
7
7
  end
8
8
 
9
9
  example do
10
- { :a => 1 }.deep_merge(:b => 2).should == { :a => 1, :b => 2 }
10
+ { a: 1 }.deep_merge(b: 2).should == { a: 1, b: 2 }
11
11
  end
12
12
 
13
13
  example do
14
- { :a => { :b => 1 } }.deep_merge(:b => 2).should == { :a => { :b => 1 }, :b => 2 }
14
+ { a: { b: 1 } }.deep_merge(b: 2).should == { a: { b: 1 }, b: 2 }
15
15
  end
16
16
 
17
17
  example do
18
- { :a => { :b => 1 } }.deep_merge(:a => { :b => 2 }).should == { :a => { :b => 2 } }
18
+ { a: { b: 1 } }.deep_merge(a: { b: 2 }).should == { a: { b: 2 } }
19
19
  end
20
20
 
21
21
  example do
22
- lambda { { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => 2 }) }.should raise_error(TypeError)
22
+ lambda { { a: { b: { c: 1 } } }.deep_merge(a: { b: 2 }) }.should raise_error(TypeError)
23
23
  end
24
24
 
25
25
  example do
26
- { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :c => 2 } }).should == { :a => { :b => { :c => 2 } } }
26
+ { a: { b: { c: 1 } } }.deep_merge(a: { b: { c: 2 } }).should == { a: { b: { c: 2 } } }
27
27
  end
28
28
 
29
29
  example do
30
- { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :d => 2 } }).should == { :a => { :b => { :c => 1, :d => 2 } } }
30
+ { a: { b: { c: 1 } } }.deep_merge(a: { b: { d: 2 } }).should == { a: { b: { c: 1, d: 2 } } }
31
31
  end
32
32
 
33
33
  example do
34
- { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 } }
34
+ { a: { b: { c: 1 } } }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 } }
35
35
  end
36
36
 
37
37
  example do
38
- { :a => { :b => { :c => 1 }, :d => 2 } }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 } }
38
+ { a: { b: { c: 1 }, d: 2 } }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 } }
39
39
  end
40
40
 
41
41
  example do
42
- { :a => { :b => { :c => 1 }, :d => 2 }, :e => 3 }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 }, :e => 3 }
42
+ { a: { b: { c: 1 }, d: 2 }, e: 3 }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 }, e: 3 }
43
43
  end
44
44
 
45
45
  example do
46
- { :a => { :b => { :c => 1 } }, :d => { :e => 3 } }.deep_merge(:a => { :b => { :c => 2 } }, :d => { :e => 4 }).should == { :a => { :b => { :c => 2 } }, :d => { :e => 4 } }
46
+ { a: { b: { c: 1 } }, d: { e: 3 } }.deep_merge(a: { b: { c: 2 } }, d: { e: 4 }).should == { a: { b: { c: 2 } }, d: { e: 4 } }
47
47
  end
48
48
 
49
49
  context do
50
50
 
51
51
  before :each do
52
- @sub_hash1 = { :b => 1 }
53
- @sub_hash2 = { :b => 2 }
52
+ @sub_hash1 = { b: 1 }
53
+ @sub_hash2 = { b: 2 }
54
54
 
55
- @hash1 = { :a => @sub_hash1 }
56
- @hash2 = { :a => @sub_hash2 }
57
- @hash3 = { :a => { :b => 2} }
55
+ @hash1 = { a: @sub_hash1 }
56
+ @hash2 = { a: @sub_hash2 }
57
+ @hash3 = { a: { b: 2} }
58
58
  end
59
59
 
60
60
  example do
@@ -25,7 +25,7 @@ describe_extended Hash, Nuggets::Hash::SeenMixin, true do
25
25
  end
26
26
 
27
27
  example do
28
- hash = Hash.seen.update(:a => :b)
28
+ hash = Hash.seen.update(a: :b)
29
29
  hash.should be_an_instance_of(Hash)
30
30
 
31
31
  hash[:a].should == :b
@@ -18,12 +18,12 @@ describe_extended Hash, Nuggets::Hash::UnrollMixin do
18
18
  end
19
19
 
20
20
  example do
21
- hash = { :a => { :b => 1 } }
21
+ hash = { a: { b: 1 } }
22
22
  hash.unroll.should == [[:a, 1]]
23
23
  end
24
24
 
25
25
  example do
26
- hash = { :a => { :b => 1, :c => 2 } }
26
+ hash = { a: { b: 1, c: 2 } }
27
27
 
28
28
  result = hash.unroll.first
29
29
  result.size.should == 3
@@ -33,36 +33,24 @@ describe_extended Hash, Nuggets::Hash::UnrollMixin do
33
33
  result.should include(2)
34
34
  end
35
35
 
36
- if RUBY_VERSION < '1.9'
37
- example do
38
- hash = { :a => { :b => 1, :c => 2 } }
39
- lambda { hash.unroll(:sort => true) }.should raise_error(NoMethodError, /<=>/)
40
- end
41
-
42
- example do
43
- hash = { :a => { :b => 1, :c => 2 } }
44
- lambda { hash.unroll(:sort_by => lambda { |h| h.to_s }) }.should_not raise_error(NoMethodError)
45
- end
46
- end
47
-
48
36
  example do
49
37
  hash = { 'a' => { 'b' => 1, 'c' => 2 } }
50
- hash.unroll(:sort => true).should == [['a', 1, 2]]
38
+ hash.unroll(sort: true).should == [['a', 1, 2]]
51
39
  end
52
40
 
53
41
  example do
54
42
  hash = { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } }
55
- hash.unroll('b', 'c', :sort => true).should == [['a', 1, 2], ['d', 0, 3]]
43
+ hash.unroll('b', 'c', sort: true).should == [['a', 1, 2], ['d', 0, 3]]
56
44
  end
57
45
 
58
46
  example do
59
47
  hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
60
- hash.unroll('b', :sort_by => lambda { |h| h.to_s }).should == [['z', 'a', 1], ['z', 'd', 0]]
48
+ hash.unroll('b', sort_by: lambda { |h| h.to_s }).should == [['z', 'a', 1], ['z', 'd', 0]]
61
49
  end
62
50
 
63
51
  example do
64
52
  hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
65
- hash.unroll(:sort => true) { |h| h['b'] = nil; h['c'] *= 2 }.should == [['z', 'a', nil, 4], ['z', 'd', nil, 6]]
53
+ hash.unroll(sort: true) { |h| h['b'] = nil; h['c'] *= 2 }.should == [['z', 'a', nil, 4], ['z', 'd', nil, 6]]
66
54
  end
67
55
 
68
56
  end
@@ -0,0 +1,74 @@
1
+ require 'nuggets/json/canonical'
2
+ require 'nuggets/json/multi'
3
+
4
+ describe_extended JSON, Nuggets::JSON::CanonicalMixin, true do
5
+
6
+ let(:source) { '{"a":1,"b":[42,23],"a":{"b":2,"a":[3,5,4]}}' }
7
+
8
+ subject { JSON.parse(source) }
9
+
10
+ describe 'plain' do
11
+
12
+ example { expect(JSON.generate(subject)).to eq('{"a":{"b":2,"a":[3,5,4]},"b":[42,23]}') }
13
+
14
+ example { expect(JSON.pretty_generate(subject)).to eq(<<-EOT.chomp) }
15
+ {
16
+ "a": {
17
+ "b": 2,
18
+ "a": [
19
+ 3,
20
+ 5,
21
+ 4
22
+ ]
23
+ },
24
+ "b": [
25
+ 42,
26
+ 23
27
+ ]
28
+ }
29
+ EOT
30
+
31
+ end
32
+
33
+ describe 'canonical' do
34
+
35
+ example { expect(JSON.generate_canonical(subject)).to eq('{"a":{"a":[3,4,5],"b":2},"b":[23,42]}') }
36
+
37
+ example { expect(JSON.pretty_generate_canonical(subject)).to eq(<<-EOT.chomp) }
38
+ {
39
+ "a": {
40
+ "a": [
41
+ 3,
42
+ 4,
43
+ 5
44
+ ],
45
+ "b": 2
46
+ },
47
+ "b": [
48
+ 23,
49
+ 42
50
+ ]
51
+ }
52
+ EOT
53
+
54
+ example { expect(JSON.pretty_print_canonical(source)).to eq(<<-EOT.chomp) }
55
+ {
56
+ "a": 1,
57
+ "a": {
58
+ "a": [
59
+ 3,
60
+ 4,
61
+ 5
62
+ ],
63
+ "b": 2
64
+ },
65
+ "b": [
66
+ 23,
67
+ 42
68
+ ]
69
+ }
70
+ EOT
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,67 @@
1
+ require 'nuggets/json/multi'
2
+
3
+ describe_extended JSON, Nuggets::JSON::MultiMixin, true do
4
+
5
+ let(:source) { '{"a":1,"b":[42,23],"a":{"b":2}}' }
6
+
7
+ describe 'plain' do
8
+
9
+ subject { JSON.parse(source) }
10
+
11
+ example { expect(subject).to be_a(Hash) }
12
+ example { expect(subject.size).to eq(2) }
13
+ example { expect(subject.keys).to eq(%w[a b]) }
14
+
15
+ example { expect(subject['a']).to eq('b' => 2) }
16
+ example { expect(subject['b']).to eq([42, 23]) }
17
+
18
+ example { expect(JSON.pretty_generate(subject)).to eq(<<-EOT.chomp) }
19
+ {
20
+ "a": {
21
+ "b": 2
22
+ },
23
+ "b": [
24
+ 42,
25
+ 23
26
+ ]
27
+ }
28
+ EOT
29
+
30
+ end
31
+
32
+ describe 'multi' do
33
+
34
+ subject { JSON.parse_multi(source) }
35
+
36
+ let(:multi) { subject.fetch_multi('a') }
37
+
38
+ let(:pretty) { <<-EOT.chomp }
39
+ {
40
+ "a": 1,
41
+ "b": [
42
+ 42,
43
+ 23
44
+ ],
45
+ "a": {
46
+ "b": 2
47
+ }
48
+ }
49
+ EOT
50
+
51
+ example { expect(subject).to be_a(Hash) }
52
+ example { expect(subject.size).to eq(3) }
53
+ example { expect(subject.keys).to eq(%w[a b a]) }
54
+
55
+ example { expect(subject['a']).to eq(1) } unless RUBY_ENGINE == 'jruby'
56
+ example { expect(subject['b']).to eq([42, 23]) }
57
+
58
+ example { expect(multi[0]).to eq(1) }
59
+ example { expect(multi[1]).to be_a(Hash) }
60
+ example { expect(multi[1]['b']).to eq(2) }
61
+
62
+ example { expect(JSON.pretty_generate(subject)).to eq(pretty) }
63
+ example { expect(JSON.pretty_print_multi(source)).to eq(pretty) }
64
+
65
+ end
66
+
67
+ end
@@ -23,11 +23,11 @@ describe_extended Object, Nuggets::Object::BlankMixin do
23
23
  example { o.should be_vain }
24
24
  }
25
25
 
26
- [['', [], [nil], {}], { :x => nil, :y => [], :z => { :zz => nil } }].each { |o|
26
+ [['', [], [nil], {}], { x: nil, y: [], z: { zz: nil } }].each { |o|
27
27
  example { o.should_not be_void }
28
28
  }
29
29
 
30
- [['', [], [nil], {}], { :x => nil, :y => [], :z => { :zz => nil } }].each { |o|
30
+ [['', [], [nil], {}], { x: nil, y: [], z: { zz: nil } }].each { |o|
31
31
  example { o.should be_vain }
32
32
  }
33
33
 
@@ -2,12 +2,13 @@ require 'nuggets/object/singleton_class'
2
2
 
3
3
  describe_extended Object, Nuggets::Object::SingletonClassMixin do
4
4
 
5
- objects = [Class, Object.new, Class.new, 's', [1, 2], { :a => 'b' }]
5
+ objects = [Class, Object.new, Class.new, 's', [1, 2], { a: 'b' }]
6
6
  objects.unshift(Object) unless %w[jruby rbx].include?(RUBY_ENGINE)
7
7
 
8
8
  objects.each { |o|
9
9
  example { o.should_not be_a_singleton_class }
10
- example { lambda { o.singleton_object }.should raise_error(TypeError) }
10
+ example(nil, if: o != Object || RUBY_VERSION < '2.3') {
11
+ lambda { o.singleton_object }.should raise_error(TypeError) }
11
12
 
12
13
  s = o.singleton_class
13
14
 
@@ -0,0 +1,85 @@
1
+ require 'nuggets/string/format'
2
+
3
+ describe_extended String, Nuggets::String::FormatMixin do
4
+
5
+ example do
6
+ expect{'a%bc'.format}.to raise_error(ArgumentError, 'wrong number of arguments (given 0, expected 1)')
7
+ end
8
+
9
+ example do
10
+ expect('a%bc'.format('b' => 'foo')).to eq('afooc')
11
+ end
12
+
13
+ example do
14
+ expect('a%bc'.format(b: 'foo')).to eq('afooc')
15
+ end
16
+
17
+ example do
18
+ expect('a%bc'.format(b: :foo)).to eq('afooc')
19
+ end
20
+
21
+ example do
22
+ expect('a%bc'.format(b: 42)).to eq('a42c')
23
+ end
24
+
25
+ example do
26
+ expect('a%bc'.format(b: '')).to eq('ac')
27
+ end
28
+
29
+ example do
30
+ expect('a%bc'.format(b: nil)).to eq('ac')
31
+ end
32
+
33
+ example do
34
+ expect('a%{foo}c'.format('foo' => 'bar')).to eq('abarc')
35
+ end
36
+
37
+ example do
38
+ expect('a%{foo}c'.format(foo: 'bar')).to eq('abarc')
39
+ end
40
+
41
+ example do
42
+ expect('a%{foo}c'.format(foo: :bar)).to eq('abarc')
43
+ end
44
+
45
+ example do
46
+ expect(''.format({})).to eq('')
47
+ end
48
+
49
+ example do
50
+ expect('%'.format({})).to eq('%')
51
+ end
52
+
53
+ example do
54
+ expect('%%'.format({})).to eq('%')
55
+ end
56
+
57
+ example do
58
+ expect{'%a'.format({})}.to raise_error(KeyError, 'key not found: a')
59
+ end
60
+
61
+ example do
62
+ expect{'%a'.format('b' => 'foo')}.to raise_error(KeyError, 'key not found: a')
63
+ end
64
+
65
+ example do
66
+ expect{'%a'.format(Hash.new(42))}.to raise_error(KeyError, 'key not found: a')
67
+ end
68
+
69
+ example do
70
+ expect('a%bc'.format(&:upcase)).to eq('aBc')
71
+ end
72
+
73
+ example do
74
+ expect('a%bc'.format{''}).to eq('ac')
75
+ end
76
+
77
+ example do
78
+ expect{'a%bc'.format{}}.to raise_error(ArgumentError, 'malformed format string - %b')
79
+ end
80
+
81
+ example do
82
+ expect('a%bc%d%{foo}%%{baz}'.format(b: 42, d: 'D', 'foo' => :bar)).to eq('a42cDbar%{baz}')
83
+ end
84
+
85
+ end
@@ -0,0 +1,47 @@
1
+ require 'nuggets/string/highlight'
2
+
3
+ describe_extended String, Nuggets::String::HighlightMixin do
4
+
5
+ unless RUBY_ENGINE == 'jruby' && JRUBY_VERSION.include?('pre')
6
+
7
+ example do
8
+ s = 'lingo go do the go go'; t = s.dup
9
+ expect(s.highlight('lingo')).to eq('|lingo| go do the go go')
10
+ expect(s).to eq(t)
11
+
12
+ expect(s.highlight(/lingo/)).to eq('|lingo| go do the go go')
13
+ expect(s.highlight(/l.*ngo?/)).to eq('|lingo| go do the go go')
14
+
15
+ q = ['lingo', 'go', 'go go']
16
+ expect(s.highlight(q)).to eq('|lingo go| do the |go go|')
17
+ end
18
+
19
+ example do
20
+ s = 'lingö go do the go go'
21
+ q = ['lingö', 'go', 'go go']
22
+ expect(s.highlight(q)).to eq('|lingö| |go| do the |go go|')
23
+ end
24
+
25
+ example do
26
+ s = 'lingo go do the go go'
27
+ q = ['lingo', 'go', 'go go']
28
+ expect(s.highlight(q, '^')).to eq('^lingo go^ do the ^go go^')
29
+ expect(s.highlight(q, '<', '>')).to eq('<lingo go> do the <go go>')
30
+ expect(s.highlight(q, '<i>', '</i>')).to eq('<i>lingo go</i> do the <i>go go</i>')
31
+ end
32
+
33
+ example do
34
+ s = 'The fox went out on a chilly night'
35
+ q = [/[eo]n/]
36
+ expect(s.highlight(q)).to eq('The fox w|en|t out |on| a chilly night')
37
+ q << 'went'
38
+ expect(s.highlight(q)).to eq('The fox |went| out |on| a chilly night')
39
+ q << /ou.*a/
40
+ expect(s.highlight(q)).to eq('The fox |went| |out on a| chilly night')
41
+ q << 'went out'
42
+ expect(s.highlight(q)).to eq('The fox |went out on a| chilly night')
43
+ end
44
+
45
+ end
46
+
47
+ end