Checked 3.4.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,4 +24,6 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'rake'
25
25
  s.add_development_dependency 'bacon'
26
26
  s.add_development_dependency 'Bacon_Colored'
27
+
28
+ s.add_runtime_dependency 'term-ansicolor'
27
29
  end
@@ -1,6 +1,7 @@
1
1
  require "Checked/version"
2
2
  require "Checked/DSL"
3
3
  require "Checked/Arch"
4
+ require "Checked/Spec"
4
5
 
5
6
 
6
7
  class Checked
@@ -1,3 +1,5 @@
1
+ require 'term/ansicolor'
2
+
1
3
  class Object
2
4
 
3
5
  def Checked= val
@@ -43,6 +45,18 @@ class Checked
43
45
 
44
46
  module Ruby
45
47
 
48
+ def spec val, msg
49
+ ::Checked::SPECS.spec val, msg
50
+ end
51
+
52
+ def spec! val, msg
53
+ ::Checked::SPECS.spec! val, msg
54
+ end
55
+
56
+ def dont_print_specs
57
+ ::Checked::SPECS.dont_print
58
+ end
59
+
46
60
  def demand val, bool, raw_msg
47
61
  return val if bool
48
62
 
@@ -0,0 +1,63 @@
1
+
2
+ class Checked
3
+
4
+ class Spec
5
+ Fail = Class.new(RuntimeError)
6
+ end
7
+
8
+ class Specs
9
+
10
+ include Term::ANSIColor
11
+
12
+ def initialize
13
+ @specs = []
14
+ @print = true
15
+ end
16
+
17
+ def spec val, msg
18
+ if to_a.empty?
19
+ at_exit { ::Checked::SPECS.print if !$! && ::Checked::SPECS.print? }
20
+ end
21
+ @specs << [val, msg]
22
+ end
23
+
24
+ def spec! val, msg
25
+ if val
26
+ spec val, msg
27
+ else
28
+ print
29
+ dont_print
30
+ raise Checked::Spec::Fail, red(msg)
31
+ end
32
+ end
33
+
34
+ def to_a
35
+ @specs
36
+ end
37
+
38
+ def dont_print
39
+ @print = false
40
+ end
41
+
42
+ def print?
43
+ @print
44
+ end
45
+
46
+ def print
47
+ @specs.each { |pair|
48
+
49
+ val, msg = pair
50
+
51
+ if val
52
+ $stdout.print green(msg), "\n"
53
+ else
54
+ $stdout.print red(msg), "\n"
55
+ end
56
+
57
+ }
58
+ end
59
+
60
+ end # === class Specs
61
+
62
+ SPECS = Specs.new
63
+ end # === class Checked
@@ -1,3 +1,3 @@
1
1
  class Checked
2
- VERSION = "3.4.0"
2
+ VERSION = "3.5.0"
3
3
  end
@@ -14,11 +14,10 @@ end
14
14
 
15
15
 
16
16
  FOLDER = ("/tmp/Checked_Test")
17
+ %x! rm -r #{FOLDER} ! if File.directory?(FOLDER)
17
18
  %x! mkdir -p #{FOLDER}!
18
- at_exit {
19
- %x! rm -rf #{FOLDER} !
20
- }
21
19
 
20
+ require 'open3'
22
21
  def ruby_e cmd
23
22
  file = "#{FOLDER}/delete_me_perf_#{rand(100000)}.rb"
24
23
  begin
@@ -29,12 +28,25 @@ def ruby_e cmd
29
28
  #{cmd}
30
29
  ~
31
30
  }
32
- %x[ bundle exec ruby #{file} 2>&1].strip
33
- ensure
34
- File.delete file
31
+ # %x[ bundle exec ruby #{file} 2>&1].strip
32
+
33
+ data = ''
34
+ Open3.popen3(" bundle exec ruby #{file}") { |i, o, e, t|
35
+ data << o.read
36
+ data << e.read
37
+ }
38
+ data.strip
35
39
  end
36
40
  end
37
41
 
42
+ def dsl_e cmd
43
+ ruby_e %~
44
+ require 'Checked'
45
+ extend Checked::DSL
46
+ #{cmd}
47
+ ~
48
+ end
49
+
38
50
  Dir.glob('spec/tests/*.rb').each { |file|
39
51
  require File.expand_path(file.sub('.rb', '')) if File.file?(file)
40
52
  }
@@ -65,6 +65,79 @@ describe "Size!" do
65
65
 
66
66
  end # === describe Sizes_0_1!
67
67
 
68
+ describe ":spec" do
69
+
70
+ before { extend Term::ANSIColor }
71
+
72
+ it 'outputs a green line if pass' do
73
+ dsl_e(%~
74
+ spec true, 'It passed.'
75
+ ~).should == green("It passed.")
76
+ end
77
+
78
+ it 'outputs a red line if fail' do
79
+ dsl_e(%~
80
+ spec false, 'It failed.'
81
+ ~).should == red("It failed.")
82
+ end
83
+
84
+ it 'prints message :at_exit' do
85
+ dsl_e(%~
86
+ spec true, 'It is true.'
87
+ puts "First output."
88
+ ~).should == "First output.\n#{green 'It is true.'}"
89
+ end
90
+
91
+ it 'does not output message if there were any exceptions raised' do
92
+ dsl_e(%~
93
+ spec true, "Never gets printed."
94
+ raise "something"
95
+ ~)
96
+ .should.match %r!\A/tmp/Checked_Test/[a-zA-Z\_\-\d]+.rb:\d+:in `<main>': something \(RuntimeError\)\Z!
97
+ end
98
+
99
+ end # === describe :spec
100
+
101
+ describe ":spec! bool, msg" do
102
+
103
+ it 'prints remaining specs, then raises Spec::Failed' do
104
+ start = "\e[32mThis is true.\e[0m\n\e[31mThis is false.\e[0m\n/home/#{`whoami`.strip}"
105
+ middle = ":in `spec!': \e[31mThis is error.\e[0m (Checked::Spec::Fail)\n\tfrom /"
106
+ ending = ":in `<main>'"
107
+ m = dsl_e(%~
108
+ spec true, "This is true."
109
+ spec false, "This is false."
110
+ spec! false, "This is error."
111
+ spec true, "This never gets printed."
112
+ ~)
113
+
114
+ m.index(start).should == 0
115
+ m[middle].should == middle
116
+ m.should.match %r!#{Regexp.escape ending}\Z!
117
+ end
118
+
119
+ end # === describe :spec! bool, msg
120
+
121
+ describe ":dont_print_specs" do
122
+
123
+ it 'prevents specs from being printed' do
124
+ dsl_e(%~
125
+ puts 'No specs printed.'
126
+ spec true, "Never gets printed."
127
+ dont_print_specs
128
+ ~).should == 'No specs printed.'
129
+ end
130
+
131
+ it 'sets SPECS.print? to false' do
132
+ dsl_e(%~
133
+ puts Checked::SPECS.print?.inspect
134
+ dont_print_specs
135
+ puts Checked::SPECS.print?.inspect
136
+ ~).should == "true\nfalse"
137
+ end
138
+
139
+ end # === describe SPECS#dont_print
140
+
68
141
  describe "string! " do
69
142
 
70
143
  behaves_like :ruby_dsl
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Checked
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-29 00:00:00.000000000 Z
12
+ date: 2012-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &14833040 !ruby/object:Gem::Requirement
16
+ requirement: &7925600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *14833040
24
+ version_requirements: *7925600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bacon
27
- requirement: &14831340 !ruby/object:Gem::Requirement
27
+ requirement: &7925100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14831340
35
+ version_requirements: *7925100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: Bacon_Colored
38
- requirement: &14744240 !ruby/object:Gem::Requirement
38
+ requirement: &7924600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,18 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14744240
46
+ version_requirements: *7924600
47
+ - !ruby/object:Gem::Dependency
48
+ name: term-ansicolor
49
+ requirement: &7923980 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *7923980
47
58
  description: ! "\n Various DSLs to clean, question (Ask), and validate (Demand)
48
59
  your objects,\n their classes (data types), and their properties.\n "
49
60
  email:
@@ -77,6 +88,7 @@ files:
77
88
  - lib/Checked/Demand/Strings.rb
78
89
  - lib/Checked/Demand/Symbols.rb
79
90
  - lib/Checked/Demand/Vars.rb
91
+ - lib/Checked/Spec.rb
80
92
  - lib/Checked/version.rb
81
93
  - spec/helper.rb
82
94
  - spec/main.rb